MYSQL Slave開機啟動指令碼

獵手家園發表於2021-06-08

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

 

2、賦予可執行許可權

chmod +x mysql-slave-start.sh

 

3、加入開機啟動

vim /etc/rc.local

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

配置完畢!

 

相關文章