开发目的:实际企业项目中集群服务器宕机后,Mysql主主默认不支持重启后自动配置.一旦出现突发状况,给企业项目带来极大的不利与损失.本文旨在使用Linux系统,通过编写Linux脚本实现全自动监控Mysql服务,服务器宕机后会立即重启Mysql服务并自动配置主主模式.使系统仍然正常运行,服务器宕机的影响降低最小化.
开发背景:这里假如有2台已经配置了主主模式的数据库node1和node2.ip地址如下:
mysql node1:
172.26.2.114
mysql node2:
172.26.2.115
开发步骤:
在172.26.2.114和172.26.2.115两台服务器分别编写监控Mysql服务的脚本startup.sh用以自动检测Mysql服务是否正常运行.
#!/bin/bash
#author by xiaokai 2019-10-08
while true
do
sleep 5
count=`ps -ef | grep mysql | grep -v "grep" | wc -l`
#echo $count
if [ 0 == $count ]; then
#进程不存在
time2=$(date "+%Y-%m-%d %H:%M:%S")
#echo "mysql服务宕机,正在重新启动"
echo 'mysql服务宕机,宕机时间为:'$time2>>error.log
sleep 2
service mysqld restart
sleep 15
sh setMysql.sh
sleep 5
echo 'mysql重新启动成功!!!'
#else
#rest进程存在,杀掉进程
#echo "mysql服务运行正常...持续监控中"
fi
#time=$(date "+%Y-%m-%d %H:%M:%S")
#echo "本轮监测完毕,当前时间为:"$time
done
3.在172.26.2.114和172.26.2.115两台服务器分别编写同步Mysql主主模式配置的脚本setMysql.sh.
这里假定数据库node1和node2节点的连接账号和密码为xiaokai和123456
#!/bin/bash
#author by xiaokai 2019-07-29
#-*- coding:utf-8 -*-
#重启数据库
#echo 'restart mysql1 node...'
#docker restart mysql1
#echo 'restart mysql2 node...'
#docker restart mysql2
echo -e "\e[1;34m connect mysql node1... \n"
echo 'connect mysql node1...'
#数据库ip
HOSTNAME1="172.26.2.114"
#数据库端口
PORT1="3306"
#数据库用户名
USERNAME1="xiaokai"
#数据库密码
PASSWORD1="123456"
#数据库名
DBNAME1="mysql"
#查询语句
select_sql="show master status"
#*#执行查询*
result1=`mysql -h${HOSTNAME1} -P${PORT1} -u${USERNAME1} -p${PASSWORD1} ${DBNAME1} -Bse "${select_sql}"`
node1_file_position=`echo ${result1} | awk '{print $1}'` #获取到的第一个值
node1_Postion=`echo ${result1} | awk '{print $2}'` #获取到的第二个值
node1_comtend=`echo ${result1} | awk '{print $n}'` #获取到的第N个值
echo 'node1_file_position:'$node1_file_position
echo 'node1_Postion:'$node1_Postion
echo 'node1_comtend:'$node1_comtend
echo '***************************'
echo 'connect mysql node2...'
#数据库ip
HOSTNAME2="172.26.2.115"
#数据库端口
PORT2="3306"
#数据库用户名
USERNAME2="xiaokai"
#数据库密码
PASSWORD2="123456"
#数据库名
DBNAME2="mysql"
#查询语句
select_sql="show master status"
#*#执行查询*
result2=`mysql -h${HOSTNAME2} -P${PORT2} -u${USERNAME2} -p${PASSWORD2} ${DBNAME2} -Bse "${select_sql}"`
node2_file_position=`echo ${result2} | awk '{print $1}'` #获取到的第一个值
node2_Postion=`echo ${result2} | awk '{print $2}'` #获取到的第二个值
node2_comtend=`echo ${result2} | awk '{print $n}'` #获取到的第N个值
echo 'node2_file_position:'$node2_file_position
echo 'node2_Postion:'$node2_Postion
echo 'node2_comtend:'$node2_comtend
#创建同步帐号并授权
create_acount_sql="create user 'xiaokai'@'%' identified by '123456"
`mysql -h${HOSTNAME1} -P${PORT1} -u${USERNAME1} -p${PASSWORD1} ${DBNAME1} -Bse "${create_acount_sql}"`
grant_acount_sql="grant all on *.* to 'xiaokai'@'%' identified by '123456'"
`mysql -h${HOSTNAME1} -P${PORT1} -u${USERNAME1} -p${PASSWORD1} ${DBNAME1} -Bse "${grant_acount_sql}"`
`mysql -h${HOSTNAME2} -P${PORT2} -u${USERNAME2} -p${PASSWORD2} ${DBNAME2} -Bse "${create_acount_sql}"`
`mysql -h${HOSTNAME2} -P${PORT2} -u${USERNAME2} -p${PASSWORD2} ${DBNAME2} -Bse "${grant_acount_sql}"`
echo '同步帐号授权完毕'
#配置从节点写入
stop_slave_sql="stop slave"
`mysql -h${HOSTNAME1} -P${PORT1} -u${USERNAME1} -p${PASSWORD1} ${DBNAME1} -Bse "${stop_slave_sql}"`
`mysql -h${HOSTNAME2} -P${PORT2} -u${USERNAME2} -p${PASSWORD2} ${DBNAME2} -Bse "${stop_slave_sql}"`
#配置node1
change_master_node1="CHANGE MASTER TO MASTER_HOST='${HOSTNAME1}',MASTER_PORT=${PORT1},MASTER_USER='${USERNAME1}',MASTER_PASSWORD='${PASSWORD1}',MASTER_LOG_FILE='${node1_file_position}',MASTER_LOG_POS=${node1_Postion}"
change_master_node2="CHANGE MASTER TO MASTER_HOST='${HOSTNAME2}',MASTER_PORT=${PORT2},MASTER_USER='${USERNAME2}',MASTER_PASSWORD='${PASSWORD2}',MASTER_LOG_FILE='${node2_file_position}',MASTER_LOG_POS=${node2_Postion}"
echo ${change_master_node1}
echo ${change_master_node2}
echo '==============================================='
start_slave_sql="start slave"
`mysql -h${HOSTNAME2} -P${PORT2} -u${USERNAME2} -p${PASSWORD2} ${DBNAME2} -Bse "${change_master_node1}"`
sleep 1s
`mysql -h${HOSTNAME2} -P${PORT2} -u${USERNAME2} -p${PASSWORD2} ${DBNAME2} -Bse "${start_slave_sql}"`
sleep 1s
`mysql -h${HOSTNAME1} -P${PORT1} -u${USERNAME1} -p${PASSWORD1} ${DBNAME1} -Bse "${change_master_node2}"`
sleep 1s
`mysql -h${HOSTNAME1} -P${PORT1} -u${USERNAME1} -p${PASSWORD1} ${DBNAME1} -Bse "${start_slave_sql}"`
#回调slave状态
show_slave_sql="show slave status"
slave1_status=`mysql -h${HOSTNAME1} -P${PORT1} -u${USERNAME1} -p${PASSWORD1} ${DBNAME1} -Bse "${show_slave_sql}"`
slave_node1_status=`echo ${slave1_status} | awk '{print $n}'`
echo 'node1:'${slave_node1_status}
echo '执行完毕'
echo -e '\e[0m'
#查询语句
#select_sql2='select now()'
#dateResult=`mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -Bse "${select_sql2}"`
#time2=`echo ${dateResult}|awk '{print $n}'`
#echo 'time:'$time2
4.分别在两台服务器启动startup.sh和setMysql.sh脚本(两个脚本放在同一目录下),以后台命令启动即可以实现后台监控.
nohup startup.sh &
5.因服务器宕机后脚本会失效,所以还要额外在服务器添加crontab任务.
输入命令crontab -e打开任务配置界面
@reboot(sleep 10; nohup \location\startup.sh )
这样配置后服务器宕机后,重启后也会自动后台加载启动脚本实现全自动监控配置Mysql服务啦.
最后总结:因Mysql主主官方默认不支持重启后自动配置主主模式,使用Linux脚本可以极大简化开发时间成本,实现零人工,低成本的解决企业项目数据库因服务器宕机或其它原因导致的数据库异常终止.实现项目敏捷开发与上线的目的.