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

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

高台ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!

如图:

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

如果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)写流量放在新主上


分享题目:一主两从的环境,如果主库挂了,如何选举一个从库作为主库?
路径分享:http://bzwzjz.com/article/jciigg.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 手机网站制作 成都企业网站设计 网站设计制作 宜宾网站设计 成都网站制作 网站建设费用 手机网站设计 成都网站制作 成都网站建设 营销网站建设 达州网站设计 网站制作报价 高端网站设计推广 营销网站建设 成都营销网站制作 成都网站制作 网站制作 网站制作 成都网站设计 网站设计 营销网站建设 网站建设推广