#!/bin/bash	

mysql -V
if [ $? -eq 0 ];then
    echo "<<========== MySQL已安装 =============>>"
	date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
	echo "<<========== MySQL已安装 =============>>" >> ./init.log
	echo "##########" >> ./init.log
	exit 1;
fi

echo "<<======= 部署Mysql(5.7)[开始] =======>>"
date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
echo "<<======= 部署Mysql(5.7)[开始] =======>>" >> ./init.log
echo "##########" >> ./init.log


#yum -y update
#wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
cd ./rar
yum -y localinstall mysql57-community-release-el7-8.noarch.rpm 
if [ $? -eq 0 ] ; then 
  echo -e "MySQL镜像下载                  \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo    "MySQL镜像下载--------------------------[完成]" >> ./init.log
  echo "##########" >> ./init.log
else
  echo -e "MySQL镜像下载                  \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo    "MySQL镜像下载--------------------------[失败]" >> ./init.log
  echo "##########" >> ./init.log
  exit 1;
fi
cd - 
#yum repolist enabled | grep "mysql.*-community.*"
yum -y install mysql-community-server --nogpgcheck
if [ $? -eq 0 ] ; then 
  echo -e "MySQL安装                      \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo    "MySQL安装------------------------------[完成]" >> ./init.log
  echo "##########" >> ./init.log
else 
  echo -e "MySQL安装                      \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo    "MySQL安装------------------------------[失败]" >> ./init.log
  echo "##########" >> ./init.log
  exit 1;
fi
cp /etc/my.cnf /etc/my.cnf.backup
sed -i "/\[mysqld\]/a\sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'" /etc/my.cnf
sed -i "/\[mysqld\]/a\max_connections=1000" /etc/my.cnf
sed -i "/\[mysqld\]/a\max_allowed_packet = 128M" /etc/my.cnf
sed -i "/\[mysqld\]/a\innodb_flush_log_at_trx_commit = 0" /etc/my.cnf
sed -i "/\[mysqld\]/a\innodb_log_buffer_size = 16M" /etc/my.cnf
sed -i "/\[mysqld\]/a\innodb_autoextend_increment = 128M" /etc/my.cnf
sed -i "/\[mysqld\]/a\innodb_log_file_size = 128M" /etc/my.cnf
sed -i "/\[mysqld\]/a\innodb_buffer_pool_size = 4096M" /etc/my.cnf
sed -i "/\[mysqld\]/a\bulk_insert_buffer_size = 120M" /etc/my.cnf
sed -i "/\[mysqld\]/a\skip-name-resolve" /etc/my.cnf
sed -i "/\[mysqld\]/a\# Simba-mysql" /etc/my.cnf

systemctl start mysqld
if [ $? -eq 0 ] ; then 
  echo -e "MySQL启动                      \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo    "MySQL启动------------------------------[完成]" >> ./init.log
  echo "##########" >> ./init.log
else 
  echo -e "MySQL启动                      \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo    "MySQL启动------------------------------[失败]" >> ./init.log
  echo "##########" >> ./init.log
  exit 1;
fi
systemctl enable mysqld 
systemctl daemon-reload

#if [ $? -eq 0 ] ; then echo "MySQL start OK！" ; else echo "MySQL start FAIL" ; exit 1 ; fi
password=$(cat /var/log/mysqld.log | awk '/root@localhost:/{print $11}')
mysqladmin -uroot -p$password password Simba.123456 
mysql -u root -pSimba.123456  << EOF 
	alter user 'root'@'localhost' identified by 'Simba.123456';
	alter user 'root'@'localhost' password expire never;
	grant all privileges on *.* to 'root'@'%' identified by 'Simba.123456' with grant option;
	flush privileges;
EOF
# set @@GLOBAL.sql_mode='';
if [ $? -eq 0 ] ; then 
  echo -e "root密码设置                   \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo    "root密码设置---------------------------[完成]" >> ./init.log
  echo "##########" >> ./init.log
  echo "<<======= 部署Mysql(5.7)[完成] =======>>"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo "<<======= 部署Mysql(5.7)[完成] =======>>" >> ./init.log
  echo "##########" >> ./init.log
else 
  echo -e "root密码设置                   \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo    "root密码设置---------------------------[失败]" >> ./init.log
  echo "##########" >> ./init.log
  echo "<<======= 部署Mysql(5.7)[失败] =======>>"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo "<<======= 部署Mysql(5.7)[失败] =======>>" >> ./init.log
  echo "##########" >> ./init.log
  exit 1;
fi
#rm -rf ./mysql57-community*
