ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

【Azure 应用服务】App Service 无法连接到Azure MySQL服务,报错:com.mysql.cj.jdbc.exceptions.CommunicationsException:

2021-11-25 21:32:27  阅读:236  来源: 互联网

标签:cj jdbc cn 8.0 MySQL server 报错 mysql Azure


问题描述

App Service使用jdbc连接MySQL服务,出现大量的  Communications link failure:

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server

 

问题分析

最开始出现连接不上的原因,怀疑是MySQL服务的IP地址白名单没有配置正确的App Service 出站IP。但是根据错误提示,发现明显不对,因为如果是IP地址不允许访问,它的错误消息应该是:

Client with IP address '183.2xx.xx.xx' is not allowed to connect to this MySQL server.

所以不应该是MySQL服务器对IP白名单设置的问题。

 

为了深入找出问题,单独用Java Spring Boot来写一个简单的数据库连接代码,根据MySQL官方的代码:https://docs.azure.cn/zh-cn/mysql/connect-java

POM.XML内容为:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>

    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.20</version>
        </dependency>
    </dependencies>
</project>

注:这里使用的 mysql-connector-java 依赖版本为 8.0.20 

Java Main函数代码:

    public static void main(String[] args) {
        String url = "jdbc:mysql://xxxxxx.mysql.database.chinacloudapi.cn:3306/xxxxxx?useSSL=true&requireSSL=false&characterEncoding=utf8&serverTimezone=UTC";
        String username = "xxxxxx@xxxxxx";
        String password = "xxxxxxxxxxxx";
        try {
            Connection connection = DriverManager.getConnection(url, username, password);
            System.out.println(connection.getMetaData().getURL());
            connection.close();
            System.out.println("connected!");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            System.out.println(e.getMessage());
            return; // return because nothing can be done w/out a connection
        }

    }

同样的数据库连接字符串,运行结果正常。根据验证结果对比,出现 Communications link failure |  The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server, 的JDBC版本为 8.0.15, 所以瞬间定位问题原因:客户端jdbc的驱动问题。

 

根据这一判断,再次检查了MySQL的版本为8.0并且为单实例(Single Server)。而微软的官方文档中提示了 use 8.0.17+ with MySQL 8.0 (https://docs.microsoft.com/en-us/azure/mysql/concepts-compatibility#mysql-drivers)

 

随后,对mysql-connector jdbc的版本 8.0.17, 8.0.18, 8.0.19 这三个版本进行验证,看是否不会出现 Communications link failure 异常:

8.0.17 Error

8.0.18 Error

8.0.19 Successful

 

 

经过以上验证,得出微软官方文档中的 8.0.17+有问题,需要在8.0.19+的驱动版本后才能成功。(一个调查了三天的大坑)

 

参考资料

将 Java 和 JDBC 与 Azure Database for MySQL 配合使用 : https://docs.azure.cn/zh-cn/mysql/connect-java

 

标签:cj,jdbc,cn,8.0,MySQL,server,报错,mysql,Azure
来源: https://www.cnblogs.com/lulight/p/15604596.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有