日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/15 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一主两从的环境,如果主库挂了,如何选举一个从库作为主库? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

如圖:



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



本文轉自 Darren_Chen 51CTO博客,原文鏈接:http://blog.51cto.com/darrenmemos/1922183,如需轉載請自行聯系原作者

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的一主两从的环境,如果主库挂了,如何选举一个从库作为主库?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。