stand-alone multi-instance restart mysql database service

1、# cat db.txt
backend 3310
base 3320
storage 3330
payment 3340
promotion 3350

2、# cat restart_mysql_slave.sh
#!/bin/sh
export PATH=$PATH:/usr/local/mysql/bin

echo -e "\033[31m`date +"%Y-%m-%d %T"` Restart begin! \033[0m"

# Stop the database
/opt/ops-tool/stop_all.sh

# Start the database
/opt/ops-tool/start_all.sh


# Start from the library
/opt/ops-tool/start_slave.sh


# Check status from master
/opt/ops-tool/mysql_replication_check.sh

echo -e "\033[31m`date +"%Y-%m-%d %T"` Restart end! \n \033[0m"

3、# cat stop_all.sh
#!/bin/sh
export PATH=$PATH:/usr/local/mysql/bin

# Stop database
CAT /opt/ops-tool/db.txt | the while Read Line
do
pwd = 'XXX'
Port = $ (echo $ Line | awk '{} Print $ 2')
/ usr / local / MySQL / bin / mysqladmin Port -uroot--p -P $ -h127.0.0.1 $ pwd the shutdown &> / dev / null
SLEEP 10
DONE

4、# cat start_all.sh
/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/account_3550/conf/my.cnf &>/dev/null &
/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/backend_3310/conf/my.cnf &>/dev/null &
/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/base_3320/conf/my.cnf &>/dev/null &
/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/bbtoms_3520/conf/my.cnf &>/dev/null &
/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/bcoin_3610/conf/my.cnf &>/dev/null &

sleep 20

5、# cat mysql_replication_check.sh
#!/bin/sh
export PATH=$PATH:/usr/local/mysql/bin
cat /opt/ops-tool/db.txt| while read line
do
pwd='xxx'
service=$(echo $line |awk '{print $1}')
port=$(echo $line |awk '{print $2}')
mysql="/usr/local/mysql/bin/mysql -h127.0.0.1 -P$port -uroot -p$pwd"
$mysql -e "show slave status \G;" 2>/dev/null > tmp.log
IO_env=`cat tmp.log | grep -w Slave_IO_Running | awk '{print $2}'`
SQL_env=`cat tmp.log | grep -w Slave_SQL_Running | awk '{print $2}'`
#判断Slave_IO_Running Slave_SQL_Running状态
if [ "$IO_env" = "Yes" -a "$SQL_env" = "Yes" ]
then
echo -e " replication \033[32m$service --> $port \033[0m OK!"
else
echo -e " replication \033[31m$service --> $port \033[0m is abnormal!"
fi
done

/bin/rm -rf tmp.log

6、# crontab -l
0 17 * * * /bin/bash /opt/ops-tool/restart_mysql_slave.sh > /tmp/restart_mysql_$(date "+\%Y-\%m-\%d").log

Guess you like

Origin www.cnblogs.com/manger/p/12574899.html