Spring Boot连接Azure MySQL失败求助:query_cache_size异常
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:
- Use the correct driver class: The old
com.mysql.jdbc.Driveris deprecated—switch to the 8.x-specific driver:spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver - 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=UTCuseSSL=true: Azure MySQL requires SSL connections by defaultserverTimezone=UTC: MySQL 8.x mandates a timezone setting to avoid connection errors (replace with your local timezone likeAsia/Shanghaiif needed)
Step 3: Verify and test
- Clean your project build (run
mvn clean installfor Maven,gradle clean buildfor Gradle) - Restart your Spring Boot application
- Check if the connection establishes successfully without the
query_cache_sizeerror
That should get you up and running! Let me know if you hit any other bumps along the way.
内容的提问来源于stack exchange,提问作者Rakesh L

