13.4.1.2 RESET MASTER Statement语句

RESET MASTER [TO binary_log_file_index_number]
Warning警告

Use this statement with caution to ensure you do not lose any wanted binary log file data and GTID execution history.请谨慎使用此语句,以确保不会丢失任何需要的二进制日志文件数据和GTID执行历史记录。

RESET MASTER requires the RELOAD privilege.RESET MASTER需要RELOAD权限。

For a server where binary logging is enabled (log_bin is ON), RESET MASTER deletes all existing binary log files and resets the binary log index file, resetting the server to its state before binary logging was started. 对于启用二进制日志记录的服务器(log_bin处于ON状态),RESET MASTER会删除所有现有的二进制日志文件并重置二进制日志索引文件,从而将服务器重置为二进制日志记录启动前的状态。A new empty binary log file is created so that binary logging can be restarted.将创建一个新的空二进制日志文件,以便可以重新启动二进制日志记录。

For a server where GTIDs are in use (gtid_mode is ON), issuing RESET MASTER resets the GTID execution history. 对于使用gtid的服务器(gtid_modeON),发出RESET MASTER会重置gtid执行历史记录。The value of the gtid_purged system variable is set to an empty string (''), the global value (but not the session value) of the gtid_executed system variable is set to an empty string, and the mysql.gtid_executed table is cleared (see mysql.gtid_executed Table). gtid_purged系统变量的值设置为空字符串(''),gtid_executed系统变量的全局值(但不是会话值)设置为空字符串,并清除mysql.gtid_executed表(请参阅mysql.gtid_executed表)。If the GTID-enabled server has binary logging enabled, RESET MASTER also resets the binary log as described above. 如果启用GTID的服务器启用了二进制日志记录,则RESET MASTER也会如上所述重置二进制日志。Note that RESET MASTER is the method to reset the GTID execution history even if the GTID-enabled server is a replica where binary logging is disabled; RESET REPLICA | SLAVE has no effect on the GTID execution history. 请注意,RESET MASTER是重置GTID执行历史记录的方法,即使启用GTID的服务器是禁用二进制日志记录的副本;RESET REPLICA | SLAVE对GTID执行历史没有影响。For more information on resetting the GTID execution history, see Resetting the GTID Execution History.有关重置GTID执行历史记录的更多信息,请参阅重置GTID执行历史记录

Issuing RESET MASTER without the optional TO clause 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 starting at 1. 在没有可选的TO子句的情况下发出RESET MASTER将删除索引文件中列出的所有二进制日志文件,将二进制日志索引文件重置为空,并从1开始创建一个新的二进制日志文件。Use the optional TO clause to start the binary log file index from a number other than 1 after the reset.重置后,使用可选的TO子句从1以外的数字开始二进制日志文件索引。

Using RESET MASTER with the TO clause to specify a binary log file index number to start from simplifies failover by providing a single statement alternative to the FLUSH BINARY LOGS and PURGE BINARY LOGS TO statements. 使用RESET MASTER配合TO子句指定要从中开始的二进制日志文件索引号,通过提供一条替换语句替代FLUSH BINARY LOGS语句和PURGE BINARY LOGS TO语句,简化了故障切换。Check that you are using a reasonable value for the index number. 检查索引号是否使用了合理的值。If you enter an incorrect value, you can correct this by issuing another RESET MASTER statement with or without the TO clause. 如果输入的值不正确,可以通过发出另一条带或不带TO子句的RESET MASTER语句来更正此错误。If you do not correct a value that is out of range, the server cannot be restarted.如果不更正超出范围的值,则无法重新启动服务器。

The following example demonstrates TO clause usage:以下示例演示TO子句的用法:

RESET MASTER TO 1234;

SHOW BINARY LOGS;
+-------------------+-----------+-----------+
| Log_name          | File_size | Encrypted |
+-------------------+-----------+-----------+
| source-bin.001234 |       154 | No        |
+-------------------+-----------+-----------+
Important重要

The effects of RESET MASTER without the TO clause differ from those of PURGE BINARY LOGS in 2 key ways:不带TO子句的RESET MASTER的效果与PURGE BINARY LOGS的效果在两个关键方面不同:

  1. RESET MASTER removes all binary log files that are listed in the index file, leaving only a single, empty binary log file with a numeric suffix of .000001, whereas the numbering is not reset by PURGE BINARY LOGS.RESET MASTER删除索引文件中列出的所有二进制日志文件,只留下一个数字后缀为.000001的空二进制日志文件,而PURGE BINARY LOGS不会重置编号。

  2. RESET MASTER is not intended to be used while any replicas are running. 在运行任何复制副本时,不打算使用RESET MASTERThe behavior of RESET MASTER when used while replicas are running is undefined (and thus unsupported), whereas PURGE BINARY LOGS may be safely used while replicas are running.在副本运行时使用RESET MASTER的行为未定义(因此不受支持),而在副本运行时可以安全地使用PURGE BINARY LOGS

See also Section 13.4.1.1, “PURGE BINARY LOGS Statement”.另请参阅第13.4.1.1节,“清除二进制日志语句”

RESET MASTER without the TO clause can prove useful when you first set up a source and replica, so that you can verify the setup as follows:在首次设置源和复制副本时,如果不使用TO子句,则可以使用RESET MASTER,以便按如下方式验证设置:

  1. Start the source and replica, and start replication (see Section 17.1.2, “Setting Up Binary Log File Position Based Replication”).启动源和副本,并启动复制(请参阅第17.1.2节,“设置二进制日志文件基于位置的复制”)。

  2. Execute a few test queries on the source.对源代码执行一些测试查询。

  3. Check that the queries were replicated to the replica.检查查询是否已复制到副本。

  4. When replication is running correctly, issue STOP REPLICA | SLAVE followed by RESET REPLICA | SLAVE on the replica, then verify that no unwanted data from the test queries exists on the replica.复制正确运行时,在复制副本上发出STOP REPLICA | SLAVE,然后RESET REPLICA | SLAVE,然后验证复制副本上不存在来自测试查询的不需要的数据。

  5. Issue RESET MASTER on the source to clean up the test queries.在源上发出RESET MASTER以清除测试查询。

After verifying the setup, resetting the source and replica and ensuring that no unwanted data or binary log files generated by testing remain on the source or replica, you can start the replica and begin replicating.在验证设置、重置源和副本并确保源或副本上没有保留测试生成的不需要的数据或二进制日志文件后,您可以启动副本并开始复制。