[mysql]-shell 检测myql主从同步的主库状态并自动升级slave

都是没有上过生产实际测试的- -.只是自己操作玩玩的. 

假设现在主库已经宕机 出现问题.按照我的设想.现在先dump一下98的全库,模拟晚上24点的全备,执行完备份之后再进行一些操作,使binlog的偏移量发生变化.测试用从库的全量备份 可不可以再98从库升级为主库之后恢复到和98一样的偏移量的位置.

 

 

需要一个虚拟ip  配置在主上.如果需要读写分离 可以一个虚拟ip 在主上负责写  一个虚拟ip 在从上负责读.

 

 

 

50 23 * * *  sh /opt/dump_mysql.sh >> /opt/mysql_dump/dump.log

mkdir -p /opt/mysql_dump

 

 

#!/bin/bash

. /etc/init.d/functions

 

#全量备份每天11点50运行 最好是锁表备份

#获取今天的日期

log=$(date -d "now" +%Y-%m-%d)

echo ${log}

 

#获取昨天的日期

log2=$(date -d "yesterday" +%Y-%m-%d)

echo ${log2}

 

#主库锁表

lockCmd="flush tables with read lock; "

mysql  -uroot -pHangzhou@123 -e "${lockCmd}"

 

data_dir=/opt/mysql_dump

Now=$(date -d "now" +%Y-%m-%d)

echo $Now

SQL=$data_dir/$Now.sql

mysqldump  -E   -uroot -pHangzhou@123  --all-databases  > $SQL

echo $SECONDS

 

#获取pos

masterSqlname=root

masterSqlpass=Hangzhou@123

 

 

 

#查看master状态

statusCmd="mysql  -u${masterSqlname} -p${masterSqlpass} -e \"show master status;\""

echo "status cmd:" ${statusCmd}

mysql  -u${masterSqlname} -p${masterSqlpass} -e "show master status;" >/opt/mysql_dump/status_$log.txt

if [ $? -eq 0 ]; then

  echo "exec: success"

  [ $? -eq 0  ] && action "查看 master 状态成功" /bin/true

else

  echo "exec: failure"

fi

 

#查看master状态 获取file 以及position的信息

file=`tac /opt/mysql_dump/status_$log.txt|head -n 1 |awk '{print $1}'`

 

pos=`tac /opt/mysql_dump/status_$log.txt |head -n 1 |awk '{print $2}'`

 

if [ -n "$file" ] && [ -n "$pos" ];then

 echo " $file $pos"

 [ $? -eq 0  ] && action "获取 master status 参数成功" /bin/true

else

 echo "put in please"

fi

#表锁释放

unlockCmd=" unlock tables; "

mysql  -uroot -pHangzhou@123 -e "${unlockCmd}"

 

 

#find /opt/mysql_dump  -mtime +7 |xargs rm -rf

 

创建初始的主从同步的过程省略,初始是49是主 98是从,并且98是用154这个偏移量生成的. 下面显示结果已经同步,首次主从复制成功.

 

root@localhost [(none)] [Sat Jul 27 13:20:48 2019]>change master to master_host='172.20.19.49',master_user='repl',master_password='ht00@pass',master_port=3306,master_log_file='mysql-bin.000015',master_log_pos=154,master_connect_retry=30;

Query OK, 0 rows affected, 2 warnings (0.22 sec)

 

root@localhost [(none)] [Sat Jul 27 13:21:54 2019]>start slave;

Query OK, 0 rows affected (0.03 sec)

 

root@localhost [(none)] [Sat Jul 27 13:22:02 2019]>show slave status\G;

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 172.20.19.49

                  Master_User: repl

                  Master_Port: 3306

                Connect_Retry: 30

              Master_Log_File: mysql-bin.000015

          Read_Master_Log_Pos: 316

               Relay_Log_File: zookeeper-2-relay-bin.000002

                Relay_Log_Pos: 482

        Relay_Master_Log_File: mysql-bin.000015

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB:

          Replicate_Ignore_DB: mysql,information_schema,performance_schema

           Replicate_Do_Table:

       Replicate_Ignore_Table:

      Replicate_Wild_Do_Table:

  Replicate_Wild_Ignore_Table:

                   Last_Errno: 0

                   Last_Error:

                 Skip_Counter: 0

          Exec_Master_Log_Pos: 316

              Relay_Log_Space: 695

              Until_Condition: None

               Until_Log_File:

                Until_Log_Pos: 0

           Master_SSL_Allowed: No

           Master_SSL_CA_File:

           Master_SSL_CA_Path:

              Master_SSL_Cert:

            Master_SSL_Cipher:

               Master_SSL_Key:

        Seconds_Behind_Master: 0

Master_SSL_Verify_Server_Cert: No

                Last_IO_Errno: 0

                Last_IO_Error:

               Last_SQL_Errno: 0

               Last_SQL_Error:

  Replicate_Ignore_Server_Ids:

             Master_Server_Id: 49

                  Master_UUID: 9346321d-a2ec-11e9-87ef-080027adcbf9

             Master_Info_File: /usr/local/mysql/data/master.info

                    SQL_Delay: 0

          SQL_Remaining_Delay: NULL

      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

           Master_Retry_Count: 86400

                  Master_Bind:

      Last_IO_Error_Timestamp:

     Last_SQL_Error_Timestamp:

               Master_SSL_Crl:

           Master_SSL_Crlpath:

           Retrieved_Gtid_Set:

            Executed_Gtid_Set:

                Auto_Position: 0

         Replicate_Rewrite_DB:

                 Channel_Name:

           Master_TLS_Version:

1 row in set (0.00 sec)

 

ERROR:

No query specified

 

root@localhost [(none)] [Sat Jul 27 13:22:14 2019]>show master status;

+------------------+----------+--------------+------------------+-------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000014 |  2493553 |              |                  |                   |

+------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

 

root@localhost [(none)] [Sat Jul 27 13:22:45 2019]>

 

通过上面在98的show master status;

看到mysql-bin.000014 |  2493553 这两个信息

[mysql]-shell 检测myql主从同步的主库状态并自动升级slave_第1张图片

 

 

#定时任务就不执行了直接手动在98上执行以下备份脚本

[mysql]-shell 检测myql主从同步的主库状态并自动升级slave_第2张图片


 

在从上编写一个定时任务脚本/opt/check.sh  检测vip是否飘到了从机98上

#!/bin/bash

# Author:

# Email:

# Description: 检测vip

# Notes:只在98上执行

 

#调试模式

#set -x

 

if [  `ip addr  |grep 172.20.19.95 | wc -l` -eq 1 ];then

        echo 95 here

 

 

 

 

fi

 

 

 

sleep 5

if [  `ip addr  |grep 172.20.19.95 | wc -l` -eq 1 ];then

        echo 95 here

        echo 执行切换主从操作

        bash /opt/change.sh #测试的时候我注释了 手动执行了

        exit

fi

        echo 95 not here

        echo 95已经飘回49

       

 

这边为了测试第二天出现故障 使用第一天晚上的全备进行恢复.所以将

sql名称和status的名称改成前一天的了

[mysql]-shell 检测myql主从同步的主库状态并自动升级slave_第3张图片

 

在98的/opt下编写change.sh 用于变更主从并从新建立.

#去除主从关系

 

mysql  -uroot -pHangzhou@123 -e "

stop slave;

reset slave;

change master to master_host=' ';

show slave status\G;

quit"

 

echo 1

 

#获取昨天的日期

log2=$(date -d "yesterday" +%Y-%m-%d)

echo ${log2}

 

data_dir=/opt/mysql_dump

SQL=$data_dir/$log2.sql

echo $SQL

 

echo 2

 

#查看master状态 获取file 以及position的信息

file=`tac /opt/mysql_dump/status_$log2.txt|head -n 1 |awk '{print $1}'`

 

pos=`tac /opt/mysql_dump/status_$log2.txt |head -n 1 |awk '{print $2}'`

 

echo $file $pos

echo 3

#

mysql -uroot -pHangzhou@123 -h172.20.19.49 < $SQL

echo 4

 

mysql  -uroot -pHangzhou@123 -e "

CREATE USER repl_user;

GRANT REPLICATION SLAVE On *.* TO repl_user IDENTIFIED BY 'ht00@pass';

flush privileges;

quit"

 

echo 5

 

changeCmd="change master to MASTER_HOST='172.20.19.98',MASTER_PORT=3306,MASTER_USER='repl_user',MASTER_PASSWORD='ht00@pass',MASTER_LOG_FILE='${file}',MASTER_LOG_POS=${pos};"

echo $changeCmd

mysql  -uroot -pHangzhou@123 -h172.20.19.49 -e "${changeCmd}"

 

echo 6

 

mysql  -uroot -pHangzhou@123 -h172.20.19.49 -e "

stop slave;

start slave;

show slave status\G;

quit"

 

 

echo 7

 

 

 

 

模拟vip 漂移 发生了故障. 这主从两台机子date时间差1分钟  请忽略吧 哈哈.尴尬

[mysql]-shell 检测myql主从同步的主库状态并自动升级slave_第4张图片

vip 现在在98上了 如果定时任务执行了检测的话

就会执行脚本了..

[mysql]-shell 检测myql主从同步的主库状态并自动升级slave_第5张图片

执行check.sh

[mysql]-shell 检测myql主从同步的主库状态并自动升级slave_第6张图片

执行change.sh

[root@zookeeper-2 opt]# sh change.sh

Logging to file '/usr/local/mysql/data/mysql_history.log'

mysql: [Warning] Using a password on the command line interface can be insecure.

*************************** 1. row ***************************

               Slave_IO_State:

                  Master_Host: 

                  Master_User: repl

                  Master_Port: 3306

                Connect_Retry: 30

              Master_Log_File:

          Read_Master_Log_Pos: 4

               Relay_Log_File: zookeeper-2-relay-bin.000001

                Relay_Log_Pos: 4

        Relay_Master_Log_File:

             Slave_IO_Running: No

            Slave_SQL_Running: No

              Replicate_Do_DB:

          Replicate_Ignore_DB: mysql,information_schema,performance_schema

           Replicate_Do_Table:

       Replicate_Ignore_Table:

      Replicate_Wild_Do_Table:

  Replicate_Wild_Ignore_Table:

                   Last_Errno: 0

                   Last_Error:

                 Skip_Counter: 0

          Exec_Master_Log_Pos: 0

              Relay_Log_Space: 154

              Until_Condition: None

               Until_Log_File:

                Until_Log_Pos: 0

           Master_SSL_Allowed: No

           Master_SSL_CA_File:

           Master_SSL_CA_Path:

              Master_SSL_Cert:

            Master_SSL_Cipher:

               Master_SSL_Key:

        Seconds_Behind_Master: NULL

Master_SSL_Verify_Server_Cert: No

                Last_IO_Errno: 0

                Last_IO_Error:

               Last_SQL_Errno: 0

               Last_SQL_Error:

  Replicate_Ignore_Server_Ids:

             Master_Server_Id: 0

                  Master_UUID:

             Master_Info_File: /usr/local/mysql/data/master.info

                    SQL_Delay: 0

          SQL_Remaining_Delay: NULL

      Slave_SQL_Running_State:

           Master_Retry_Count: 86400

                  Master_Bind:

      Last_IO_Error_Timestamp:

     Last_SQL_Error_Timestamp:

               Master_SSL_Crl:

           Master_SSL_Crlpath:

           Retrieved_Gtid_Set:

            Executed_Gtid_Set:

                Auto_Position: 0

         Replicate_Rewrite_DB:

                 Channel_Name:

           Master_TLS_Version:

2019-07-26

/opt/mysql_dump/2019-07-26.sql

mysql-bin.000014 2493553

Logging to file '/usr/local/mysql/data/mysql_history.log'

mysql: [Warning] Using a password on the command line interface can be insecure.

mysql: [Warning] Using a password on the command line interface can be insecure.

change master to MASTER_HOST='172.20.19.98',MASTER_PORT=3306,MASTER_USER='repl_user',MASTER_PASSWORD='ht00@pass',MASTER_LOG_FILE='mysql-bin.000014',MASTER_LOG_POS=2493553;

Logging to file '/usr/local/mysql/data/mysql_history.log'

mysql: [Warning] Using a password on the command line interface can be insecure.

Logging to file '/usr/local/mysql/data/mysql_history.log'

mysql: [Warning] Using a password on the command line interface can be insecure.

*************************** 1. row ***************************

               Slave_IO_State: Connecting to master

                  Master_Host: 172.20.19.98

                  Master_User: repl_user

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000014

          Read_Master_Log_Pos: 2493553

               Relay_Log_File: zookeeper-1-relay-bin.000001

                Relay_Log_Pos: 4

        Relay_Master_Log_File: mysql-bin.000014

             Slave_IO_Running: Connecting

            Slave_SQL_Running: Yes

              Replicate_Do_DB:

          Replicate_Ignore_DB: mysql,information_schema,performance_schema

           Replicate_Do_Table:

       Replicate_Ignore_Table:

      Replicate_Wild_Do_Table:

  Replicate_Wild_Ignore_Table:

                   Last_Errno: 0

                   Last_Error:

                 Skip_Counter: 0

          Exec_Master_Log_Pos: 2493553

              Relay_Log_Space: 154

              Until_Condition: None

               Until_Log_File:

                Until_Log_Pos: 0

           Master_SSL_Allowed: No

           Master_SSL_CA_File:

           Master_SSL_CA_Path:

              Master_SSL_Cert:

            Master_SSL_Cipher:

               Master_SSL_Key:

        Seconds_Behind_Master: NULL

Master_SSL_Verify_Server_Cert: No

                Last_IO_Errno: 0

                Last_IO_Error:

               Last_SQL_Errno: 0

               Last_SQL_Error:

  Replicate_Ignore_Server_Ids:

             Master_Server_Id: 0

                  Master_UUID:

             Master_Info_File: /usr/local/mysql/data/master.info

                    SQL_Delay: 0

          SQL_Remaining_Delay: NULL

      Slave_SQL_Running_State: Waiting for the next event in relay log

           Master_Retry_Count: 86400

                  Master_Bind:

      Last_IO_Error_Timestamp:

     Last_SQL_Error_Timestamp:

               Master_SSL_Crl:

           Master_SSL_Crlpath:

           Retrieved_Gtid_Set:

            Executed_Gtid_Set:

                Auto_Position: 0

         Replicate_Rewrite_DB:

                 Channel_Name:

           Master_TLS_Version:

 [root@zookeeper-2 opt]# ll

总用量 12

-rw-r--r--. 1 root root 1063 7月  27 14:57 change.sh

-rw-r--r--. 1 root root  471 7月  27 15:14 check.sh

-rw-r--r--. 1 root root 1485 7月  27 14:16 dump_mysql.sh

drwxr-xr-x. 2 root root   57 7月  27 14:39 mysql_dump

 [root@zookeeper-2 opt]# mysql -uroot -pHangzhou123 -h172.20.19.49

Logging to file '/usr/local/mysql/data/mysql_history.log'

mysql: [Warning] Using a password on the command line interface can be insecure.

ERROR 1045 (28000): Access denied for user 'root'@'172.20.19.98' (using password: YES)

[root@zookeeper-2 opt]# mysql -uroot -pHangzhou@123 -h172.20.19.49

Logging to file '/usr/local/mysql/data/mysql_history.log'

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 21

Server version: 5.7.12-log MySQL Community Server (GPL)

 

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

[email protected] [(none)] [Sat Jul 27 15:20:57 2019]>show slave status\G;

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 172.20.19.98

                  Master_User: repl_user

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000014

          Read_Master_Log_Pos: 2494188

               Relay_Log_File: zookeeper-1-relay-bin.000002

                Relay_Log_Pos: 955

        Relay_Master_Log_File: mysql-bin.000014

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB:

          Replicate_Ignore_DB: mysql,information_schema,performance_schema

           Replicate_Do_Table:

       Replicate_Ignore_Table:

      Replicate_Wild_Do_Table:

  Replicate_Wild_Ignore_Table:

                   Last_Errno: 0

                   Last_Error:

                 Skip_Counter: 0

          Exec_Master_Log_Pos: 2494188

              Relay_Log_Space: 1168

              Until_Condition: None

               Until_Log_File:

                Until_Log_Pos: 0

           Master_SSL_Allowed: No

           Master_SSL_CA_File:

           Master_SSL_CA_Path:

              Master_SSL_Cert:

            Master_SSL_Cipher:

               Master_SSL_Key:

        Seconds_Behind_Master: 0

Master_SSL_Verify_Server_Cert: No

                Last_IO_Errno: 0

                Last_IO_Error:

               Last_SQL_Errno: 0

               Last_SQL_Error:

  Replicate_Ignore_Server_Ids:

             Master_Server_Id: 98

                  Master_UUID: 98f6c429-a2ec-11e9-8664-0800279939e8

             Master_Info_File: /usr/local/mysql/data/master.info

                    SQL_Delay: 0

          SQL_Remaining_Delay: NULL

      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

           Master_Retry_Count: 86400

                  Master_Bind:

      Last_IO_Error_Timestamp:

     Last_SQL_Error_Timestamp:

               Master_SSL_Crl:

           Master_SSL_Crlpath:

           Retrieved_Gtid_Set:

            Executed_Gtid_Set:

                Auto_Position: 0

         Replicate_Rewrite_DB:

                 Channel_Name:

           Master_TLS_Version:

1 row in set (0.00 sec)

 

ERROR:

No query specified

 

[email protected] [(none)] [Sat Jul 27 15:21:02 2019]>

 

 

[mysql]-shell 检测myql主从同步的主库状态并自动升级slave_第7张图片

 

 

问题1.我这个检测第二遍vip是否飘回去是否有必要,毕竟如果当时sleep的时候有数据写入,那估计没有重新更换主从,原主从恢复的时候 也报错了.

问题2.如果是网络问题的话.我脚本重新拉起一个新从会报错,只能等服务器启动了之后再手动执行脚本

问题3 定时检测vip的脚本 需要修改一下.让脚本只执行一次.否则主宕机的话 会一直去执行.比如执行的时候创建一个文件里面是日期 每次执行前判断一下 上次执行时间是不是今天?

 

 

你可能感兴趣的:(mysql)