一主兩從的環(huán)境,如果主庫(kù)掛了,如何選舉一個(gè)從庫(kù)作為主庫(kù)?

一主兩從的環(huán)境,如果主庫(kù)掛了,如何選舉一個(gè)從庫(kù)作為主庫(kù)?

高臺(tái)ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)的ssl證書(shū)銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書(shū)合作)期待與您的合作!

如圖:

一主兩從的環(huán)境,如果主庫(kù)掛了,如何選舉一個(gè)從庫(kù)作為主庫(kù)?

如果M掛了,怎么從S1和S2中選舉一個(gè)從庫(kù)作為主庫(kù)?


傳統(tǒng)復(fù)制的解決方法

(1)查看從庫(kù)狀態(tài):

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   --主庫(kù)服務(wù)停止后由0變成null,所以這個(gè)值不能作為判斷從庫(kù)是否同步完成的標(biāo)準(zhǔn)

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)判斷每個(gè)slave是不是同步完成:

io_thread讀到主庫(kù)的binlog日志和位置:

Master_Log_File: mysql-bin.000006

Read_Master_Log_Pos: 6227

sql_thread執(zhí)行到哪個(gè)relay-log和位置:

Relay_Master_Log_File: mysql-bin.000006

Exec_Master_Log_Pos: 6227

當(dāng)Master_Log_File = Relay_Master_Log_File &&  Read_Master_Log_Pos = Exec_Master_Log_Pos 表示從庫(kù)與主庫(kù)同步完成。

如果Master_Log_File = Relay_Master_Log_File,但是Read_Master_Log_Pos > Exec_Master_Log_Pos,并且sql_thread的狀態(tài)是 Connecting,表示relay-log還沒(méi)有重放完成,大概等待2-5s也就會(huì)同步完成。


(3)比較兩個(gè)從庫(kù)的同步情況:

當(dāng)S1和S2分別同步完成,誰(shuí)靠前,誰(shuí)當(dāng)主。多數(shù)情況下S1和S2是一樣的.

當(dāng)S1.Relay_Master_Log_File=S2.Relay_Master_Log_File 但 S1.Exec_Master_Log_Pos > S2.Exec_Master_Log_Pos,則表示S1同步靠前,選擇S1作為新主。

或者比較:

當(dāng)S1.Master_Log_File = S2.Master_Log_File 但 S1.Read_Master_Log_Pos > S2.Read_Master_Log_Pos,則表示S1同步靠前,選擇S1作為新主。


(4)S1和S2數(shù)據(jù)不一致怎么辦?

如果萬(wàn)一出現(xiàn)S1靠前,S2數(shù)據(jù)比S1數(shù)據(jù)少,那么把S1作為新的主之后,業(yè)務(wù)讀寫(xiě)都先放在S1上,然后通過(guò)pt-table-checksum和pt-table-sync工具修復(fù)S2的數(shù)據(jù),再用S2分擔(dān)業(yè)務(wù)。

GTID復(fù)制的解決方法


(1) 判斷每個(gè)slave是不是同步完成:

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

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

當(dāng)Retrieved_Gtid_Set = Executed_Gtid_Set (即28=28)表示從庫(kù)已經(jīng)和主庫(kù)完成同步。

(2)選舉一個(gè)從庫(kù)作為主庫(kù):

如果S1. Executed_Gtid_Set = S2. Executed_Gtid_Set,隨機(jī)選擇一個(gè)作為主;

如果S1. Executed_Gtid_Set> S2. Executed_Gtid_Set,則選舉S1作為主,S2可以直接change master to到S1,作為S1的從庫(kù)

損壞的主庫(kù)怎么辦?

(1)把以前的主庫(kù)重新change master to 新主,然后主從一致性校驗(yàn),數(shù)據(jù)修復(fù)。

(2)如果是原來(lái)的主庫(kù)數(shù)據(jù)損壞,需要重新作為從庫(kù)加到新主上面

如何暫時(shí)停止主庫(kù)寫(xiě)操作?

(1)改密碼,不能影響已有的連接,記得要把已有的連接都kill掉。

(2)flush table with read lock

(3)開(kāi)啟參數(shù)super_read_only=on

(4)通過(guò)防火墻把3306端口封住

總結(jié):

一主兩從的環(huán)境,如果主庫(kù)掛了,如何選舉一個(gè)從庫(kù)作為主庫(kù)的切換過(guò)程(整個(gè)過(guò)程快的話大概1-5秒):

(1)修改主庫(kù)密碼,斷開(kāi)所有連接

(2)判斷S1和S2同步情況

(3)選舉新庫(kù)

(4)寫(xiě)流量放在新主上

網(wǎng)站名稱:一主兩從的環(huán)境,如果主庫(kù)掛了,如何選舉一個(gè)從庫(kù)作為主庫(kù)?
文章網(wǎng)址:http://bm7419.com/article16/jciigg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、App開(kāi)發(fā)軟件開(kāi)發(fā)、網(wǎng)站營(yíng)銷商城網(wǎng)站、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名