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

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
云数据库 RDS PostgreSQL,高可用系列 2核4GB
简介: 【Azure 应用服务】App Service 无法连接到Azure MySQL服务,报错:com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

问题描述

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+的驱动版本后才能成功。(一个调查了三天的大坑)

 

附录一:JDBC Connector TLS 版本问题

老版本的JDBC connector存在几个issue。在老版本的connector上,这两项很明显冲突的配置可能会同时存在,结果就导致在连接开始阶段直接失败。

第一个issue是老版本的connector默认没有enable TLS 1.2,

第二个问题是这些老版本的connector在某些情况下还会错误的禁止使用TLS1.2以下的版本。

MySQL Connector版本的发布日志中发现,MySQL Driver 8.0.19及以后的版本中有关于TLS改变:https://dev.mysql.com/doc/relnotes/connector-j/8.0/en/news-8-0-19.html

 

简单的解决方案是在连接串中启用TLS1.2(enabledTLSProtocols=TLSv1.2),也可以升级connector版本

 

参考资料

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

相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
2月前
|
测试技术 Linux 网络安全
【App Services】App Service报错远程证书无效 - "The remote certificate is invalid according to the validation procedure"
在开发环境中,新部署的应用(App Service)无法与 Salesforce 的远程端点建立 SSL/TLS 连接,报错显示证书无效。经分析,防火墙启用了 SSL Inspection,插入了私有 CA 签发的中间证书,导致 App Service 无法验证。解决方案包括禁用 SSL Inspection、设置 `WEBSITE_LOAD_ROOT_CERTIFICATES` 环境变量或临时禁用代码中的 SSL 验证(仅限测试环境)。
|
9月前
|
JSON 小程序 JavaScript
uni-app开发微信小程序的报错[渲染层错误]排查及解决
uni-app开发微信小程序的报错[渲染层错误]排查及解决
1985 7
|
6月前
|
监控 关系型数据库 MySQL
|
7月前
|
人工智能 自然语言处理 前端开发
从0开始打造一款APP:前端+搭建本机服务,定制暖冬卫衣先到先得
通义灵码携手科技博主@玺哥超carry 打造全网第一个完整的、面向普通人的自然语言编程教程。完全使用 AI,再配合简单易懂的方法,只要你会打字,就能真正做出一个完整的应用。
10984 29
|
7月前
|
人工智能 自然语言处理 芯片
上千人挑战,用通义灵码从 0 开始打造一款 App 爆火 | 第二课:搭建本机服务
通义灵码携手科技博主@玺哥超carry 打造全网第一个完整的、面向普通人的自然语言编程教程。完全使用 AI,再配合简单易懂的方法,只要你会打字,就能真正做出一个完整的应用。
1304 9
|
8月前
|
C#
【Azure App Service】使用Microsoft.Office.Interop.Word来操作Word文档,部署到App Service后报错COMException
System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (0x80040154 (REGDB_E_CLASSNOTREG)).
134 2
|
11月前
|
开发框架 .NET Windows
【App Service】在App Service中配置Virtual applications and directories,访问目录中的静态文件报错404
【App Service】在App Service中配置Virtual applications and directories,访问目录中的静态文件报错404

推荐镜像

更多