Problem Description:
When using DataX to migrate MySQL 8.0, a connection failure is reported:
RetryUtil - Exception when calling callable, 即将尝试执行第1次重试.本次重试计划等待[1000]ms,实际等待[1000]ms,
异常Msg:[Code:[DBUtilErrorCode-10], Description:[连接数据库失败. 请检查您的 账号、密码、数据库名称、IP、Port或者向 DBA 寻求帮助(注意网络环境).]. -
具体错误信息为:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
Cause Analysis:
According to the last line of the exception log, com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.]
it can be determined that the root problem is caused by the MySQL driver not supporting it.
By looking at the lib folder of the mysql plug-in mysqlwriter/mysqlreader
, we can see that DataX is currently using the mysql driver mysql-connector-java-5.1.34.jar
, which does not support mysql8 connections.
solution:
Change the mysql driver version, modify some codes, and reload DataX
- Download the DataX source code. Get the source code
bygit clone
or downloading datax-master.zip directly. - Change the plug-in's pom dependency and upgrade the mysql driver.
Since I only use the mysqlwriter plug-in here, I only modify the pom.xml of the reading plug-in.
vi DataX-master\mysqlwriter\pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
By viewing the source code, MysqlWriter.java was introduced com.alibaba.datax.plugin.rdbms.util.DataBaseType
, so the DataBaseType needs to be modified.
- Modify the pom.xml of rdbms and upgrade the MySQL driver
vi DataX-master\plugin-rdbms-util\pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
- Modify
DataBaseType.java
the source code.
There are two modifications involved here.
One is tocom.mysql.jdbc.Driver
change it tocom.mysql.cj.jdbc.Driver
; the other is to change
the connection attribute to ;zeroDateTimeBehavior=convertToNull
zeroDateTimeBehavior=CONVERT_TO_NULL
vi DataX-master\plugin-rdbms-util\src\main\java\com\alibaba\datax\plugin\rdbms\util
- maven recompile DataX
cd DataX-master
mvn -U clean package assembly:assembly -Dmaven.test.skip=true
- Copying the recompiled plug-in to DataX
mainly involves copying three jar packages:
$ cd DataX-master\mysqlwriter\target\datax\plugin\writer\mysqlwriter\libs
$ ls
mysql-connector-java-8.0.19.jar
plugin-rdbms-util-0.0.1-SNAPSHOT.jar
Copy the above two jar packages to the originally installed DataX directory /datax/plugin/writer/mysqlwriter/libs
.
$ cd DataX-master\mysqlwriter\target\datax\plugin\writer\mysqlwriter
$ ls
mysqlwriter-0.0.1-SNAPSHOT.jar
Copy the above jar package to /datax/plugin/writer/mysqlwriter
the directory where DataX was originally installed.
- Verify:
Write a mysqljob task and execute it