TiDB实战篇-Sync-Diff-Inspector校验数据

简介

使用Sync-Diff-Inspector校验数据。

功能

原理

  • 表数据根据特定的键进行排序,然后切分成一个个chunk。
  • 源端的chunk和目标端切分的chunk(因为他们数据排序方式默认是相同的,那么就可以根据特定的数据端进行校验)进行checksum的比对(简单点就是校验码的比对)。
  • 如果它们的checksum不相同,那么他们会根据自身切分成更加小的chunk,进行数据不相同地方的chunk定位。
  • 下面的checkpoint是记录比对到哪一个chunk了,用于任务失败以后的断点续比。

执行任务需要的权限

使用限制

下载安装

下载

TiDB 社区版 | PingCAP

安装

#解压下载好的安装包
tar -zxvf tidb-community-toolkit-v6.5.0-linux-amd64.tar.gz
#进入到文件夹以后解压出dumpling
cd tidb-community-toolkit-v6.5.0-linux-amd64

vi /etc/profile.d/my.sh
#TOOLKIT_HOME
export TOOLKIT_HOME=/root/tidb-community-toolkit-v6.5.0-linux-amd64
export PATH=$PATH:$TOOLKIT_HOME
source /etc/profile.d/my.sh

实战

sync-diff-inspector 用户文档 | PingCAP 文档中心

下面的目的是拿TiDB的test1库的所有表和MySQL的test1库下面所有的表做校验,并且输出修复文件。修复文件的内容是把TiDB的test1库里面的内容修复成和MySQL里面的test1库里面的内容一样,少增多减。

数据准备 

#数据准备
----------mysql-------
mysql -uroot -proot -h127.0.0.1 -P 3306

create database test1;
use test1;
create table T1 (id int);
insert into T1 values(1);

mysql> use test1;
Database changed
mysql> create table T1 (id int);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into T1 values(1);
Query OK, 1 row affected (0.03 sec)

mysql> select * from T1;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)


----------tidb---------
mysql -uroot -ptidb -h127.0.0.1 -P 4000

create database test1;
use test1;
create table T1 (id int);


mysql> create database test1;
Query OK, 0 rows affected (0.14 sec)

mysql> use test1;
Database changed
mysql> create table T1 (id int);
Query OK, 0 rows affected (0.15 sec)

mysql> select * from T1;
Empty set (0.01 sec)

相关配置文件

#写配置文件
vi sync-test-datebase.toml

# Diff Configuration.

######################### Global config #########################
check-thread-count = 4
export-fix-sql = true
check-struct-only = false

######################### Datasource config #########################
[data-sources]
[data-sources.mysql1] 
    host = "127.0.0.1"
    port = 3306
    user = "root"
    password = "root" 
    route-rules = ["rule1"]

[data-sources.tidb0]
    host = "127.0.0.1"
    port = 4000
    user = "root"
    password = "tidb"


########################### Routes ###########################
[routes]
[routes.rule1]
schema-pattern = "test1"      # 匹配数据源的库名
target-schema = "test1"         # 目标库名


######################### Task config #########################
[task]
    output-dir = "./output"
    source-instances = ["mysql1"]
    target-instance = "tidb0"
    target-check-tables = ["test1.*"] #这里配置的是routes里面的target-schema

执行 

sync_diff_inspector --config=./sync-test-datebase.toml

打印

A total of 1 tables need to be compared


Comparing the table structure of ``test1`.`T1`` ... equivalent
Comparing the table data of ``test1`.`T1`` ... failure
_____________________________________________________________________________
Progress [============================================================>] 100% 0/0
The data of `test1`.`T1` is not equal

The rest of tables are all equal.
The patch file has been generated in 
	'output/fix-on-tidb0/'
You can view the comparision details through './output/sync_diff.log'

注意事项

建议使用的时候先收集统计信息的目的是因为,表的一些基础信息在统计信息里面,比如表的索引什么的。 

开启sharding校验的效果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

工作变成艺术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值