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