/opt/module/dataX
cd /opt/module/dataX
python bin/datax.py -r mysqlreader -w mysqlwriter
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": ""
}
}
}
}
具体配置项目说明参见官网 https://github.com/alibaba/DataX
vim job/mysql2mysql.json
粘贴以上json数据部分
修改配置文件
{
"job": {
"content": [
{
"reader": {
"name": "mysqlreader",
"parameter": {
"column": [
"etpsname",
"etpsno"
],
"connection": [
{
"jdbcUrl": [
"jdbc:mysql://***.***.***.***:3306/bigdata?useUnicode=true&characterEncoding=utf8"
],
"table": [
"etps_no"
]
}
],
"password": "密码",
"username": "cigar",
"where": "id <=1000 "
}
},
"writer": {
"name": "mysqlwriter",
"parameter": {
"column": [
"etps_name",
"etps_no"
],
"connection": [
{
"jdbcUrl": "jdbc:mysql://192.168.0.102:3306/datax?useUnicode=true&characterEncoding=utf8",
"table": [
"etps_no"
]
}
],
"password": "root",
"preSql": [],
"session": [],
"username": "root",
"writeMode": "insert"
}
}
}
],
"setting": {
"speed": {
"channel": "1"
}
}
}
}
python bin/datax.py job/mysql2mysql.json
2020-03-09 15:40:00.703 [job-0] WARN DBUtil - test connection of [jdbc:mysql://122.51.161.234:3306/bigdata?useUnicode=true&characterEncoding=utf8] failed, for Code:[DBUtilErrorCode-10], Description:[连接数据库失败. 请检查您的 账号、密码、数据库名称、IP、Port或者向 DBA 寻求帮助(注意网络环境).]. - 具体错误信息为:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server..
2020-03-09 15:40:00.708 [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确认该数据库的连接信息是否正确。
at com.alibaba.datax.plugin.rdbms.util.DBUtil$2.call(DBUtil.java:71) ~[plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.DBUtil$2.call(DBUtil.java:51) ~[plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.common.util.RetryUtil$Retry.call(RetryUtil.java:164) ~[datax-common-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.common.util.RetryUtil$Retry.doRetry(RetryUtil.java:111) ~[datax-common-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.common.util.RetryUtil.executeWithRetry(RetryUtil.java:30) [datax-common-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.DBUtil.chooseJdbcUrl(DBUtil.java:51) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.reader.util.OriginalConfPretreatmentUtil.dealJdbcAndTable(OriginalConfPretreatmentUtil.java:92) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.reader.util.OriginalConfPretreatmentUtil.simplifyConf(OriginalConfPretreatmentUtil.java:59) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.reader.util.OriginalConfPretreatmentUtil.doPretreatment(OriginalConfPretreatmentUtil.java:33) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.reader.CommonRdbmsReader$Job.init(CommonRdbmsReader.java:55) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.reader.mysqlreader.MysqlReader$Job.init(MysqlReader.java:37) [mysqlreader-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.core.job.JobContainer.initJobReader(JobContainer.java:673) [datax-core-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.core.job.JobContainer.init(JobContainer.java:303) [datax-core-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.core.job.JobContainer.start(JobContainer.java:113) [datax-core-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.core.Engine.start(Engine.java:92) [datax-core-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.core.Engine.entry(Engine.java:171) [datax-core-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.core.Engine.main(Engine.java:204) [datax-core-0.0.1-SNAPSHOT.jar:na]
public class ConnMysql {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
connToMysql();
}
public static void connToMysql() throws ClassNotFoundException, SQLException {
// 加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 获得连接
Connection connection = DriverManager.getConnection("jdbc:mysql://***.***.***.***:3306/bigdata?useUnicode=true&characterEncoding=utf8", "cigar", "密码");
// 预处理SQL
String sql = "select etpsname,etpsno from etps_no limit 100";
Statement statement = connection.createStatement();
// 执行语句
ResultSet qu = statement.executeQuery(sql);
// 处理结果
while (qu.next()){
String etpsName = qu.getString("etpsname");
String etpsNo = qu.getString("etpsno");
System.out.println(etpsName + " => " + etpsNo);
}
// 关闭资源
statement.close();
connection.close();
}
}
以上代码可以获取到连接成功,并且查询到结果,说明JDBC连接没问题,需要从其他地方寻找错误。
查看MySQL驱动包
ll /opt/module/dataX/plugin/reader/mysqlreader/libs
-rw-r--r--. 1 62265 users 518641 11月 24 2017 commons-collections-3.0.jar
-rw-r--r--. 1 62265 users 185140 11月 24 2017 commons-io-2.4.jar
-rw-r--r--. 1 62265 users 412739 11月 24 2017 commons-lang3-3.3.2.jar
-rw-r--r--. 1 62265 users 1599627 11月 24 2017 commons-math3-3.1.1.jar
-rw-r--r--. 1 62265 users 95053 11月 24 2017 datax-common-0.0.1-SNAPSHOT.jar
-rw-r--r--. 1 62265 users 1952759 11月 24 2017 druid-1.0.15.jar
-rw-r--r--. 1 62265 users 372746 11月 24 2017 fastjson-1.1.46.sec01.jar
-rw-r--r--. 1 62265 users 934783 11月 24 2017 guava-r05.jar
-rw-r--r--. 1 62265 users 45024 11月 24 2017 hamcrest-core-1.3.jar
-rw-r--r--. 1 62265 users 264600 11月 24 2017 logback-classic-1.0.13.jar
-rw-r--r--. 1 62265 users 418870 11月 24 2017 logback-core-1.0.13.jar
-rw-r--r--. 1 62265 users 960372 11月 24 2017 mysql-connector-java-5.1.34.jar
-rw-r--r--. 1 62265 users 95960 11月 24 2017 plugin-rdbms-util-0.0.1-SNAPSHOT.jar
-rw-r--r--. 1 62265 users 32119 11月 24 2017 slf4j-api-1.7.10.jar
mysql-connector-java-5.1.34.jar
包替换为 mysql-connector-java-8.0.17.jar
ll /opt/module/dataX/plugin/reader/mysqlreader/libs
-rw-r--r--. 1 62265 users 518641 11月 24 2017 commons-collections-3.0.jar
-rw-r--r--. 1 62265 users 185140 11月 24 2017 commons-io-2.4.jar
-rw-r--r--. 1 62265 users 412739 11月 24 2017 commons-lang3-3.3.2.jar
-rw-r--r--. 1 62265 users 1599627 11月 24 2017 commons-math3-3.1.1.jar
-rw-r--r--. 1 62265 users 95053 11月 24 2017 datax-common-0.0.1-SNAPSHOT.jar
-rw-r--r--. 1 62265 users 1952759 11月 24 2017 druid-1.0.15.jar
-rw-r--r--. 1 62265 users 372746 11月 24 2017 fastjson-1.1.46.sec01.jar
-rw-r--r--. 1 62265 users 934783 11月 24 2017 guava-r05.jar
-rw-r--r--. 1 62265 users 45024 11月 24 2017 hamcrest-core-1.3.jar
-rw-r--r--. 1 62265 users 264600 11月 24 2017 logback-classic-1.0.13.jar
-rw-r--r--. 1 62265 users 418870 11月 24 2017 logback-core-1.0.13.jar
-rw-r--r--. 1 62265 users 960372 11月 24 2017 mysql-connector-java-5.1.34.jar.bak
-rw-r--r--. 1 root root 2321813 3月 9 00:54 mysql-connector-java-8.0.17.jar
-rw-r--r--. 1 62265 users 95960 11月 24 2017 plugin-rdbms-util-0.0.1-SNAPSHOT.jar
-rw-r--r--. 1 62265 users 32119 11月 24 2017 slf4j-api-1.7.10.jar
python bin/datax.py job/mysql2mysql.json
2020-03-09 01:29:50.995 [job-0] ERROR RetryUtil - Exception when calling callable, 异常Msg:Code:[MYSQLErrCode-01], Description:[数据库用户名或者密码错误,请检查填写的账号密码或者联系DBA确认账号和密码是否正确]. - 该数据库用户名为:root 具体错误信息为:java.sql.SQLException: Access denied for user 'root'@'DESKTOP-OEAO22T' (using password: YES)
com.alibaba.datax.common.exception.DataXException: Code:[MYSQLErrCode-01], Description:[数据库用户名或者密码错误,请检查填写的账号密码或者联系DBA确认账号和密码是否正确]. - 该数据库用户名为:root 具体错误信息为:java.sql.SQLException: Access denied for user 'root'@'DESKTOP-OEAO22T' (using password: YES)
at com.alibaba.datax.common.exception.DataXException.asDataXException(DataXException.java:26) ~[datax-common-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.RdbmsException.asConnException(RdbmsException.java:21) ~[plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.DBUtil.connect(DBUtil.java:394) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.DBUtil.connect(DBUtil.java:384) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.DBUtil.access$000(DBUtil.java:22) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.DBUtil$3.call(DBUtil.java:322) ~[plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.DBUtil$3.call(DBUtil.java:319) ~[plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.common.util.RetryUtil$Retry.call(RetryUtil.java:164) ~[datax-common-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.common.util.RetryUtil$Retry.doRetry(RetryUtil.java:111) ~[datax-common-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.common.util.RetryUtil.executeWithRetry(RetryUtil.java:30) [datax-common-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.DBUtil.getConnection(DBUtil.java:319) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.DBUtil.getConnection(DBUtil.java:303) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.JdbcConnectionFactory.getConnecttion(JdbcConnectionFactory.java:27) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.writer.util.OriginalConfPretreatmentUtil.dealColumnConf(OriginalConfPretreatmentUtil.java:105) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.writer.util.OriginalConfPretreatmentUtil.dealColumnConf(OriginalConfPretreatmentUtil.java:140) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.writer.util.OriginalConfPretreatmentUtil.doPretreatment(OriginalConfPretreatmentUtil.java:35) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.writer.CommonRdbmsWriter$Job.init(CommonRdbmsWriter.java:41) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.writer.mysqlwriter.MysqlWriter$Job.init(MysqlWriter.java:31) [mysqlwriter-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.core.job.JobContainer.initJobWriter(JobContainer.java:704) [datax-core-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.core.job.JobContainer.init(JobContainer.java:304) [datax-core-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.core.job.JobContainer.start(JobContainer.java:113) [datax-core-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.core.Engine.start(Engine.java:92) [datax-core-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.core.Engine.entry(Engine.java:171) [datax-core-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.core.Engine.main(Engine.java:204) [datax-core-0.0.1-SNAPSHOT.jar:na]
通过关键词定位,发现DataX程序在写入时连接到本机上的MySQL被拒绝,说明网络通畅,是被MySQL的权限拒绝了。联想到之前使用Linux环境下的MySQL8.0,MySQL8.0版本对权限控制即为严格,root用户在默认情况下仅允许本地登录
mysql -uroot -proot
mysql> use mysql;
mysql> select user,host from user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
以上结果表明,root用户仅允许在localhost登录,不允许远程登录,造成DataX在写入时连接失败。
此处选用方案二
mysql> create user zhangsan identified by '密码';
mysql> select user,host from user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| zhangsan | % |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
mysql> alter user 'zhangsan'@'%' identified with mysql_native_password by '密码';
mysql> grant all privileges on *.* to cigar@'%';
说明:此处为方便,赋予了新用户所有权限,使其相当于root用户。为了安全起见,仅赋予其基本增删改查权限即可。
"writer": {
"name": "mysqlwriter",
"parameter": {
"column": [
"etps_name",
"etps_no"
],
"connection": [
{
"jdbcUrl": "jdbc:mysql://192.168.0.102:3306/datax?useUnicode=true&characterEncoding=utf8",
"table": [
"etps_no"
]
}
],
"password": "密码",
"preSql": [
"truncate table etps_no;"
],
"session": [],
"username": "zhangsan",
"writeMode": "insert"
}
}
python bin/datax.py job/mysql2mysql.json
com.alibaba.datax.common.exception.DataXException: Code:[DBUtilErrorCode-10], Description:[连接数据库失败. 请检查您的 账号、密码、数据库名称、IP、Port或者向 DBA 寻求帮助(注意网络环境).]. - 具体错误信息为:java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
at com.alibaba.datax.common.exception.DataXException.asDataXException(DataXException.java:26) ~[datax-common-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.RdbmsException.asConnException(RdbmsException.java:23) ~[plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.DBUtil.connect(DBUtil.java:394) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.DBUtil.connect(DBUtil.java:384) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.DBUtil.access$000(DBUtil.java:22) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.DBUtil$3.call(DBUtil.java:322) ~[plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.DBUtil$3.call(DBUtil.java:319) ~[plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.common.util.RetryUtil$Retry.call(RetryUtil.java:164) ~[datax-common-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.common.util.RetryUtil$Retry.doRetry(RetryUtil.java:111) ~[datax-common-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.common.util.RetryUtil.executeWithRetry(RetryUtil.java:30) [datax-common-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.DBUtil.getConnection(DBUtil.java:319) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.DBUtil.getConnection(DBUtil.java:303) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.JdbcConnectionFactory.getConnecttion(JdbcConnectionFactory.java:27) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.writer.util.OriginalConfPretreatmentUtil.dealColumnConf(OriginalConfPretreatmentUtil.java:105) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.writer.util.OriginalConfPretreatmentUtil.dealColumnConf(OriginalConfPretreatmentUtil.java:140) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.writer.util.OriginalConfPretreatmentUtil.doPretreatment(OriginalConfPretreatmentUtil.java:35) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.writer.CommonRdbmsWriter$Job.init(CommonRdbmsWriter.java:41) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.writer.mysqlwriter.MysqlWriter$Job.init(MysqlWriter.java:31) [mysqlwriter-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.core.job.JobContainer.initJobWriter(JobContainer.java:704) [datax-core-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.core.job.JobContainer.init(JobContainer.java:304) [datax-core-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.core.job.JobContainer.start(JobContainer.java:113) [datax-core-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.core.Engine.start(Engine.java:92) [datax-core-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.core.Engine.entry(Engine.java:171) [datax-core-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.core.Engine.main(Engine.java:204) [datax-core-0.0.1-SNAPSHOT.jar:na]
通过关键词定位发现,可能是时区问题,由于之前遇到过类似问题,仅需要在jdbc连接参数上加上时区即可
编辑writer的jdbc连接
"writer": {
"name": "mysqlwriter",
"parameter": {
"column": [
"etps_name",
"etps_no"
],
"connection": [
{
"jdbcUrl": "jdbc:mysql://192.168.0.102:3306/datax?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC",
"table": [
"etps_no"
]
}
],
"password": "密码",
"preSql": [
"truncate table etps_no;"
],
"session": [],
"username": "zhangsan",
"writeMode": "insert"
}
}
python bin/datax.py job/mysql2mysql.json
2020-03-09 01:34:02.251 [job-0] INFO JobContainer -
任务启动时刻 : 2020-03-09 01:33:49
任务结束时刻 : 2020-03-09 01:34:02
任务总计耗时 : 12s
任务平均流量 : 1.76KB/s
记录写入速度 : 100rec/s
读出记录总数 : 1000
读写失败总数 : 0
BinGo!终于执行成功!
mysql-connector-java-5.1.6.jar
,可以连接成功并获取到数据,mysql-connector-java-5.1.34.jar
,连接数据库失败