Spring Boot - 无法连接 Azure MySQL 数据库

Posted

技术标签:

【中文标题】Spring Boot - 无法连接 Azure MySQL 数据库【英文标题】:Spring Boot - Unable to connect Azure MySQL database 【发布时间】:2020-01-11 07:44:27 【问题描述】:

我是从 Spring Boot 应用程序连接 Azure mysql 数据库的新手。

下面是application.properties中提到的sn-p

    spring.datasource.url=jdbc:mysql://XXXX.mysql.database.azure.com:3306/MyDbName
spring.datasource.username= ******
spring.datasource.password= ******

我遇到了以下异常

org.springframework.jdbc.support.MetaDataAccessException: Could not get Connection for extracting meta data; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Unknown system variable 'query_cache_size'

我用谷歌搜索过,有人提到需要将 MySQLConnector 的版本更新为8.0.13

请帮我找到解决办法。

【问题讨论】:

【参考方案1】:

根据您的异常,不是因为 Azure mysql。这是因为系统变量'query_cache_size'。参考:java.sql.SQLException: Unknown system variable 'query_cache_size'

【讨论】:

【参考方案2】:

我也曾在 azure MySQL 上遇到过这种快乐。正如 dvo 提到的,当它认为它在版本 8.0.3 下时,它会放置这个变量。

来自 com.mysql.cj.NativeSession.java...

if (!versionMeetsMinimum(8, 0, 3)) 
    queryBuf.append(", @@query_cache_size AS query_cache_size");
    queryBuf.append(", @@query_cache_type AS query_cache_type");

但由于我的 Azure MySQL 版本是 8.0.15,它与我的驱动程序版本匹配,所以我怀疑数据库可能是驱动程序处理的堆栈的版本 8 部分。

编辑:

来自https://docs.microsoft.com/en-us/azure/mysql/concepts-limits

当前已知问题

建立连接后,MySQL 服务器实例显示错误的服务器版本。要获取正确的服务器实例引擎版本,请使用 select version();命令。

所以我猜这是问题所在,目前 Azure MySQL 8 无法与 java mysql 连接器一起使用。

我可能会考虑自己构建连接器并将版本号硬编码为 8.0.15。多么快乐。

编辑编辑:

修改 ServerVersion.java parseVersion(…) 以返回

return new ServerVersion(8, 0, 15);

解决问题的方法足以让我继续我的工作。

这是微软应该尽快解决的问题。

【讨论】:

以上是关于Spring Boot - 无法连接 Azure MySQL 数据库的主要内容,如果未能解决你的问题,请参考以下文章

无法将 Spring Boot 应用程序部署到 Azure

在 Azure 应用服务上部署 Spring Boot jar

如果我们保持空闲一段时间,则在连接到 Azure MSSQL DB 时,Spring Boot 应用程序中的数据库连接将关闭

在 Azure 中部署 Gradle Spring Boot Rest Service 应用程序

Azure App Service - Spring Boot 应用程序在启动时卡住

无法用spring boot连接mysql