4503.com,你或然感兴趣的篇章:

  • Shell脚本中推行sql语句操作mysql的五种格局
  • shell脚本连接、读写、操作mysql数据库实例
  • MySQL的一些效能实用的Linux shell脚本分享

指标:定期监察和控制MySQL主从数据库是还是不是同步,假如区别台,记录故障时间,并实施命令使主从…

一、创造脚本文件

CentOS下编写制定shell脚本来监察和控制MySQL主从复制的科目,

指标:定时监控MySQL主从数据库是还是不是同步,假如不一同,记录故障时间,并实施命令使基本复苏同步状态

一、制造脚本文件

vi /home/crontab/check_mysql_slave.sh  #编辑,添加下面代码

#!/bin/sh

# check_mysql_slave status

ip=eth0 #网卡名称

mysql_binfile=/usr/local/mysql/bin/mysql

mysql_user=root #MySQL数据库账号

mysql_pass=123456 #密码

mysql_sockfile=/tmp/mysql.sock

datetime=`date +"%Y-%m-%d/%H:%M:%S"`  #获取当前时间

mysql_slave_logfile=/home/logs/check_mysql_slave.log  #日志文件路径,必须提前创建好

slave_ip=`ifconfig $ip|grep "inet addr" | awk -F[:" "]+ '{print $4}'`

status=$($mysql_binfile -u$mysql_user -p$mysql_pass -S $mysql_sockfile -e "show slave status\G" | grep -i "running")

Slave_IO_Running=`echo $status | grep Slave_IO_Running | awk ' {print $2}'`

Slave_SQL_Running=`echo $status | grep Slave_SQL_Running | awk '{print $2}'`

if [ "$Slave_IO_Running" = "Yes" -a "$Slave_SQL_Running" = "Yes" ]

then echo "Slave is Running!"

else

echo " $datetime $slave_ip Slave is not running!" >> $mysql_slave_logfile

$mysql_binfile -u$mysql_user -p$mysql_pass -S $mysql_sockfile -e "SLAVE STOP;"

$mysql_binfile -u$mysql_user -p$mysql_pass -S $mysql_sockfile -e "SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1;"

$mysql_binfile -u$mysql_user -p$mysql_pass -S $mysql_sockfile -e "SLAVE START;"

$mysql_binfile -u$mysql_user -p$mysql_pass -S $mysql_sockfile -e "EXIT"

fi

:wq! #保存退出

chmod +x /home/crontab/check_mysql_slave.sh #添加脚本执行权限

二、增多职务布置,修改/etc/crontab

vi /etc/crontab #在最后一行添加

*/10 * * * * root /home/crontab/check_mysql_slave.sh #表示每10分钟执行一次

:wq! #保存退出

三、重新启航crond使设置生效

/etc/rc.d/init.d/crond restart  #yum install -y vixie-cron安装计划任务,某些系统上可能没有预装

chkconfig crond on #设为开机启动

service crond start #启动

可以依据日志文件/home/logs/check_mysql_slave.log查看MySQL主从同步状态

PS:接下去这么些剧本扩张了“当开采1头出现不能够同步的时候”会自行提取主库的file号,以及pos,进行联合主库,脚本内容如下:
 

#!/bin/sh
#set -x
#file is slave_repl.sh
#Author by Kevin
#date is 2011-11-13

mstool="/usr/local/mysql-3307/bin/mysql -h 192.168.1.106 -uroot -pw!zl7POg27 -P 3307"
sltool="/usr/local/mysql-3307/bin/mysql -h 192.168.1.107 -uroot -pw!zl7POg27 -P 3307"
declare -a slave_stat
slave_stat=($($sltool -e "show slave status\G"|grep Running |awk '{print $2}'))
if [ "${slave_stat[0]}" = "Yes" -a "${slave_stat[1]}" = "Yes" ]
   then
   echo "OK slave is running"
   exit 0
else
   echo "Critical slave is error"
   echo
   echo "*********************************************************"
   echo "Now Starting replication with Master Mysql!"
    file=`$mstool -e "show master status\G"|grep "File"|awk '{print $2}'` 
    pos=`$mstool -e "show master status\G"|grep "Pos"|awk '{print $2}'` 
    $sltool -e "slave stop;change master to master_host='192.168.1.106',master_port=3307,master_user='repl',master_password='w!zl7POg27',master_log_file='$file',master_log_pos=$pos;slave start;"
    sleep 3
    $sltool -e "show slave status\G;"|grep Running
  echo
  echo "Now Replication is Finished!"
  echo
  echo "**********************************************************"
    exit 2
fi

 
运维后效果,如下:
 

# ./slave_stop3307.sh 
*******************************
Now stop Slave Replication!
      Slave_IO_Running: No
     Slave_SQL_Running: No
*******************************
# ./slave_repl3307.sh 
Critical slave is error
*********************************************************
Now Starting replication with Master Mysql!
      Slave_IO_Running: Yes
     Slave_SQL_Running: Yes
Now Replication is Finished!
**********************************************************

 
运作后效果,如下:
 

# ./slave_stop3307.sh 
*******************************
Now stop Slave Replication!
      Slave_IO_Running: No
     Slave_SQL_Running: No
*******************************
# ./slave_repl3307.sh 
Critical slave is error
*********************************************************
Now Starting replication with Master Mysql!
      Slave_IO_Running: Yes
     Slave_SQL_Running: Yes
Now Replication is Finished!
**********************************************************
#!/bin/sh
#set -x
#file is slave_repl.sh
#Author by Kevin
#date is 2011-11-13

mstool="/usr/local/mysql-3307/bin/mysql -h 192.168.1.106 -uroot -pw!zl7POg27 -P 3307"
sltool="/usr/local/mysql-3307/bin/mysql -h 192.168.1.107 -uroot -pw!zl7POg27 -P 3307"
declare -a slave_stat
slave_stat=($($sltool -e "show slave status\G"|grep Running |awk '{print $2}'))
if [ "${slave_stat[0]}" = "Yes" -a "${slave_stat[1]}" = "Yes" ]
   then
   echo "OK slave is running"
   exit 0
else
   echo "Critical slave is error"
   echo
   echo "*********************************************************"
   echo "Now Starting replication with Master Mysql!"
    file=`$mstool -e "show master status\G"|grep "File"|awk '{print $2}'` 
    pos=`$mstool -e "show master status\G"|grep "Pos"|awk '{print $2}'` 
    $sltool -e "slave stop;change master to master_host='192.168.1.106',master_port=3307,master_user='repl',master_password='w!zl7POg27',master_log_file='$file',master_log_pos=$pos;slave start;"
    sleep 3
    $sltool -e "show slave status\G;"|grep Running
  echo
  echo "Now Replication is Finished!"
  echo
  echo "**********************************************************"
    exit 2
fi

指标:定期监察和控制MySQL主从数据库是不是同步,若是不一致台,记录故障时间,并推行命令使大旨恢复生机同步状态

vi /etc/crontab #在最后一行添加

*/10 * * * * root /home/crontab/check_mysql_slave.sh #表示每10分钟执行一次

:wq! #保存退出

PS:接下去那个剧本扩展了“当发掘一块出现十分小概共同的时候”会活动提取主库的file号,以及pos,举行同步主库,脚本内容如下: