mysql主从同步-percona-toolkit工具 使用梳理

在mysql工作中接触最多的就是mysql replication,mysql在复制方面还是会有一些常规问题,比如主库宕机或者从库宕机有可能会导致复制中断,通常需要进行人为修复,或者很多时候需要把一个从库提升为主库,但对从库和主库的数据一致性不能保证一样。这种情况下就需要使用percona-toolkit工具的pt-table-checksum组件来检查主从数据的一致性;如果发现不一致的数据,可以通过pt-table-sync修复;还可以通过pt-heartbeat监控主从复制延迟。当然如果数据量小,slave只是当做一个备份使用,那么出现数据不一致完全可以重做,或者通过其他方法解决。如果数据量非常大,重做就是非常蛋碎的一件事情了。比如说,线上数据库做了主从同步环境,数据库在进行了迁移后,需要对mysql迁移(Replication)后的数据一致性进行校验,但又不能对生产环境使用造成影响,pt-table-checksum成为了绝佳也是唯一的检查工具。

percona-toolkit介绍
percona-toolkit是一组高级命令行工具的集合,用来执行各种通过手工执行非常复杂和麻烦的mysql和系统任务,这些任务包括:
   1)检查master和slave数据的一致性
   2)有效地对记录进行归档
   3)查找重复的索引
   4)对服务器信息进行汇总
   5)分析来自日志和tcpdump的查询
   6)当系统出问题的时候收集重要的系统信息
percona-toolkit源自Maatkit和Aspersa工具,这两个工具是管理mysql的最有名的工具不过,现在Maatkit工具已经不维护了,所以以后推荐还是使用percona-toolkit工具!
这些工具主要包括开发、性能、配置、监控、复制、系统、实用六大类,作为一个优秀的DBA,里面有的工具非常有用,如果能掌握并加以灵活应用,将能极大的提高工作效率。

percona-toolkit工具中最主要的三个组件分别是:
   1)pt-table-checksum 负责监测mysql主从数据一致性
   2)pt-table-sync 负责当主从数据不一致时修复数据,让它们保存数据的一致性
   3)pt-heartbeat 负责监控mysql主从同步延迟
下面就对这三个组件的使用做一记录,当然percona-toolkit工具也有很多其他组件,后面会一一说明。

percona-toolkit工具安装(建议主库和从库服务器上都安装)
软件下载并在主库服务器上安装 [百度云盘下载地址:https://pan.baidu.com/s/1bp1OOgf   (提取密码:y462)]
[root@master-server src]# wget https://www.percona.com/downloads/percona-toolkit/2.2.7/RPM/percona-toolkit-2.2.7-1.noarch.rpm
[root@master-server src]# rpm -ivh percona-toolkit-2.2.7-1.noarch.rpm     //安装后,percona-toolkit工具的各个组件命令就有有了(输入ht-,按TAB键就会显示)

安装该工具依赖的软件包
[root@master-server src]# yum install perl-IO-Socket-SSL perl-DBD-MySQL perl-Time-HiRes perl perl-DBI -y

一、pt-table-checksum使用梳理
pt-table-checksum 是 Percona-Toolkit的组件之一,用于检测MySQL主、从库的数据是否一致。其原理是在主库执行基于statement的sql语句来生成主库数据块的checksum,把相同的sql语句传递到从库执行,并在从库上计算相同数据块的checksum,最后,比较主从库上相同数据块的checksum值,由此判断主从数据是否一致。检测过程根据唯一索引将表按row切分为块(chunk),以为单位计算,可以避免锁表。检测时会自动判断复制延迟、 master的负载, 超过阀值后会自动将检测暂停,减小对线上服务的影响。
pt-table-checksum 默认情况下可以应对绝大部分场景,官方说,即使上千个库、上万亿的行,它依然可以很好的工作,这源自于设计很简单,一次检查一个表,不需要太多的内存和多余的操作;必要时,pt-table-checksum 会根据服务器负载动态改变 chunk 大小,减少从库的延迟。

为了减少对数据库的干预,pt-table-checksum还会自动侦测并连接到从库,当然如果失败,可以指定--recursion-method选项来告诉从库在哪里。它的易用性还体现在,复制若有延迟,在从库 checksum 会暂停直到赶上主库的计算时间点(也通过选项--设定一个可容忍的延迟最大值,超过这个值也认为不一致)。

为了保证主数据库服务的安全,该工具实现了许多保护措施:
    1)自动设置 innodb_lock_wait_timeout 为1s,避免引起
    2)默认当数据库有25个以上的并发查询时,pt-table-checksum会暂停。可以设置 --max-load 选项来设置这个阀值
    3)当用 Ctrl+C 停止任务后,工具会正常的完成当前 chunk 检测,下次使用 --resume 选项启动可以恢复继续下一个 chunk

pt-table-checksum [OPTIONS] [DSN]
pt-table-checksum:在主(master)上通过执行校验的查询对复制的一致性进行检查,对比主从的校验值,从而产生结果。DSN指向的是主的地址,该工具的退出状态不为零,如果发现有任何差别,或者如果出现任何警告或错误。注意:第一次运行的时候需要加上--create-replicate-table参数,生成checksums表!!如果不加这个参数,那么就需要在对应库下手工添加这张表了,表结构SQL如下:

[plain]  view plain  copy
  1. CREATE TABLE checksums (  
  2.    db             char(64)     NOT NULL,  
  3.    tbl            char(64)     NOT NULL,  
  4.    chunk          int          NOT NULL,  
  5.    chunk_time     float            NULL,  
  6.    chunk_index    varchar(200)     NULL,  
  7.    lower_boundary text             NULL,  
  8.    upper_boundary text             NULL,  
  9.    this_crc       char(40)     NOT NULL,  
  10.    this_cnt       int          NOT NULL,  
  11.    master_crc     char(40)         NULL,  
  12.    master_cnt     int              NULL,  
  13.    ts             timestamp    NOT NULL,  
  14.    PRIMARY KEY (db, tbl, chunk),  
  15.    INDEX ts_db_tbl (ts, db, tbl)  
  16. ) ENGINE=InnoDB;  

常用参数解释:
--nocheck-replication-filters :不检查复制过滤器,建议启用。后面可以用--databases来指定需要检查的数据库。
--no-check-binlog-format : 不检查复制的binlog模式,要是binlog模式是ROW,则会报错。
--replicate-check-only :只显示不同步的信息。
--replicate= :把checksum的信息写入到指定表中,建议直接写到被检查的数据库当中。
--databases= :指定需要被检查的数据库,多个则用逗号隔开。
--tables= :指定需要被检查的表,多个用逗号隔开
h= :Master的地址
u= :用户名
p=:密码
P= :端口

最重要的一点就是:
要在主库上授权,能让主库ip访问。这一点不能忘记!(实验证明从库上可以不授权,但最好还是从库也授权)
注意:
1)根据测试,需要一个即能登录主库,也能登录从库的账号;
2)只能指定一个host,必须为主库的IP;
3)在检查时会向表加S锁;
4)运行之前需要从库的同步IO和SQL进程是YES状态。

例如:(本文例子中:192.168.1.101是主库ip,192.168.1.102是从库ip)
在主库执行授权(一定要对主库ip授权,授权的用户名和密码可以自行定义,不过要保证这个权限能同时登陆主库和从库)
mysql> GRANT SELECT, PROCESS, SUPER, REPLICATION SLAVE,CREATE,DELETE,INSERT,UPDATE ON *.* TO 'root'@'192.168.1.101' identified by '123456';
mysql> flush privileges;

在从库上执行授权
mysql> GRANT SELECT, PROCESS, SUPER, REPLICATION SLAVE ON *.* TO 'root'@'192.168.1.101' IDENTIFIED BY '123456';
mysql> flush privileges;

如下,在主库上执行的一个检查主从数据一致性的命令(别忘了第一次运行的时候需要添加--create-replicate-table参数,后续再运行时就不需要加了):
下面命令中的192.168.1.101是主库ip
检查的是huanqiu库下的haha表的数据(当然,命令中也可以不跟表,直接检查某整个库的数据;如下去掉--tables=haha表,直接检查huanqiu库的数据)
[root@master-server ~]# pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --replicate=huanqiu.checksums --create-replicate-table --databases=huanqiu --tables=haha h=192.168.1.101,u=root,p=123456,P=3306

[plain]  view plain  copy
  1. <span style="color:#333333;">Diffs cannot be detected because no slaves were found.  Please read the --recursion-method documentation for information.  
  2.             TS ERRORS  DIFFS     ROWS  CHUNKS SKIPPED    TIME TABLE  
  3. 01-08T04:04:54      0      0        4       1       0   0.009 huanqiu.haha</span>  

上面有报错:
Diffs cannot be detected because no slaves were found. Please read the --recursion-method documentation for information
上面的提示信息很清楚,因为找不到从,所以执行失败,提示用参数--recursion-method 可以指定模式解决。
其实是因为从库的slave关闭了。
在主库上执行:

[plain]  view plain  copy
  1. mysql> show processlist;  
  2. +----+------+-----------+------+---------+------+-------+------------------+  
  3. | Id | User | Host      | db   | Command | Time | State | Info             |  
  4. +----+------+-----------+------+---------+------+-------+------------------+  
  5. | 10 | root | localhost | NULL | Query   |    0 | init  | show processlist |  
  6. +----+------+-----------+------+---------+------+-------+------------------+  

发现没有slave在运行。

在从库上开启slave
mysql> start slave;
mysql> show slave status\G;

再在主库上执行:

[plain]  view plain  copy
  1. mysql> show processlist;  
  2. +----+-------+---------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+  
  3. | Id | User  | Host                | db   | Command     | Time | State                                                                 | Info             |  
  4. +----+-------+---------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+  
  5. | 10 | root  | localhost           | NULL | Query       |    0 | init                                                                  | show processlist |  
  6. | 18 | slave | 192.168.1.102:37115 | NULL | Binlog Dump |    5 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL             |  
  7. +----+-------+---------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+  

发现已有slave在运行。

再次执行检查命令:
[root@master-server ~]# pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --replicate=huanqiu.checksums --databases=huanqiu --tables=haha h=192.168.1.101,u=root,p=123456,P=3306

[plain]  view plain  copy
  1. <span style="color:#ff0000;">        </span><span style="color:#333333;"> TS ERRORS  DIFFS     ROWS  CHUNKS SKIPPED    TIME TABLE  
  2. 01-08T04:11:03      0      0        4       1       0   1.422 huanqiu.haha</span>  

解释:
TS :完成检查的时间。
ERRORS :检查时候发生错误和警告的数量。
DIFFS :0表示一致,1表示不一致。当指定--no-replicate-check时,会一直为0,当指定--replicate-check-only会显示不同的信息。
ROWS :表的行数。
CHUNKS :被划分到表中的块的数目。
SKIPPED :由于错误或警告或过大,则跳过块的数目。
TIME :执行的时间。
TABLE :被检查的表名。

二、pt-table-sync用法梳理
如果通过pt-table-checksum 检查找到了不一致的数据表,那么如何同步数据呢?即如何修复MySQL主从不一致的数据,让他们保持一致性呢?
这时候可以利用另外一个工具pt-table-sync。
使用方法:
pt-table-sync: 高效的同步MySQL表之间的数据,他可以做单向和双向同步的表数据。他可以同步单个表,也可以同步整个库。它不同步表结构、索引、或任何其他模式对象。所以在修复一致性之前需要保证他们表存在。

假如上面检查数据时发现主从不一致
[root@master-server ~]# pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --replicate=huanqiu.checksums --databases=huanqiu --tables=haha h=192.168.1.101,u=root,p=123456,P=3306

[plain]  view plain  copy
  1. <span style="color:#333333;">TS ERRORS  DIFFS     ROWS  CHUNKS SKIPPED    TIME TABLE  
  2. 01-08T04:18:07      0      1        4       1       0   0.843 huanqiu.haha</span>  

现在需要DIFFS为1可知主从数据不一致,需要修复!修复命令如下:
先master的ip,用户,密码,然后是slave的ip,用户,密码
[root@master-server ~]# pt-table-sync --replicate=huanqiu.checksums h=192.168.1.101,u=root,p=123456 h=192.168.1.102,u=root,p=123456 --print

[plain]  view plain  copy
  1. <span style="color:#333333;">REPLACE INTO `huanqiu`.`haha`(`id`, `name`) VALUES ('1', 'wangshibo') /*percona-toolkit src_db:huanqiu src_tbl:haha src_dsn:h=192.168.1.101,p=...,u=root dst_db:huanqiu dst_tbl:haha dst_dsn:h=192.168.1.102,p=...,u=root lock:1 transaction:1 changing_src:huanqiu.checksums replicate:huanqiu.checksums bidirectional:0 pid:23676 user:root host:master-server*/;  
  2. REPLACE INTO `huanqiu`.`haha`(`id`, `name`) VALUES ('2', 'wangshikui') /*percona-toolkit src_db:huanqiu src_tbl:haha src_dsn:h=192.168.1.101,p=...,u=root dst_db:huanqiu dst_tbl:haha dst_dsn:h=192.168.1.102,p=...,u=root lock:1 transaction:1 changing_src:huanqiu.checksums replicate:huanqiu.checksums bidirectional:0 pid:23676 user:root host:master-server*/;  
  3. REPLACE INTO `huanqiu`.`haha`(`id`, `name`) VALUES ('3', 'limeng') /*percona-toolkit src_db:huanqiu src_tbl:haha src_dsn:h=192.168.1.101,p=...,u=root dst_db:huanqiu dst_tbl:haha dst_dsn:h=192.168.1.102,p=...,u=root lock:1 transaction:1 changing_src:huanqiu.checksums replicate:huanqiu.checksums bidirectional:0 pid:23676 user:root host:master-server*/;  
  4. REPLACE INTO `huanqiu`.`haha`(`id`, `name`) VALUES ('4', 'wanghi') /*percona-toolkit src_db:huanqiu src_tbl:haha src_dsn:h=192.168.1.101,p=...,u=root dst_db:huanqiu dst_tbl:haha dst_dsn:h=192.168.1.102,p=...,u=root lock:1 transaction:1 changing_src:huanqiu.checksums replicate:huanqiu.checksums bidirectional:0 pid:23676 user:root host:master-server*/;</span>  

参数解释:
--replicate= :指定通过pt-table-checksum得到的表,这2个工具差不多都会一直用。
--databases= : 指定执行同步的数据库。
--tables= :指定执行同步的表,多个用逗号隔开。
--sync-to-master :指定一个DSN,即从的IP,他会通过show processlist或show slave status 去自动的找主。
h= :服务器地址,命令里有2个ip,第一次出现的是Master的地址,第2次是Slave的地址。
u= :帐号。
p= :密码。
--print :打印,但不执行命令
--execute :执行命令

上面命令介绍完了,接下来开始执行修复:
通过(--print)打印出来了修复数据的sql语句,可以手动的在slave从库上执行,让他们数据保持一致性,这样比较麻烦!
可以直接在master主库上执行修复操作,通过--execute参数,如下:
[root@master-server ~]# pt-table-sync --replicate=huanqiu.checksums h=192.168.1.101,u=root,p=123456 h=192.168.1.102,u=root,p=123456 --execute

如上修复后,再次检查,发现主从库数据已经一致了!
[root@master-server ~]# pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --replicate=huanqiu.checksums --databases=huanqiu --tables=haha h=192.168.1.101,u=root,p=123456,P=3306

[plain]  view plain  copy
  1. <span style="color:#ff0000;">         </span><span style="color:#333333;"> TS ERRORS  DIFFS     ROWS  CHUNKS SKIPPED    TIME TABLE  
  2. 01-08T04:36:43      0      0        4       1       0   0.040 huanqiu.haha</span>  

-----------------------------------------------------------------------------------------------------------------------
建议:
修复数据的时候,最好还是用--print打印出来的好,这样就可以知道那些数据有问题,可以人为的干预下。
不然直接执行了,出现问题之后更不好处理。总之还是在处理之前做好数据的备份工作。

注意:要是表中没有唯一索引或则主键则会报错:
Can't make changes on the master because no unique index exists at /usr/local/bin/pt-table-sync line 10591.
-----------------------------------------------------------------------------------------------------------------------
为了确保主从数据的一致性,可以编写监控脚本,定时检查。当检查到主从数据不一致时,强制修复数据。
[root@master-server ~]# cat /root/pt_huanqiu.sh

[plain]  view plain  copy
  1. #!/bin/bash  
  2. NUM=$(/usr/bin/pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --replicate=huanqiu.checksums --databases=huanqiu  h=192.168.1.101,u=root,p=123456,P=3306|awk -F" " '{print $3}'|sed -n '2p')  
  3. if [ $NUM -eq 1 ];then  
  4.   /usr/bin/pt-table-sync --replicate=huanqiu.checksums h=192.168.1.101,u=root,p=123456 h=192.168.1.102,u=root,p=123456 --print  
  5.   /usr/bin/pt-table-sync --replicate=huanqiu.checksums h=192.168.1.101,u=root,p=123456 h=192.168.1.102,u=root,p=123456 --execute  
  6. else  
  7.   echo "data is ok"  
  8. fi  

[root@master-server ~]# cat /root/pt_huanpc.sh 

[plain]  view plain  copy
  1. #!/bin/bash  
  2. NUM=$(/usr/bin/pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --replicate=huanpc.checksums --databases=huanpc  h=192.168.1.101,u=root,p=123456,P=3306|awk -F" " '{print $3}'|sed -n '2p')  
  3. if [ $NUM -eq 1 ];then  
  4.   /usr/bin/pt-table-sync --replicate=huanpc.checksums h=192.168.1.101,u=root,p=123456 h=192.168.1.102,u=root,p=123456 --print  
  5.   /usr/bin/pt-table-sync --replicate=huanpc.checksums h=192.168.1.101,u=root,p=123456 h=192.168.1.102,u=root,p=123456 --execute  
  6. else  
  7.   echo "data is ok"  
  8. fi  

[root@master-server ~]# crontab -l
#检查主从huanqiu库数据一致性
* * * * * /bin/bash -x /root/pt_huanqiu.sh > /dev/null 2>&1
* * * * * sleep 10;/bin/bash -x /root/pt_huanqiu.sh > /dev/null 2>&1
* * * * * sleep 20;/bin/bash -x /root/pt_huanqiu.sh > /dev/null 2>&1
* * * * * sleep 30;/bin/bash -x /root/pt_huanqiu.sh > /dev/null 2>&1
* * * * * sleep 40;/bin/bash -x /root/pt_huanqiu.sh > /dev/null 2>&1
* * * * * sleep 50;/bin/bash -x /root/pt_huanqiu.sh > /dev/null 2>&1

#检查主从huanpc库数据一致性
* * * * * /bin/bash -x /root/root/pt_huanpc.sh > /dev/null 2>&1
* * * * * sleep 10;/bin/bash -x /root/pt_huanpc.sh > /dev/null 2>&1
* * * * * sleep 20;/bin/bash -x /root/pt_huanpc.sh > /dev/null 2>&1
* * * * * sleep 30;/bin/bash -x /root/pt_huanpc.sh > /dev/null 2>&1
* * * * * sleep 40;/bin/bash -x /root/pt_huanpc.sh > /dev/null 2>&1
* * * * * sleep 50;/bin/bash -x /root/pt_huanpc.sh > /dev/null 2>&1

-----------------------------------------------------------------------------------------------------------------------
最后总结:
pt-table-checksum和pt-table-sync工具很给力,工作中常常在使用。注意使用该工具需要授权,一般SELECT, PROCESS, SUPER, REPLICATION SLAVE等权限就已经足够了。

-----------------------------------------------------------------------------------------------------------------------
另外说一个问题:
在上面的操作中,在主库里添加pt-table-checksum检查的权限(从库可以不授权)后,进行数据一致性检查操作,会在操作的库(实例中是huanqiu、huanpc)下产生一个checksums表!
这张checksums表是pt-table-checksum检查过程中产生的。这张表一旦产生了,默认是删除不了的,并且这张表所在的库也默认删除不了,删除后过一会儿就又会出来。

[plain]  view plain  copy
  1. <span style="color:#333333;">mysql> use huanqiu;  
  2. Reading table information for completion of table and column names  
  3. You can turn off this feature to get a quicker startup with -A  
  4.    
  5. Database changed  
  6. mysql> show tables;  
  7. +-------------------+  
  8. | Tables_in_huanqiu |  
  9. +-------------------+  
  10. | checksums         |  
  11. | haha              |  
  12. +-------------------+  
  13. 2 rows in set (0.00 sec)  
  14.    
  15. mysql> drop table checksums;  
  16. Query OK, 0 rows affected (0.01 sec)  
  17.    
  18. mysql> show tables;  
  19. +-------------------+  
  20. | Tables_in_huanqiu |  
  21. +-------------------+  
  22. | haha              |  
  23. +-------------------+  
  24. 1 row in set (0.00 sec)  
  25.    
  26. mysql> show tables;         //过一段时间再次查看,发现checksums表还在  
  27. +-------------------+  
  28. | Tables_in_huanqiu |  
  29. +-------------------+  
  30. | checksums         |  
  31. | haha              |  
  32. +-------------------+  
  33. 2 rows in set (0.00 sec)  
  34.    
  35. 不仅这张表删除不了,这张表所在的库也删除不了,删除后过一会儿就是自动生成。  
  36. mysql> drop database huanqiu;  
  37. Query OK, 1 row affected (0.01 sec)  
  38.    
  39. mysql> drop database huanpc;  
  40. Query OK, 1 row affected (0.02 sec)  
  41.    
  42. mysql> show databases;  
  43. +--------------------+  
  44. | Database           |  
  45. +--------------------+  
  46. | information_schema |  
  47. | huanqiu            |  
  48. | mysql              |  
  49. | performance_schema |  
  50. | test               |  
  51. +--------------------+  
  52. 5 rows in set (0.00 sec)  
  53.    
  54. mysql> drop database huanqiu;  
  55. Query OK, 1 row affected (0.01 sec)  
  56.    
  57. mysql> show databases;  
  58. +--------------------+  
  59. | Database           |  
  60. +--------------------+  
  61. | information_schema |  
  62. | huanpc             |  
  63. | huanqiu            |  
  64. | mysql              |  
  65. | performance_schema |  
  66. | test               |  
  67. +--------------------+  
  68. 6 rows in set (0.00 sec)</span>  

要想删除的话,一定要先把pt-table-checksum检查前添加的权限收回!

[plain]  view plain  copy
  1. <span style="color:#333333;">mysql> show grants for 'root'@'192.168.1.101';      
  2. +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+  
  3. | Grants for [email protected]                                                                                                                                                       |  
  4. +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+  
  5. | GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, PROCESS, SUPER, REPLICATION SLAVE ON *.* TO 'root'@'192.168.1.101' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |  
  6. +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+  
  7. 1 row in set (0.00 sec)  
  8.    
  9. mysql> revoke SELECT, INSERT, UPDATE, DELETE, CREATE, PROCESS, SUPER, REPLICATION SLAVE ON *.* FROM 'root'@'192.168.1.101';  
  10. Query OK, 0 rows affected (0.01 sec)  
  11.    
  12. mysql> show grants for 'root'@'192.168.1.101';  
  13. +-----------------------------------------------------------------------------------------------------------------+  
  14. | Grants for [email protected]                                                                                   |  
  15. +-----------------------------------------------------------------------------------------------------------------+  
  16. | GRANT USAGE ON *.* TO 'root'@'192.168.1.101' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |  
  17. +-----------------------------------------------------------------------------------------------------------------+  
  18. 1 row in set (0.00 sec)  
  19.    
  20. mysql> select user,host,password from mysql.user;  
  21. +-------+---------------+-------------------------------------------+  
  22. | user  | host          | password                                  |  
  23. +-------+---------------+-------------------------------------------+  
  24. | root  | localhost     | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |  
  25. | root  | master-server |                                           |  
  26. | root  | 127.0.0.1     |                                           |  
  27. | root  | ::1           |                                           |  
  28. |       | localhost     |                                           |  
  29. |       | master-server |                                           |  
  30. | root  | 192.168.1.101 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |  
  31. | slave | 192.168.1.102 | *4F0FF134CC4C1A2872D972373A6AA86CA0A81872 |  
  32. +-------+---------------+-------------------------------------------+  
  33. 8 rows in set (0.00 sec)  
  34.    
  35. mysql> delete from mysql.user where user="root" and host="192.168.1.101";   //这一步其实不必操作,此步删除操作不能在上面revoke执行前进行,否则revoke回收权限失败!  
  36. Query OK, 1 row affected (0.00 sec)  
  37.    
  38. mysql> select user,host,password from mysql.user;  
  39. +-------+---------------+-------------------------------------------+  
  40. | user  | host          | password                                  |  
  41. +-------+---------------+-------------------------------------------+  
  42. | root  | localhost     | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |  
  43. | root  | master-server |                                           |  
  44. | root  | 127.0.0.1     |                                           |  
  45. | root  | ::1           |                                           |  
  46. |       | localhost     |                                           |  
  47. |       | master-server |                                           |  
  48. | slave | 192.168.1.102 | *4F0FF134CC4C1A2872D972373A6AA86CA0A81872 |  
  49. +-------+---------------+-------------------------------------------+  
  50. 7 rows in set (0.00 sec)</span>  

权限删除后,就能成功删除checksums这张表和它所在的库了!
主库的checksums删除了,从库的这张表也会跟着删除

[plain]  view plain  copy
  1. <span style="color:#333333;">mysql> use huanpc;  
  2. Database changed  
  3. mysql> show tables;  
  4. +------------------+  
  5. | Tables_in_huanpc |  
  6. +------------------+  
  7. | checksums        |  
  8. | heihei           |  
  9. +------------------+  
  10. 2 rows in set (0.00 sec)  
  11.    
  12. mysql> drop table checksums;  
  13. Query OK, 0 rows affected (0.01 sec)  
  14.    
  15. mysql> show tables;  
  16. +------------------+  
  17. | Tables_in_huanpc |  
  18. +------------------+  
  19. | heihei           |  
  20. +------------------+  
  21. 1 row in set (0.01 sec)  
  22.    
  23. mysql> use huanqiu;  
  24. Reading table information for completion of table and column names  
  25. You can turn off this feature to get a quicker startup with -A  
  26.    
  27. Database changed  
  28. mysql> show tables;  
  29. +-------------------+  
  30. | Tables_in_huanqiu |  
  31. +-------------------+  
  32. | checksums         |  
  33. | haha              |  
  34. +-------------------+  
  35. 2 rows in set (0.00 sec)  
  36.    
  37. mysql> drop table checksums;  
  38. Query OK, 0 rows affected (0.00 sec)  
  39.    
  40. mysql> show tables;  
  41. +-------------------+  
  42. | Tables_in_huanqiu |  
  43. +-------------------+  
  44. | haha              |  
  45. +-------------------+  
  46. 1 row in set (0.00 sec)</span>  

也就是说,checksums表一旦产生,不仅这张表默认删除不了,连同它所在的库,要是想删除它们,只能如上操作先撤销权限。
pt-heartbeat监控mysql主从复制延迟整理

猜你喜欢

转载自blog.csdn.net/shangyuanlang/article/details/80813818