MySQL中RESET SLAVE和RESET MASTER的區別


 RESET SLAVE的語法如下:

RESET SLAVE [ALL] [channel_option]

channel_option: 
    FOR CHANNEL channel

 

其中,channel_option主要是針對5.7.6引入的多源復制。

 

RESET SLAVE

官方的解釋如下

RESET SLAVE makes the slave forget its replication position in the master's binary log. This statement is meant to be used for a clean start: It clears the master info and relay log info repositories, deletes all the relay log files, and starts a new relay log file. It also resets to 0 the replication delay specified with the MASTER_DELAY option to CHANGE MASTER TO. To use RESET SLAVE, the slave replication threads must be stopped (use STOP SLAVE if necessary).

其實,它是直接刪除master.info和relay-log.info文件,並刪除所有的relay log,然后重新生成一個新的relay log,即使relay log中還有SQL沒有被SQL線程apply完。

但是

RESET SLAVE有個問題,它雖然刪除了上述文件,但內存中的change master信息並沒有刪除,此時,可直接執行start slave,但因為刪除了master.info和relay-log.info,它會從頭開始接受主的binlog並應用。

RESET SLAVE does not change any replication connection parameters such as master host, master port, master user, or master password, which are retained in memory. This means that START SLAVE can be issued without requiring a CHANGE MASTER TO statement following RESET SLAVE.

 

RESET SLAVE ALL

相對於RESET SLAVE,RESET SLAVE ALL還會刪除內存中的連接信息,這個時候,執行start slave會報錯。

如下所示:

mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

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

mysql> start slave;
ERROR 1200 (HY000): The server is not configured as slave; fix in config file or with CHANGE MASTER TO

 

注意:從MySQL 5.6.7開始,RESET SLAVE和RESET SLAVE ALL會對當前事務進行隱式提交。

 

RESET MASTER

Deletes all binary log files listed in the index file, resets the binary log index file to be empty, and creates a new binary log file.

刪除所有的二進制日志,並重新創建一個新的二進制日志

 

在GTID環境中,

RESET MASTER會清除掉系統變量gtid_purged和gtid_executed的值。

從MySQL 5.7.5開始,該語句同樣會清空mysql.gtid_executed的內容,該表保存着GTID的信息,這樣,在slave中可不用開啟binlog。

 

RESET MASTER和PURGE BINARY LOGS的區別

1. RESET MASTER會刪除所有的二進制日志,而PURGE BINARY LOGS是一種基於時間點的刪除

   PURGE BINARY LOGS語法如下:

PURGE { BINARY | MASTER } LOGS
    { TO 'log_name' | BEFORE datetime_expr }

  譬如:

PURGE BINARY LOGS TO 'mysql-bin.010';
PURGE BINARY LOGS BEFORE '2008-04-02 22:46:26';

 

2. 在正常的主從復制環境中,如果在master上執行RESET MASTER,結果是不可預測的。但使用PURGE BINARY LOGS語句刪除binlog沒多大影響(前提是,刪除的binlog中的events已經傳輸到slave上)

 

參考

1. http://dev.mysql.com/doc/refman/5.7/en/reset-slave.html

2. http://dev.mysql.com/doc/refman/5.7/en/reset-master.html

3. http://dev.mysql.com/doc/refman/5.7/en/purge-binary-logs.html

 

   

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM