MySQL主從恢復(在線不停機)

前提:主從同步只同步該庫testdb,否則記錄的Binlog是整個機器,需導出整個機器的數據

1、模擬測試從機備份時長

shell>time mysqldump --databases testdb -uroot -p12345 -l --master-data=2 -h192.168.0.11 >/tmp/testdb.sql

測試結果:1G大約在22~24s 這也是鎖定庫的時長

-l :鎖定整個庫

--master-data=2 :記錄開始備份時的binlog文件名和binlog位置

--databases: 指定需要備份的庫名

2、在主機備份數據

shell>time mysqldump --databases testdb -uroot -p12345 -l --master-data=2 -h192.168.0.10 >/tmp/testdb.sql

3、查看binlog位置

shell>head -n 100 /tmp/testdb.sql

-- CHANGE MASTER TO MASTER_LOG_FILE='masterlog.000146', MASTER_LOG_POS=37522828;

4、將主機數據拷貝至從機

在從機執行shell>scp [email protected]:/tmp/testdb.sql /tmp/

輸入主機的密碼進行拷貝

5、拷貝完成後進行導入

登錄從機mysql

shell>mysql -uroot -p12345

mysql>source /tmp/testdb.sql;

mysql> change master to master_host='192.168.0.10',master_user='u_master',master_password='master@2016',master_port=3306,master_log_file='masterlog.000146',master_log_pos=37522828;

mysql> start slave;

6、驗證同步狀態是否正常

mysql>show slave status\G;

Master_Log_File: masterlog.000147 

Read_Master_Log_Pos: 1105108 

Relay_Log_File: slavelog.000004 

Relay_Log_Pos: 1105271 

Relay_Master_Log_File: masterlog.000147 

Slave_IO_Running: Yes 

Slave_SQL_Running: Yes

Exec_Master_Log_Pos: 1105108

說明:

Slave_IO_Running與Slave_SQL_Running都需要是yes,說明同步監聽線程及執行線程都是正常

Read_Master_Log_Pos 與 Exec_Master_Log_Pos需相等說明日誌已同步至相同位置

至此完成主從恢復。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章