暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

gt-checksum 使用

原创 huayumicheng 2023-07-20
497


1、下载gt-checksum

--下载二进制版本
https://gitee.com/GreatSQL/gt-checksum/releases

wget https://product.greatdb.com/gt-checksum/gt-checksum-1.2.1-linux-x86-64.tar.gz

2、特性

MySQL DBA最常用的数据校验&修复工具应该是Percona Toolkit中的pt-table-checksum和pt-table-sync这两个工具,不过这两个工具并不支持MySQL MGR架构,以及国内常见的上云下云业务场景,还有MySQL、Oracle间的异构数据库等多种场景。

GreatSQL开源的gt-checksum工具可以满足上述多种业务需求场景,解决这些痛点。

gt-checksum工具支持以下几种常见业务需求场景:

  1. MySQL主从复制:主从复制中断后较长时间才发现,且主从间差异的数据量太多,这时候通常基本上只能重建复制从库,如果利用pt-table-checksum先校验主从数据一致性后,再利用pt-table-sync工具修复差异数据,这个过程要特别久,时间代价太大。

  2. MySQL MGR组复制:MySQL MGR因故崩溃整个集群报错退出,或某个节点异常退出,在恢复MGR集群时一般要面临着先检查各节点间数据一致性的需求,这时通常为了省事会选择其中一个节点作为主节点,其余从节点直接复制数据重建,这个过程要特别久,时间代价大。

  3. 上云下云业务场景:目前上云下云的业务需求很多,在这个过程中要进行大量的数据迁移及校验工作,如果出现字符集改变导致特殊数据出现乱码或其他的情况,如果数据迁移工具在迁移过程中出现bug或者数据异常而又迁移成功,此时都需要在迁移结束后进行一次数据校验才放心。

  4. 异构迁移场景:有时我们会遇到异构数据迁移场景,例如从Oracle迁移到MySQL,通常存在字符集不同,以及数据类型不同等情况,也需要在迁移结束后进行一次数据校验才放心。

  5. 定期校验场景:作为DBA在维护高可用架构中为了保证主节点出现异常后能够快速放心切换,就需要保证各节点间的数据一致性,需要定期执行数据校验工作。

以上这些场景,都可以利用gt-chcksum工具来满足。


3、使用环境

预编译好的二进制文件包,已经在Ubuntu、CentOS、RHEL等多个下测试通过。

下载配置Oracle驱动程序

如果需要校验Oracle数据库,则还需要先下载Oracle数据库相应版本的驱动程序。例如:待校验的数据库为Oracle 11-2,则要下载Oracle 11-2的驱动程序,并使之生效,否则连接Oracle会报错。

下载Oracle Instant Client
从 https://www.oracle.com/database/technologies/instant-client/downloads.html 下载免费的Basic或Basic Light软件包。

oracle basic client, instantclient-basic-linux.x64-11.2.0.4.0.zip

oracle sqlplus, instantclient-sqlplus-linux.x64-11.2.0.4.0.zip

oracle sdk, instantclient-sdk-linux.x64-11.2.0.4.0.zip

配置oracle client并生效
shell> unzip instantclient-basic-linux.x64-11.2.0.4.0.zip
shell> unzip instantclient-sqlplus-linux.x64-11.2.0.4.0.zip
shell> unzip instantclient-sdk-linux.x64-11.2.0.4.0.zip
shell> mv instantclient_11_2 /usr/local
shell> echo "export LD_LIBRARY_PATH=/usr/local/instantclient_11_2:$LD_LIBRARY_PATH" >> /etc/profile
shell> source /etc/profile


4、配置主从,生成测试数据

mysql -S /data/mysql/data/3306/mysqld.sock -uroot -p

mysql> create database mydb;
mysql> use mydb

create table t1 (id bigint not null auto_increment primary key,info1 varchar(255),info2 varchar(20),row_create_time datetime default current_timestamp,row_lastupdate_time datetime default current_timestamp on update current_timestamp) engine=innodb charset=utf8mb4 collate=utf8mb4_bin;

--新建存储过程
DELIMITER //

CREATE PROCEDURE insert_t1(IN insert_count INT)
BEGIN
  DECLARE i INT DEFAULT 1;
  DECLARE info1_value VARCHAR(20);
  DECLARE info2_value VARCHAR(20);

  WHILE i <= insert_count DO
      SET info1_value = SUBSTRING(MD5(RAND()), 1, 20);
      SET info2_value = SUBSTRING(MD5(RAND()), 1, 20);

      INSERT INTO mydb.t1 (info1, info2) VALUES (info1_value, info2_value);

      SET i = i + 1;
  END WHILE;
END //

DELIMITER ;


--执行存储过程
mysql> call insert_t1(100000);

--生成第二张表
mysql> create table t2 like t1;
mysql> insert into t2 select * from t1;



--在从库分别删除删除两条数据
mysql> set sql_log_bin=0;
mysql> delete from mydb.t1 where id in (1,10000);
mysql> delete from mydb.t2 where id in (1,10000);
mysql> set sql_log_bin=1;

5、gt-checksum安装

tar xzvf gt-checksum-1.2.1-linux-x86-64.tar.gz
mv gt-checksum-1.2.1-linux-x86-64 /usr/local/
ln -s /usr/local/gt-checksum-1.2.1-linux-x86-64 /usr/local/gt-checksum
echo "export PATH=\$PATH:/usr/local/gt-checksum" >>/etc/profile
source /etc/profile

--查看帮助

[root@go ~]# gt-checksum -h
NAME:
  gt-checksum - An opensource table and data checksum tool by GreatSQL

USAGE:
  gt-checksum [global options] command [command options] [arguments...]

VERSION:
  1.2.1

AUTHOR:
  GreatSQL <GreatSQL <greatsql@greatdb.com>>

COMMANDS:
  help, h Shows a list of commands or help for one command

GLOBAL OPTIONS:
  --config value, -f value                 Specifies config file. For example: --config gc.conf or -f gc.conf
  --srcDSN value, -S value                 Set source DSN. For example: -S type=oracle,user=root,passwd=abc123,host=127.0.0.1,port=1521,sid=helowin
  --dstDSN value, -D value                 Set destination DSN. For example: -D type=mysql,user=root,passwd=abc123,host=127.0.0.1,port=3306,charset=utf8
  --tables value, -t value                 Specify which tables to check. For example: --tables db1.* (default: "nil") [$nil, $schema.table, $...]
  --ignore-table value, --it value         Specify which tables ignore to check. For example: -it nil (default: "nil") [$nil, $database.table, $...]
  --checkNoIndexTable value, --nit value   Specify whether to check non-indexed tables. For example: --nit no (default: "no") [$yes, $no]
  --lowerCaseTableNames value, --lc value Specify whether to use lowercase table names. For example: --lc no (default: "no") [$yes, $no]
  --parallel-thds value, --thds value     Specify the number of parallel threads for data checksum. For example: --thds 5 (default: 5)
  --chanRowCount value, --cr value         Specifies how many rows are retrieved to check each time. For example: --cr 10000 (default: 10000)
  --queue-size value, --qs value           Specify data check queue depth. for example: --qs 100 (default: 100)
  --checkMode value, --cm value           Specify data check mode. For example: --cm count (default: "rows") [$count, $rows, $sample]
  --ratio value, -r value                 When checkMode is set to sample, specify the data sampling rate, set the range of 1-100, in percentage. For example: -r 10 (default: 10)
  --checkObject value, --co value         Specify data check object. For example: --co struct (default: "data") [$data, $struct, $index, $partitions, $foreign, $trigger, $func, $proc]
  --ScheckFixRule value, --sfr value       column to fix based on. For example: --sfr src (default: "src") [$src, $dst]
  --ScheckOrder value, --sco value         The positive sequence check of column. For example: --sco yes (default: "yes") [$yes, $no]
  --ScheckMod value, --scm value           column check mode. For example: --scm strict (default: "strict") [$strict, $loose]
  --logFile value, --lf value             Specify output log file name. For example: --lf ./gt-checksum.log (default: "./gt-checksum.log")
  --logLevel value, --ll value             Specify output log level. For example: --ll info (default: "info") [$debug, $info, $warn, $error]
  --datafix value, --df value             Specify data repair mode. For example: --df table (default: "file") [$file, $table]
  --fixFileName value, --ffn value         Set data repair SQL file name. For example: --ffn ./gt-checksum-DataFix.sql (default: "./gt-checksum-DataFix.sql")
  --fixTrxNum value, --ftn value           Maximum number of concurrent transactions when repairing data. For example: --ftn 20 (default: 20)
  --help, -h                               show help
  --version, -v                           print the version
 

6、gt-checksum校验同步数据


# 数据库授权
# 想要运行gt-checksum工具,需要至少授予以下几个权限
# MySQL端
# 1.全局权限
# a.`REPLICATION CLIENT`
# b.`SESSION_VARIABLES_ADMIN`,如果是MySQL 8.0版本的话,MySQL 5.7版本不做这个要求
# 2.校验数据对象
# a.如果`datafix=file`,则只需要`SELECT`权限
# b.如果`datafix=table`,则需要`SELECT、INSERT、DELETE`权限,如果还需要修复表结构不一致的情况,则需要`ALTER`权限
#
# 假设现在要对db1.t1做校验和修复,则可授权如下

mysql> GRANT REPLICATION CLIENT, SESSION_VARIABLES_ADMIN ON *.* to ...;
mysql> GRANT SELECT, INSERT, DELETE ON db1.t1 to ...;

# Oracle端
# 1.全局权限
# a.`SELECT ANY DICTIONARY`
# 2.校验数据对象
# a.如果`datafix=file`,则只需要`SELECT ANY TABLE`权限
# b.如果`datafix=table`,则需要`SELECT ANY TABLE、INSERT ANY TABLE、DELETE ANY TABLE`权限


--MySQL同步校验,新建MySQL账号,基于文件校验

create user checksum@'%' identified with mysql_native_password by '123456';
grant replication client, session_variables_admin on *.* to checksum@'%';
grant select on mydb.* to checksum@'%';
flush privileges;

--查看gc配置文件

[root@go gt-checksum]# egrep -v '^$|^;' gc.conf            
[DSNs]
srcDSN = mysql|checksum:123456@tcp(192.168.100.81:3306)/information_schema?charset=utf8mb4
dstDSN = mysql|checksum:123456@tcp(192.168.100.82:3306)/information_schema?charset=utf8mb4
[Schema]
tables = mydb.*
ignore-tables =
checkNoIndexTable = no
lowerCaseTableNames = yes
[Rules]
parallel-thds = 10
chanRowCount = 10000
queue-size = 100
checkMode = rows
ratio = 10
checkObject = data
[Struct]
ScheckMod = strict
ScheckOrder = yes
ScheckFixRule = src
[Logs]
log = ./gt-checksum.log
logLevel = info
[Repair]
datafix = file
fixTrxNum = 100
fixFileName = ./gt-checksum-DataFix.sql



--执行
[root@go gt-checksum]# gt-checksum -f ./gc.conf
-- gt-checksum init configuration files --
-- gt-checksum init log files --
-- gt-checksum init check parameter --
-- gt-checksum init check table name --
-- gt-checksum init check table column --
-- gt-checksum init check table index column --
-- gt-checksum init source and dest transaction snapshoot conn pool --
-- gt-checksum init cehck table query plan and check data --
begin checkSum index table mydb.t2
[██████████           ]100% task:      10/10
table mydb.t2 checksum complete
begin checkSum index table mydb.t1
[██████████           ]100% task:      10/10
table mydb.t1 checksum complete

** gt-checksum Overview of results **
Check time:  3.20s (Seconds)
Schema Table   IndexCol       checkMod       Rows           Differences     Datafix
mydb   t2     id             rows            99750,99748     yes             file  
mydb   t1     id             rows            98968,99016     yes             file  




--基于命令行执行,--cm rows 模式可以生成修复的文件
[root@go gt-checksum]# gt-checksum -S type=mysql,user=checksum,passwd=123456,host=192.168.100.81,port=3306,charset=utf8 -D type=mysql,user=checksum,passwd=123456,host=192.168.100.82,port=3306,charset=utf8 -t mydb.* --sfr src --nit no --lc yes --df file --cm rows --ffn ./gt-checksum-DataFix.sql  
-- gt-checksum init configuration files --
-- gt-checksum init log files --
-- gt-checksum init check parameter --
-- gt-checksum init check table name --
-- gt-checksum init check table column --
-- gt-checksum init check table index column --
-- gt-checksum init source and dest transaction snapshoot conn pool --
-- gt-checksum init cehck table query plan and check data --
begin checkSum index table mydb.t1
[██████████           ]100% task:      10/10
table mydb.t1 checksum complete
begin checkSum index table mydb.t2
[██████████           ]100% task:      10/10
table mydb.t2 checksum complete

** gt-checksum Overview of results **
Check time:  2.67s (Seconds)
Schema Table   IndexCol       checkMod       Rows           Differences     Datafix
mydb   t1     id             rows            98968,99016     yes             file  
mydb   t2     id             rows            99750,99748     yes             file  


--查看修复的文件

[root@go gt-checksum]# more gt-checksum-DataFix.sql
begin;
insert into `mydb`.`t2` values('10000','f579b76a925352785476','de4f1f8a192ab85cc03c',date_format('2023-07-19 22:29:41','%Y-%m-%d %H:%i:%s'),date_for
mat('2023-07-19 22:29:41','%Y-%m-%d %H:%i:%s')) ;
insert into `mydb`.`t2` values('1','98a4e6342db33b0e8f77','b64759f1fa94ab47af3f',date_format('2023-07-19 22:29:37','%Y-%m-%d %H:%i:%s'),date_format(
'2023-07-19 22:29:37','%Y-%m-%d %H:%i:%s')) ;
commit;
begin;
insert into `mydb`.`t1` values('1','98a4e6342db33b0e8f77','b64759f1fa94ab47af3f',date_format('2023-07-19 22:29:37','%Y-%m-%d %H:%i:%s'),date_format(
'2023-07-19 22:29:37','%Y-%m-%d %H:%i:%s')) ;
insert into `mydb`.`t1` values('10000','f579b76a925352785476','de4f1f8a192ab85cc03c',date_format('2023-07-19 22:29:41','%Y-%m-%d %H:%i:%s'),date_for
mat('2023-07-19 22:29:41','%Y-%m-%d %H:%i:%s')) ;
commit;



--使用  --cm count   模式,只校验差异数据量
[root@go gt-checksum]# gt-checksum -S type=mysql,user=checksum,passwd=123456,host=192.168.100.81,port=3306,charset=utf8 -D type=mysql,user=checksum,passwd=123456,host=192.168.100.82,port=3306,charset=utf8 -t mydb.* --sfr src --nit no --lc yes --df file --cm count --ffn ./gt-checksum-DataFix.sql
-- gt-checksum init configuration files --
-- gt-checksum init log files --
-- gt-checksum init check parameter --
-- gt-checksum init check table name --
-- gt-checksum init check table column --
-- gt-checksum init check table index column --
-- gt-checksum init source and dest transaction snapshoot conn pool --
-- gt-checksum init cehck table query plan and check data --

** gt-checksum Overview of results **
Check time:  0.40s (Seconds)
Schema Table   checkObject     checkMod       Rows           Differences
mydb   t2     data           count           100000,99998    yes        
mydb   t1     data           count           100000,99998    yes    



--复制到从库,手动导入数据库
set sql_log_bin=0;
source gt-checksum-DataFix.sql;
set sql_log_bin=1;


--再次运行脚本,只校验count
gt-checksum -S type=mysql,user=checksum,passwd=123456,host=192.168.100.81,port=3306,charset=utf8 -D type=mysql,user=checksum,passwd=123456,host=192.168.100.82,port=3306,charset=utf8 -t mydb.* --sfr src --nit no --lc yes --df file --cm count --ffn ./gt-checksum-DataFix.sql



7、一直缺陷

截止最新的1.2.1版本中,当表中有多行数据是完全重复的话,可能会导致校验结果不准确,详见 已知缺陷





「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论