数据同步步骤:
按照网上的步骤下载部署好datax后,开始尝试数据同步:先准备好数据,任务为将test数据库的test1同步至testwriter数据库的test2.
datax目录为/usr/local/datax,datax.py目录为/usr/local/datax/bin下。由于此次数据同步连接的两个数据库均为mysql类型,先查看.json文件配置格式:
cd /usr/local/datax/bin //先进入data.py目录下
python datax.py -r mysqlreader -w mysqlwriter //查看.json文件配置格式
DataX (DATAX-OPENSOURCE-3.0), From Alibaba !
Copyright (C) 2010-2017, Alibaba Group. All Rights Reserved.
Please refer to the mysqlreader document:
https://github.com/alibaba/DataX/blob/master/mysqlreader/doc/mysqlreader.md
Please refer to the mysqlwriter document:
https://github.com/alibaba/DataX/blob/master/mysqlwriter/doc/mysqlwriter.md
Please save the following configuration as a json file and use
python {DATAX_HOME}/bin/datax.py {JSON_FILE_NAME}.json
to run the job.
{
"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": ""
}
}
}
}
注意要严格按照.json格式,reader的“jdbcUrl”有中括号[],writer的“jdbcUrl”没有中括号。
OK,此时可以复制.json格式,自建一个.json文件作修改了。我的是/usr/local/datax/jsonfile下的mysql2mysql.json
{
"job": {
"content": [
{
"reader": {
"name": "mysqlreader", //reader
"parameter": {
"column":["*"], //字段列表
"connection":[
{
"jdbcUrl":["jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false"], //数据库地址
"table":["test1"] //表名
}
],
"password":"12345678", //账号密码
"username":"root"
}
},
"writer": {
"name": "mysqlwriter", //writer,格式与reader相同
"parameter": {
"column":["*"],
"connection":[
{
"jdbcUrl":"jdbc:mysql://localhost:3306/testwriter?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false",
"table":["test2"]
}
],
"password":"12345678",
"username":"root",
"writeMode":"insert"
}
}
}
],
"setting": {
"speed": {
"channel": "5"
}
}
}
}
配置好.json后,回到data.py所在的bin目录下,开始同步。
python datax.py /usr/local/datax/jsonfile/mysql2mysql.json
显示已同步完成,此时再打开dbeaver,查看testwriter.test2中数据确实被同步了。
success!
常见报错及解决办法:
1. 经DataX智能分析,该任务最可能的错误原因是:
com.alibaba.datax.common.exception.DataXException: Code:[Framework-02], Description:[DataX引擎运行过程出错,具体原因请参看DataX运行结束时的错误诊断信息 .]. - java.lang.ClassCastException: class java.lang.String cannot be cast to class java.util.List (java.lang.String and java.util.List are in module java.base of loader 'bootstrap')
出现这类错误大概率问题在.json文件,检查格式是否有错误,jdbc链接是否正确等。可以新建一个终端窗口试试能否远程登录mysql来验证。
2. WARN DBUtil - test connection of [jdbc:mysql://127.0.0.1:3306/test] failed, for Code:[DBUtilErrorCode-10], Description:[连接数据库失败. 请检查您的 账号、密码、数据库名称、IP、Port或者向 DBA 寻求帮助(注意网络环境).]. - 具体错误信息为:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server..
2024-11-08 10:11:54.423 [job-0] ERROR RetryUtil - Exception when calling callable, 异常Msg:DataX无法连接对应的数据库,可能原因是:1) 配置的ip/port/database/jdbc错误,无法连接。2) 配置的username/password错误,鉴权失败。请和DBA确认该数据库的连接信息是否正确。
java.lang.Exception: DataX无法连接对应的数据库,可能原因是:1) 配置的ip/port/database/jdbc错误,无法连接。2) 配置的username/password错误,鉴权失败。请和DBA确认该数据库的连接信息是否正确。
出现这类错误是因为datax的lib中缺少了mysql的驱动文件,直接将mysql驱动文件copy到datax的lib目录下即可。
标签:同步,数据库,json,jdbc,DataX,mysql,数据,datax From: https://blog.csdn.net/weixin_43154489/article/details/143619574