一主两从的环境,如果主库挂了,如何选举一个从库作为主库?

发布时间:2020-07-01 10:30:06 作者:Darren_Chen
来源:网络 阅读:1067


一主两从的环境,如果主库挂了,如何选举一个从库作为主库?

如图:



一主两从的环境,如果主库挂了,如何选举一个从库作为主库?

如果M挂了,怎么从S1和S2中选举一个从库作为主库?


传统复制的解决方法

(1)查看从库状态:

S1:show slave status;

S2:show slave status;


root@localhost [(none)]>show slave status\G

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

               Slave_IO_State: Reconnecting after a failed master event read

                  Master_Host: 192.168.91.22

                  Master_User: repl

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000006

          Read_Master_Log_Pos: 6227

               Relay_Log_File: relay-bin.000004

                Relay_Log_Pos: 414

        Relay_Master_Log_File: mysql-bin.000006

             Slave_IO_Running: Connecting

            Slave_SQL_Running: Yes

              Replicate_Do_DB:

          Replicate_Ignore_DB:

           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: 6227

              Relay_Log_Space: 875

              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   --主库服务停止后由0变成null,所以这个值不能作为判断从库是否同步完成的标准

Master_SSL_Verify_Server_Cert: No

                Last_IO_Errno: 2003

                Last_IO_Error: error reconnecting to master 'repl@192.168.91.22:3306' - retry-time: 60  retries: 12

               Last_SQL_Errno: 0

               Last_SQL_Error:

  Replicate_Ignore_Server_Ids:

             Master_Server_Id: 330622

                  Master_UUID: 83373570-fe03-11e6-bb0a-000c29c1b8a9

             Master_Info_File: mysql.slave_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: 170415 23:08:25

     Last_SQL_Error_Timestamp:

               Master_SSL_Crl:

           Master_SSL_Crlpath:

           Retrieved_Gtid_Set:

            Executed_Gtid_Set: 83373570-fe03-11e6-bb0a-000c29c1b8a9:1-33,

b30cdc47-216a-11e7-95a8-000c29565380:1-3

                Auto_Position: 1

         Replicate_Rewrite_DB:

                 Channel_Name:

           Master_TLS_Version





(2)判断每个slave是不是同步完成:

io_thread读到主库的binlog日志和位置:

Master_Log_File: mysql-bin.000006

Read_Master_Log_Pos: 6227


sql_thread执行到哪个relay-log和位置:

Relay_Master_Log_File: mysql-bin.000006

Exec_Master_Log_Pos: 6227


当Master_Log_File = Relay_Master_Log_File &&  Read_Master_Log_Pos = Exec_Master_Log_Pos 表示从库与主库同步完成。


如果Master_Log_File = Relay_Master_Log_File,但是Read_Master_Log_Pos > Exec_Master_Log_Pos,并且sql_thread的状态是 Connecting,表示relay-log还没有重放完成,大概等待2-5s也就会同步完成。


(3)比较两个从库的同步情况:

当S1和S2分别同步完成,谁靠前,谁当主。多数情况下S1和S2是一样的.


当S1.Relay_Master_Log_File=S2.Relay_Master_Log_File 但 S1.Exec_Master_Log_Pos > S2.Exec_Master_Log_Pos,则表示S1同步靠前,选择S1作为新主。


或者比较:


当S1.Master_Log_File = S2.Master_Log_File 但 S1.Read_Master_Log_Pos > S2.Read_Master_Log_Pos,则表示S1同步靠前,选择S1作为新主。


(4)S1和S2数据不一致怎么办?

如果万一出现S1靠前,S2数据比S1数据少,那么把S1作为新的主之后,业务读写都先放在S1上,然后通过pt-table-checksum和pt-table-sync工具修复S2的数据,再用S2分担业务。


GTID复制的解决方法


(1) 判断每个slave是不是同步完成:

Retrieved_Gtid_Set: 83373570-fe03-11e6-bb0a-000c29c1b8a9:22-28

Executed_Gtid_Set: 83373570-fe03-11e6-bb0a-000c29c1b8a9:1-28,

当Retrieved_Gtid_Set = Executed_Gtid_Set (即28=28)表示从库已经和主库完成同步。


(2)选举一个从库作为主库:

如果S1. Executed_Gtid_Set = S2. Executed_Gtid_Set,随机选择一个作为主;

如果S1. Executed_Gtid_Set> S2. Executed_Gtid_Set,则选举S1作为主,S2可以直接change master to到S1,作为S1的从库


损坏的主库怎么办?

(1)把以前的主库重新change master to 新主,然后主从一致性校验,数据修复。

(2)如果是原来的主库数据损坏,需要重新作为从库加到新主上面


如何暂时停止主库写操作?

(1)改密码,不能影响已有的连接,记得要把已有的连接都kill掉。

(2)flush table with read lock

(3)开启参数super_read_only=on

(4)通过防火墙把3306端口封住


总结:

一主两从的环境,如果主库挂了,如何选举一个从库作为主库的切换过程(整个过程快的话大概1-5秒):

(1)修改主库密码,断开所有连接

(2)判断S1和S2同步情况

(3)选举新库

(4)写流量放在新主上

推荐阅读:
  1. mysql5.7从库提升为主库简析
  2. mysql两主一从配置

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

环境 一主 两从

上一篇:W3C盒子模型和IE盒子模型的不同点

下一篇:softmax函数用法及案例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》