mysql主從架構-主從正常切換,主庫宕機切換。

來源:互聯網
上載者:User

標籤:

 

 

 

MySQL主從切換手冊

Master-Slave架構

營運部

V1.0

 

 

 

 

 

 

 

 

 

2016年      05月    24   日

 

 

 

正常切換

  1. 檢查slave同步狀態

1)在master執行:show processlist;

顯示Master has sent all binlog to slave; waiting for binlog to be updated

 

2)在slave執行:show processlist;

顯示Slave has read all relay log; waiting for the slave I/O thread to update it

mysql> show slave status \G;

 

檢查IO及SQL線程是否正常,如果為NO表明同步不一致,需要重新將slave同步保持主從資料一致。

3)停止slave io線程

在slave執行:mysql> STOP SLAVE IO_THREAD

mysql> SHOW PROCESSLIST;

確保狀態為:has read all relay log

 

以上都執行完成後可以把slave提升為master:

4)提升slave為master

Stop slave;

Reset master;

Reset slave all; 在5.6.3版本之後

Reset slave; 在5.6.3版本之前

查看slave是否唯讀模式:show variables like ‘read_only‘;

唯讀模式需要修改my.cnf檔案,注釋read-only=1並重啟mysql服務。

或者不重啟使用命令關閉唯讀,但下次重啟後失效:set global read_only=off;

mysql> show master status \G;

備忘:reset slave all 命令會刪除從庫的 replication 參數,之後 show slave status\G 的資訊返回為空白。

5)將原來master變為slave

在新的master上建立同步處理的使用者:

grant replication slave on *.* [email protected]‘IP of slave‘ identified by ‘replpwd‘;

在新的slave上重設binlog:

Reset master;

change master to master_host=‘192.168.0.104‘, //Master 伺服器Ip

master_port=3306,

master_user=‘repl‘,

master_password=’replpwd’,

master_log_file=‘master-bin.000001‘,//Master伺服器產生的日誌

master_log_pos=?;//master binlog pos

以上最後兩步可以在master執行:show master status

 

啟動slave:start slave; 並查看slave狀態:show slave status\G;

           異常切換

主機故障或者宕機:

1)   在salve執行:

stop slave;

reset master;

查看是否唯讀模式:show variables like ‘read_only‘;

唯讀模式需要修改my.cnf檔案,注釋read-only=1並重啟mysql服務。

或者不重啟使用命令關閉唯讀,但下次重啟後失效:set global read_only=off;

查看show slave status \G;

查看show master status \G;

將從庫IP地址改為主庫IP地址,測試應用串連是否正常。

 

 

<完>

 

mysql主從架構-主從正常切換,主庫宕機切換。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.