在CentOS上,使用shell脚本安装MySQL

在CentOS上,使用shell脚本安装MySQL

  • 文本使用shell脚本,在CentOS上自动安装MySQL。
  • 包括历史版本mysql卸载、历史MySQL数据备份、新版MySQL安装、MySQL数据恢复等功能。
  • 自动安装、初始化MySQL,默认密码123456.
  • 包括脚本、初始化MySQL配置文件两个。

安装脚本

baseCatalogue=/home
backupCatalogue=/home
mysqlDataCatalogue=/home

#Choose your operation
operationChoice() {
	echo -e "Hello, Please choose your operation"
	echo -e "0. exit"
	echo -e "1. Set base catalogue(After your set it, all your opertaioni will base on it. Default base catalogue is /home)"
	echo -e "2. Backup MySql Data"
	echo -e "3. UninstallOld MySql"
	echo -e "4. Install New MySql"
	echo -e "5. Recovery MySql Data"
	read -p "Please enter your option... " i
	if [[ "$i" == "0" ]]; then
		exit 0
	fi
	if [[ "$i" != "1" ]] && [[ "$i" != "2" ]] && [[ "$i" != "3" ]] && [[ "$i" != "4" ]]&& [[ "$i" != "5" ]]; then
		echo -e 'Incorrect Choice'
		operationChoice
	fi
	if [[ "$i" == "0" ]]; then
		exit
	elif [[ "$i" == "1" ]]; then
		setBaseCatalogue
	elif [[ "$i" == "2" ]]; then
		backupMysqlData
	elif [[ "$i" == "3" ]]; then
		uninstallOldMysql
	elif [[ "$i" == "4" ]]; then
		installNewMysql
	elif [[ "$i" == "5" ]]; then
		recoveryMysqlData
	else
		return ${i}
	fi
}

#Set base catalogue
setBaseCatalogue(){
	read -p "Please enter the base catalogue:" bc
	baseCatalogue=$bc
	#Check the catalogue if exist
	if [ -d ${baseCatalogue} ]; then
		echo -e "Set base catalogue success, baseCatalogue is ${baseCatalogue}"
		operationChoice
	else
		echo -e "${baseCatalogue} is not a catalogue"
		setBaseCatalogue
	fi
}

#Back up mysql data
backupMysqlData() {
	echo '###############################Begin backup mysql data###############################'
	#Stop MySQL
	systemctl stop mysqld
	#Create backup catalogue
	backupCatalogue=${baseCatalogue}/`date +%Y%m%d`/mysqlBackup
	sudo mkdir -p ${backupCatalogue}
	if [ -d ${backupCatalogue} ]; then
		echo -e "Backup Catalogue is created: "${backupCatalogue}
	else
		echo -e "Backup catalogue "${backupCatalogue}" is created failure, please check the reason"
		exit 0
	fi
	#Enter the MySQL data catalogue
	read -p "Please enter the MySQL data cataloague:" myc
	mysqlDataCatalogue=$myc
	if [ -d ${mysqlDataCatalogue} ]; then
		echo -e "Begin backup process......"
	else
		echo -e "${mysqlDataCatalogue} is not a catalogue, Please check the reason"
		exit 0
	fi
	sudo cp -r ${mysqlDataCatalogue}/* ${backupCatalogue}
	sudo tar -cvf mysql`date +%Y%m%d`.tar ${backupCatalogue}
	echo -e "Backup process finish, here is it "
	ls ${backupCatalogue}	
	echo '##################Backup mysql data finished###############'
	operationChoice
}

#Uninstall old version MySQL
uninstallOldMysql() {
	read -p "Before uninstall old version mysql, make sure you have execute step 1 to backup mysql data. Are you sure? " i
	if [[ $i == "y" ]] || [[ $i == "Y" ]]; then
		echo -e '##########Begin uninstall old version mysql############'
		sudo systemctl stop mysqld
		#Uninstall
		delMysql=$(rpm -qa | grep -i mysql)
		for m in $delMysql; do
			echo "delete $m"
			yes | yum remove $m
		done
		yes | yum remove mysql*
		# delete mariadb
		yum list installed mariadb*
		yum remove -y mariadb*
		# Delete files and catalogue about mysql
		res=$(find / -name mysql)
		for r in $res; do
			#Don't delete the mysql backup
			if [[ $r != *${backupCatalogue}* ]]; then
				echo "delete $r"
				rm -rf $r
			fi
		done
		echo -e '##########Uninstall old version mysql finished############'
	fi
	operationChoice
}

#Inastall new version MySQL
installNewMysql() {
	echo -e '##########install new version mysql############'
	cd ${baseCatalogue}
	# unzip mysql tar
	tar -xf mysql-*.tar
	# install
	yes | yum localinstall -y mysql-community-common-5.7.41-1.el7.x86_64.rpm
	yes | yum localinstall -y mysql-community-libs-5.7.41-1.el7.x86_64.rpm
	yes | yum localinstall -y mysql-community-client-5.7.41-1.el7.x86_64.rpm
	yes | yum localinstall -y mysql-community-server-5.7.41-1.el7.x86_64.rpm
	#Create files and catalogue about mysql
	read -p "Please enter the catalogue to store mysql data:" mysqlDataPlace
	mysqlDataCatalogue=${mysqlDataPlace}
	mkdir -p /${mysqlDataCatalogue}/mysql/{data,logs,tmp,mysql-files,mysql-keyring}
	touch /${mysqlDataCatalogue}/mysql/mysqld.pid
	touch /${mysqlDataCatalogue}/mysql/mysql.sock

	#authorisation
	chown -R mysql:mysql /${mysqlDataCatalogue}/mysql
	chmod -R 750 /${mysqlDataCatalogue}/mysql

	# update mysql configs
	sed -i 's/\/data\//\/'"${mysqlDataCatalogue}"'\//g' my.cnf my_skip.cnf
	#update mysql configs
	yes | cp my_skip.cnf /etc/my.cnf
	mysqld --initialize --user mysql &&
		echo 'Mysql initialize successfully'

	systemctl start mysqld.service
	echo 'Mysql running'

	mysql <<EOF1
use mysql;
update user set authentication_string=PASSWORD('123456') where user='root';	
flush privileges;
quit
EOF1

	echo 'Mysql root changed successfully'
	# sleep 1 second before restart
	sleep 1

	yes | cp my.cnf /etc/my.cnf
	systemctl restart mysqld.service
	echo 'Mysql restart'

	# authorisation
	chown -R mysql:mysql /${mysqlDataCatalogue}/mysql
	chmod -R 750 /${mysqlDataCatalogue}/mysql

	mysql -uroot -p123456 --connect-expired-password <<EOF
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
grant all privileges on *.* to root@"%" identified by "'123456";
ALTER USER 'root' IDENTIFIED BY '123456';
flush privileges;
quit
EOF
	echo -e '##########mysql password changed############'
	# register as startup services
	systemctl daemon-reload &&
		systemctl enable mysqld &&
		systemctl start mysqld &&
		echo -e '##########install new version mysql finished############'
	mysql --version
	operationChoice
}

#Recovery backup mysql data
recoveryMysqlData() {
	read -p "Please enter the mysql data backup catalogue:" bc
	backupCatalogue=${bc}
	echo "Mysql backup data is at:${backupCatalogue}"
	yes | cp -rf ${backupCatalogue}/* /${mysqlDataCatalogue}/mysql/data/
	chown -R mysql:mysql /${mysqlDataCatalogue}/mysql
	chmod -R 750 /${mysqlDataCatalogue}/mysql
	systemctl restart mysqld.service
	echo -e '##########Recovery data finished############'
}

operationChoice

配置文件

  • my.cnf
[mysqld]
#
#remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
server_id = 100
datadir=/home/mysql/data
socket=/home/mysql/mysql.sock
log-error=/home/mysql/mysqld.log
pid-file=/home/mysql/mysqld.pid
log-bin=/home/mysql/logs/logbin.log
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
lower_case_table_names=1
character_set_server=utf8
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
explicit_defaults_for_timestamp=1
[client]
socket=/home/mysql/mysql.sock

  • my_skip.cnf
[mysqld]
#
#remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
server_id = 100
datadir=/home/mysql/data
socket=/home/mysql/mysql.sock
log-error=/home/mysql/mysqld.log
pid-file=/home/mysql/mysqld.pid
log-bin=/home/mysql/logs/logbin.log
skip-grant-tables=1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
lower_case_table_names=1
character_set_server=utf8
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
 
explicit_defaults_for_timestamp=1
[client]
socket=/home/mysql/mysql.sock