4.通过DataX实MySQL数据同步
1 、生成MySQL到MySQL同步的模板:
[root@MySQL-1 ~]# python /usr/local/datax/bin/datax.py -r mysqlreader -w mysqlwriter { "job": { "content": [ { "reader": { "name": "mysqlreader", # 读取端 "parameter": { "column": [], # 需要同步的列 (* 表示所有的列) "connection": [ { "jdbcUrl": [], # 连接信息 "table": [] # 连接表 } ], "password": "", # 连接用户 "username": "", # 连接密码 "where": "" # 描述筛选条件 } }, "writer": { "name": "mysqlwriter", # 写入端 "parameter": { "column": [], # 需要同步的列 "connection": [ { "jdbcUrl": "", # 连接信息 "table": [] # 连接表 } ], "password": "", # 连接密码 "preSql": [], # 同步前. 要做的事 "session": [], "username": "", # 连接用户 "writeMode": "" # 操作类型 } } } ], "setting": { "speed": { "channel": "" # 指定并发数 } } } }
2 、编写json
文件:
[root@MySQL-1 ~]# vim install.json { "job": { "content": [ { "reader": { "name": "mysqlreader", "parameter": { "username": "root", "password": "123123", "column": ["*"], "splitPk": "ID", "connection": [ { "jdbcUrl": [ "jdbc:mysql://192.168.1.1:3306/course-study?useUnicode=true&characterEncoding=utf8" ], "table": ["t_member"] } ] } }, "writer": { "name": "mysqlwriter", "parameter": { "column": ["*"], "connection": [ { "jdbcUrl": "jdbc:mysql://192.168.1.2:3306/course-study?useUnicode=true&characterEncoding=utf8", "table": ["t_member"] } ], "password": "123123", "preSql": [ "truncate t_member" ], "session": [ "set session sql_mode='ANSI'" ], "username": "root", "writeMode": "insert" } } } ], "setting": { "speed": { "channel": "5" } } } }
3 、验证
[root@MySQL-1 ~]# python /usr/local/datax/bin/datax.py install.json
输出:
2021-12-15 16:45:15.120 [job-0] INFO JobContainer - PerfTrace not enable! 2021-12-15 16:45:15.120 [job-0] INFO StandAloneJobContainerCommunicator - Total 2999999 records, 107666651 bytes | Speed 2.57MB/s, 74999 records/s | Error 0 records, 0 bytes | All Task WaitWriterTime 82.173s | All Task WaitReaderTime 75.722s | Percentage 100.00% 2021-12-15 16:45:15.124 [job-0] INFO JobContainer - 任务启动时刻 : 2021-12-15 16:44:32 任务结束时刻 : 2021-12-15 16:45:15 任务总计耗时 : 42s 任务平均流量 : 2.57MB/s 记录写入速度 : 74999rec/s 读出记录总数 : 2999999 读写失败总数 : 0
你们可以在目标数据库中进行查看,是否同步完成。
全部的方式是完全同步的,但是当数据量测时,是同步的时间被中断的很安慰的事情;
所以在某种情况下,增量同步还是蛮重要的。
5.使用DataX进行增量同步
使用DataX全量同步和增量同步的唯一区别是:增量同步需要使用where
进行条件筛选。 (即,同步进行筛选后的SQL)
1 、编写json
文件:
[root@MySQL-1 ~]# vim where.json { "job": { "content": [ { "reader": { "name": "mysqlreader", "parameter": { "username": "root", "password": "123123", "column": ["*"], "splitPk": "ID", "where": "ID <= 1888", "connection": [ { "jdbcUrl": [ "jdbc:mysql://192.168.1.1:3306/course-study?useUnicode=true&characterEncoding=utf8" ], "table": ["t_member"] } ] } }, "writer": { "name": "mysqlwriter", "parameter": { "column": ["*"], "connection": [ { "jdbcUrl": "jdbc:mysql://192.168.1.2:3306/course-study?useUnicode=true&characterEncoding=utf8", "table": ["t_member"] } ], "password": "123123", "preSql": [ "truncate t_member" ], "session": [ "set session sql_mode='ANSI'" ], "username": "root", "writeMode": "insert" } } } ], "setting": { "speed": { "channel": "5" } } } }
需要注意的就是:where(条件筛选)部分和preSql(同步前,做的事情)参数。
2 、验证:
[root@MySQL-1 ~]# python /usr/local/data/bin/data.py where.json
输出:
2021-12-16 17:34:38.534 [job-0] INFO JobContainer - PerfTrace not enable! 2021-12-16 17:34:38.534 [job-0] INFO StandAloneJobContainerCommunicator - Total 1888 records, 49543 bytes | Speed 1.61KB/s, 62 records/s | Error 0 records, 0 bytes | All Task WaitWriterTime 0.002s | All Task WaitReaderTime 100.570s | Percentage 100.00% 2021-12-16 17:34:38.537 [job-0] INFO JobContainer - 任务启动时刻 : 2021-12-16 17:34:06 任务结束时刻 : 2021-12-16 17:34:38 任务总计耗时 : 32s 任务平均流量 : 1.61KB/s 记录写入速度 : 62rec/s 读出记录总数 : 1888 读写失败总数 : 0
数据库上:
3 、基于以上数据,再次进行增量同步:
主要是 where 配置:"where": "ID > 1888 AND ID <= 2888" # 通过条件筛选来进行增量同步 同时需要将我上面的 preSql 删除(因为我上面做的操作时 truncate 表)