TiDB学习之 MySQL数据同步

1、首先用check检查mysql数据库表是否支持迁移到TiDB
2、使用mydumper和myloader全量导入数据,mydumper从MySQL导出数据,然后myloader将数据导入TiDB
(注意,虽然我们也支持使用 MySQL 官方的 mysqldump 工具来进行数据的迁移工作,但相比于 mydumper/myloader,性能会慢很多,对于大量数据的迁移会花费很多时间,这里我们并不推荐。mydumper/myloader 是一个更强大的数据迁移工具)

使用 syncer 增量导入数据实现数据和 MySQL 实时同步
上面我们介绍了如何使用 mydumper/myloader 将 MySQL 的数据全量导入到 TiDB,但如果后续 MySQL 的数据有更新,我们仍然希望快速导入,使用全量的方式就不合适了。

TiDB 提供 syncer 工具能方便的将 MySQL 的数据增量的导入到 TiDB 里面
假设我们之前已经使用 mydumper/myloader 导入了 t1 和 t2 两张表的一些数据,现在我们希望这两张表的任何更新,都是实时的同步到 TiDB 上面

MySQL 开启 binlog
在使用 syncer 之前,我们必须保证:

  • MySQL 开启 binlog 功能,参考 Setting the Replication Master Configuration
  • Binlog 格式必须使用 row format,这也是 MySQL 5.7 之后推荐的 binlog 格式,可以使用如下语句打开:
SET GLOBAL binlog_format = ROW;

获取同步 position
我们通过 show master status 得到当前 binlog 的 position,syncer 的初始同步位置就是从这个地方开始

show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 |     1280 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+

我们将 position 相关的信息保存到一个 syncer.meta 文件里面,用于 syncer 的同步:

# cat syncer.meta
binlog-name = "mysql-bin.000003"
binlog-pos = 1280

注意:syncer.meta 只需要第一次使用的时候配置,后续 syncer 同步新的 binlog 之后会自动将其更新到最新的 position。

启动 syncer
syncer 的配置文件 config.toml:

log-level = "info"

server-id = 101

# meta 文件地址
meta = "./syncer.meta"
worker-count = 1
batch = 1

pprof-addr = ":10081"

[from]
host = "127.0.0.1"
user = "root"
password = ""
port = 3306

[to]
host = "127.0.0.1"
user = "root"
password = ""
port = 4000

启动 syncer:

./bin/syncer -config config.toml

2016/10/27 15:22:01 binlogsyncer.go:226: [info] begin to sync binlog from position (mysql-bin.000003, 1280)
2016/10/27 15:22:01 binlogsyncer.go:130: [info] register slave for master server 127.0.0.1:3306
2016/10/27 15:22:01 binlogsyncer.go:552: [info] rotate to (mysql-bin.000003, 1280)
2016/10/27 15:22:01 syncer.go:549: [info] rotate binlog to (mysql-bin.000003, 1280)

在 MySQL 插入新的数据

INSERT INTO t1 VALUES (4, 4), (5, 5);

登录到 TiDB 查看:

mysql -h127.0.0.1 -P4000 -uroot -p
mysql> select * from t1;
+----+------+
| id | age  |
+----+------+
|  1 |    1 |
|  2 |    2 |
|  3 |    3 |
|  4 |    4 |
|  5 |    5 |
+----+------+

syncer 每隔 30s 会输出当前的同步统计,如下

update = 0, delete = 0, total tps = 0, recent tps = 0, binlog name = mysql-bin.000003, binlog pos = 1280.
2016/10/27 15:23:01 syncer.go:668: [info] [syncer]total events = 2, insert = 2, update = 0, delete = 0, total tps = 0, recent tps = 0, binlog name = mysql-bin.000003, binlog pos = 1538.

可以看到,使用 syncer,我们就能自动的将 MySQL 的更新同步到 TiDB

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值