FlinkSql连接Mysql出现com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure.The last packet successfully received from the server was 23 milliseconds ago. The last packet sent successfully to the server was 15 milliseconds ago.
Caused by: java.lang.IllegalArgumentException: open() failed.Communications link failure
The last packet successfully received from the server was 23 milliseconds ago. The last packet sent successfully to the server was 15 milliseconds ago.at org.apache.flink.connector.jdbc.JdbcInputFormat.openInputFormat(JdbcInputFormat.java:167)
at org.apache.flink.streaming.api.functions.source.InputFormatSourceFunction.run(InputFormatSourceFunction.java:79)
at org.apache.flink.streaming.api.operators.StreamSource.run(StreamSource.java:110)
at org.apache.flink.streaming.api.operators.StreamSource.run(StreamSource.java:66)
at org.apache.flink.streaming.runtime.tasks.SourceStreamTask$LegacySourceFunctionThread.run(SourceStreamTask.java:241)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failureThe last packet successfully received from the server was 23 milliseconds ago. The last packet sent successfully to the server was 15 milliseconds ago.
原因:可能是账号、密码、url、driver错误
博主的原因是:Mysql版本是5.7,而Mysql的Connector使用了Mysql-5.1.44,但出现错误。
# Mysql
Mysql 5.7
# jar
mysql-connector-java-5.1.44.jar
# Driver
com.mysql.jdbc.Driver
# url
jdbc:mysql://192.168.88.101:3306/test
解决办法:使用Mysql 6
以上的jar驱动(并加上时区
)
pom
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
code
String sql = "CREATE TABLE flink_table (id string) " +
"WITH (" +
"'connector.type' = 'jdbc'," +
"'connector.url' = 'jdbc:mysql://192.168.88.101:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8'," +
"'connector.driver' = 'com.mysql.cj.jdbc.Driver'," +
"'connector.table' = 'test'," +
" 'connector.username' = 'root'," +
" 'connector.password' = '123456'" +
" )";