You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot连接Azure MySQL失败求助:query_cache_size异常

Fixing "Unknown system variable 'query_cache_size'" when connecting Spring Boot to Azure MySQL

Hey there! I’ve helped a number of developers work through this exact issue with Azure MySQL and Spring Boot, so let’s walk through the full solution step by step.

Why this error pops up

Azure MySQL runs on MySQL 8.x under the hood, which removed the query_cache_size system variable entirely. If you’re using an older MySQL JDBC driver (like 5.x), it still tries to access this deprecated variable—hence the error you’re seeing. Upgrading to driver version 8.0.13+ fixes this, but we need to make sure all related configurations are set correctly too.

Step 1: Upgrade the MySQL Connector/J dependency

Depending on whether you use Maven or Gradle, update your build file to use the compatible driver version:

For Maven (pom.xml)

Either explicitly define the dependency version:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.13</version>
    <!-- You can also use a newer stable version like 8.0.36 if preferred -->
</dependency>

Or if you’re using Spring Boot’s starter dependencies, override the version in your properties section:

<properties>
    <mysql.version>8.0.13</mysql.version>
</properties>

For Gradle (build.gradle)

Add or update the dependency directly:

implementation 'mysql:mysql-connector-java:8.0.13'

Or use Spring Boot’s dependency management to set the version:

ext {
    set('mysqlVersion', '8.0.13')
}

Step 2: Update your application.properties configurations

MySQL 8.x has a few key changes to JDBC settings you need to adjust:

  1. Use the correct driver class: The old com.mysql.jdbc.Driver is deprecated—switch to the 8.x-specific driver:
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    
  2. Tweak the JDBC URL: Add mandatory SSL and timezone parameters (required for Azure MySQL and MySQL 8.x):
    spring.datasource.url=jdbc:mysql://XXXX.mysql.database.azure.com:3306/MyDbName?useSSL=true&serverTimezone=UTC
    
    • useSSL=true: Azure MySQL requires SSL connections by default
    • serverTimezone=UTC: MySQL 8.x mandates a timezone setting to avoid connection errors (replace with your local timezone like Asia/Shanghai if needed)

Step 3: Verify and test

  • Clean your project build (run mvn clean install for Maven, gradle clean build for Gradle)
  • Restart your Spring Boot application
  • Check if the connection establishes successfully without the query_cache_size error

That should get you up and running! Let me know if you hit any other bumps along the way.

内容的提问来源于stack exchange,提问作者Rakesh L

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:28:46