baseCatalogue=/home
backupCatalogue=/home
mysqlDataCatalogue=/home
#Choose your operationoperationChoice(){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"]];thenexit0fiif[["$i"!="1"]]&&[["$i"!="2"]]&&[["$i"!="3"]]&&[["$i"!="4"]]&&[["$i"!="5"]];thenecho -e 'Incorrect Choice'
operationChoice
fiif[["$i"=="0"]];thenexitelif[["$i"=="1"]];then
setBaseCatalogue
elif[["$i"=="2"]];then
backupMysqlData
elif[["$i"=="3"]];then
uninstallOldMysql
elif[["$i"=="4"]];then
installNewMysql
elif[["$i"=="5"]];then
recoveryMysqlData
elsereturn${i}fi}#Set base cataloguesetBaseCatalogue(){read -p "Please enter the base catalogue:"bcbaseCatalogue=$bc#Check the catalogue if existif[ -d ${baseCatalogue}];thenecho -e "Set base catalogue success, baseCatalogue is ${baseCatalogue}"
operationChoice
elseecho -e "${baseCatalogue} is not a catalogue"
setBaseCatalogue
fi}#Back up mysql databackupMysqlData(){echo'###############################Begin backup mysql data###############################'#Stop MySQL
systemctl stop mysqld
#Create backup cataloguebackupCatalogue=${baseCatalogue}/`date +%Y%m%d`/mysqlBackup
sudomkdir -p ${backupCatalogue}if[ -d ${backupCatalogue}];thenecho -e "Backup Catalogue is created: "${backupCatalogue}elseecho -e "Backup catalogue "${backupCatalogue}" is created failure, please check the reason"exit0fi#Enter the MySQL data catalogueread -p "Please enter the MySQL data cataloague:" myc
mysqlDataCatalogue=$mycif[ -d ${mysqlDataCatalogue}];thenecho -e "Begin backup process......"elseecho -e "${mysqlDataCatalogue} is not a catalogue, Please check the reason"exit0fisudocp -r ${mysqlDataCatalogue}/* ${backupCatalogue}sudotar -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 MySQLuninstallOldMysql(){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"]];thenecho -e '##########Begin uninstall old version mysql############'sudo systemctl stop mysqld
#UninstalldelMysql=$(rpm -qa |grep -i mysql)formin$delMysql;doecho"delete $m"yes| yum remove $mdoneyes| yum remove mysql*
# delete mariadb
yum list installed mariadb*
yum remove -y mariadb*
# Delete files and catalogue about mysqlres=$(find / -name mysql)forrin$res;do#Don't delete the mysql backupif[[$r!= *${backupCatalogue}* ]];thenecho"delete $r"rm -rf $rfidoneecho -e '##########Uninstall old version mysql finished############'fi
operationChoice
}#Inastall new version MySQLinstallNewMysql(){#Due to modifying the default storage directory of the MySQL database, opening selinux will result in write failure. Use the following command to give system init running privileges
semanage fcontext -a -t mysqld_db_t '/${mysqlDataCatalogue}/mysql(/.*)?'
semanage fcontext -a -t mysqld_db_t '/${mysqlDataCatalogue}/mysql/logs(/.*)?'
semanage fcontext -a -t mysqld_db_t '/${mysqlDataCatalogue}/mysql/mysql-files(/.*)?'
semanage fcontext -a -t mysqld_db_t '/${mysqlDataCatalogue}/mysql/mysql-keyring(/.*)?'
semanage fcontext -a -t mysqld_db_t '/${mysqlDataCatalogue}/mysql/tmp(/.*)?'ls -lhZ /${mysqlDataCatalogue}/mysql/
restorecon -FvvR
chcon -t mysqld_db_t /${mysqlDataCatalogue}/mysql -R
echo -e '##########install new version mysql############'cd${baseCatalogue}# unzip mysql tartar -xf mysql-*.tar
# installyes| 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 mysqlread -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
#authorisationchown -R mysql:mysql /${mysqlDataCatalogue}/mysql
chmod -R 750 /${mysqlDataCatalogue}/mysql
# update mysql configssed -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