MySQL连接器中欧时区无法识别错误求助及排查
Let's work through this issue step by step—this is a common pitfall when dealing with MySQL Connector/J and European time zone configurations, and I’ve helped troubleshoot similar problems before.
Why This Error Pops Up
The root causes here are two-fold:
- The shorthand
CEST(Central European Summer Time) is ambiguous to the JDBC driver. It’s a temporary daylight saving time label, not a standardized, region-specific time zone that the driver can reliably map. - Critical mistake: Your JDBC URL uses semicolons (
;) to separate parameters instead of ampersands (&). JDBC URLs require&as the parameter separator, so yourserverTimezonesetting might not even be parsed correctly!
Solutions to Resolve the Issue
1. Fix the JDBC URL with a Specific Time Zone & Correct Syntax
Replace the vague CEST with a standardized Olson time zone identifier (like Europe/Berlin) and fix the parameter separators. Here’s the corrected URL:
URL="jdbc:mysql://127.0.0.1:3306/rk_tu_lager?useLegacyDatetimeCode=false&serverTimezone=Europe/Berlin&useSSL=false"
Olson time zones (e.g., Europe/Paris, Europe/Vienna) are region-specific and include rules for daylight saving transitions, eliminating ambiguity for the driver.
2. Set MySQL Server’s Default Time Zone
If you’d rather not configure the time zone in the JDBC URL, you can set it directly on the MySQL server:
- Open your MySQL configuration file (
my.cnfon Linux,my.inion Windows). - Add or update the
default-time-zoneparameter under the[mysqld]section:[mysqld] default-time-zone = 'Europe/Berlin' - Restart the MySQL service to apply the change.
This ensures all database connections use the correct time zone by default, so you can optionally remove theserverTimezoneparameter from your JDBC URL.
3. Adjust Driver Version & Parameter Compatibility
Since you switched to Connector/J 5.1.39, keep these notes in mind:
- The
useLegacyDatetimeCode=falseparameter is primarily relevant for 8.x drivers. For 5.x versions, this defaults totrue, so removing it might resolve unexpected behavior. - If you decide to upgrade to Connector/J 8.x (recommended for newer MySQL versions), make sure you still use a specific Olson time zone for
serverTimezoneas outlined in solution 1.
Verify the Fix
After making changes, restart your Tomcat server and test the database connection. The error about an unrecognized time zone should no longer appear.
Caused by: com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: The server time zone value 'Mitteleuropäische Sommerzeit' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specific time zone value if you want to utilize time zone support.
内容的提问来源于stack exchange,提问作者Martin Klestil

