阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

MySQL中RESET SLAVE和RESET MASTER的区别

103次阅读
没有评论

共计 2186 个字符,预计需要花费 6 分钟才能阅读完成。

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

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2016-08/134346.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计2186字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中