mysql 单机多实例重启数据库服务

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"

#停止数据库
/opt/ops-tool/stop_all.sh

#启动数据库
/opt/ops-tool/start_all.sh


#启动从库
/opt/ops-tool/start_slave.sh


#检查主从状态
/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
 
#停止数据库
cat /opt/ops-tool/db.txt| while read line
do
  pwd='xxx'
  port=$(echo $line |awk '{print $2}')
  /usr/local/mysql/bin/mysqladmin -h127.0.0.1 -P$port -umtdba_admin -p$pwd 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 start_slave.sh
#!/bin/sh
export PATH=$PATH:/usr/local/mysql/bin
cat /opt/ops-tool/db.txt| while read line
do
  pwd='xxx'
  port=$(echo $line |awk '{print $2}')
  /usr/local/mysql/bin/mysql  -umtdba_admin -p$pwd -h127.0.0.1 -P$port -e"start slave ;"  &>/dev/null
  sleep 5
done

6、# 

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 -umtdba_admin -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

 

配置crontab任务计划

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

posted @ 2020-03-26 15:47  Manger  阅读(558)  评论(0编辑  收藏  举报