RESET MASTER和RESET SLAVE使用場景和說明

wzq609發表於2015-06-16

【前言】在配置主從的時候經常會用到這兩個語句,剛開始的時候還不清楚這兩個語句的使用特性和使用場景。

經過測試整理了以下文件,希望能對大家有所幫助;

 

【一】RESET MASTER引數

功能說明:刪除所有的binglog日誌檔案,並將日誌索引檔案清空,重新開始所有新的日誌檔案。用於第一次進行搭建主從庫時,進行主庫binlog初始化工作;

 

測試如下:

未刪除前

[root@mysql01 mysql]# pwd
/data/mysql


[root@mysql01 mysql]# ls
image


mysql> show master status\G;
*************************** 1. row ***************************
            File: mysql-bin.000025
        Position: 107
    Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.01 sec)


當前有25個binlong日誌,且Position的位置為107

 

執行RESET MASTER

mysql> reset master;
Query OK, 0 rows affected (0.03 sec)


mysql> show master status\G;
*************************** 1. row ***************************
            File: mysql-bin.000001
        Position: 107
    Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)

 

image

 

顯示所有的binlog已經被刪除掉,且binlog從000001 開始記錄

 

注:當資料庫要清理binlog檔案的時候,可以透過作業系統進行刪除,也可以執行reset master進行刪除。但是如果當前是主資料庫,且主從資料庫正常的時候,千萬不能用這種方式刪除。

【使用場景】第一次搭建主從資料庫時,用於主庫的初始化binglog操作;


【二】RESET SLAVE

功能說明:用於刪除SLAVE資料庫的relaylog日誌檔案,並重新啟用新的relaylog檔案;

登入從資料庫,未刪除前

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 192.168.47.167
                  Master_User: server
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000019
          Read_Master_Log_Pos: 12992
              Relay_Log_File: mysql02-relay-bin.000004
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000019
 

image

當前relaylog為0004;

 

刪除後

mysql> stop slave;                先停止slave
Query OK, 0 rows affected (0.01 sec)

mysql> reset slave;               
Query OK, 0 rows affected (0.04 sec)

 

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State:
                  Master_Host: 192.168.47.167
                  Master_User: server
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File:
          Read_Master_Log_Pos: 4
               Relay_Log_File: mysql02-relay-bin.000001
                Relay_Log_Pos: 4

 

image

RESET SLAVE將使SLAVE忘記主從複製關係的位置資訊。該語句將被用於乾淨的啟動, 它刪除master.info檔案和relay-log.info 檔案以及所有的relay log 檔案並重新啟用一個新的relaylog檔案。

使用場景:當原來的主從關係被破壞之後,從庫經過重新初始化後直接連線會報 ERROR 1201的錯誤,執行reset slave後,重新配置主從連線就可以了;

mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.167',MASTER_USER='test',MASTER_PASSWORD='test', MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=176658;

ERROR 1201 (HY000): Could not initialize master info structure; more error messages can be found in the MySQL error log

 

總結:如果是需要刪除mysql binlog和relaylog檔案的時候,那麼透過作業系統的刪除或者PURGE命令都可以,但是涉及到mysql主從配置的時候便需要使用RESET MASTER和RESET SLAVE解決問題;

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

本文作者:JOHN,某上市公司DBA,業餘時間專注於資料庫的技術管理,從管理的角度去運用技術。

技術部落格:獵人筆記                        資料庫技術群:367875324 (請備註資料庫型別)

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12679300/viewspace-1701285/,如需轉載,請註明出處,否則將追究法律責任。

相關文章