1. 程式人生 > 資料庫 >mysql 一主兩從的環境,如果主庫掛了,如何選舉一個從庫作為主庫?

mysql 一主兩從的環境,如果主庫掛了,如何選舉一個從庫作為主庫?

一主兩從的環境,如果主庫掛了,如何選舉一個從庫作為主庫?

如圖:

wKiom1kLIVLCTYSGAAASzvVCLKg270.png-wh_50

如果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 '[email protected]: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資料不一致怎麼辦?

  1. 如果萬一出現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)寫流量放在新主上