MySQL中RESET SLAVE和RESET MASTER的區別

 RESET SLAVE的語法以下:html

RESET SLAVE [ALL] [channel_option]

channel_option: 
    FOR CHANNEL channel

 

其中,channel_option主要是針對5.7.6引入的多源複製。mysql

 

RESET SLAVEsql

官方的解釋以下app

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完。ui

可是spa

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會報錯。code

以下所示:server

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

相關文章
相關標籤/搜索