代码改变世界

MYSQL Slave开机启动脚本

2021-06-08 09:58  猎手家园  阅读(760)  评论(0编辑  收藏  举报

MYSQL做数据库主从时,从库的Slave默认是不启动的。

1、新建脚本mysql-slave-start.sh

#!/bin/sh
user=root
password="123456"
mysql -u$user -p$password -e "show slave status\G;" | grep -i running > slavestatus.log

if [ `cat slavestatus.log | grep -i IO | awk '{print $2}'` = Yes ];
  then
     echo "slave Io process is OK"
  else
     mysql -u$user -p$password  -e "stop slave;"
     wait;
     mysql -u$user -p$password  -e "start slave;"
     wait;
     echo "`date` IO slave has started" >> slavemon.log
fi

if [ `cat slavestatus.log | grep -i SQL | awk '{print $2}'` = Yes ];
   then
     echo "slave SQL process is OK"
   else
     mysql -u$user -p$password  -e "stop slave;"
     wait;
     mysql -u$user -p$password  -e "start slave;"
     wait;
     echo "`date` SQL slave has started" >> slavemon.log
fi

 

2、赋予可执行权限

chmod +x mysql-slave-start.sh

 

3、加入开机启动

vim /etc/rc.local

# 追加到末尾即可
/root/start-up/mysql-slave-start.sh

配置完毕!