在IntelliJ中使用Maven连接MySQL服务器失败,请求技术支持
Hey Jonas, sorry to hear you're stuck with this frustrating connection issue—since you already confirmed the database is reachable via IntelliJ, we can rule out basic server availability problems. Let’s break down the most likely causes and fixes:
Possible Causes & Step-by-Step Fixes
1. Mismatched JDBC Driver Version
IntelliJ’s Database tool automatically pulls a driver compatible with your MySQL server, but your project might be using an outdated or incompatible driver JAR:
- For MySQL 8.0+, you need the modern
com.mysql.cj.jdbc.Driverclass (not the legacycom.mysql.jdbc.Driver). - Verify your build configuration uses a driver version that matches your server. For example, in Maven:
<!-- Maven dependency for MySQL 8.0+ --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency> - If you’re using a manual JAR instead of a build tool, ensure it’s the latest compatible version for your MySQL server.
2. Connection String/Configuration Discrepancies
Even small differences between your code’s parameters and IntelliJ’s can break the connection:
- Mandatory timezone parameter: MySQL 8.0+ requires a
serverTimezonein the JDBC URL (e.g.,?serverTimezone=UTCor your local timezone likeserverTimezone=Europe/London). - Host address tweak: Try replacing
localhostwith127.0.0.1—sometimeslocalhostuses a Unix socket instead of TCP/IP, which might have different permissions. - Copy IntelliJ’s exact URL: Go to IntelliJ’s Database tool, right-click your connection →
Properties, then copy the full JDBC URL directly into your code to eliminate typos or missing parameters. - Add debug parameters: Append
useSSL=false(for development) andallowPublicKeyRetrieval=trueif you’re hitting public key retrieval errors.
3. Bugs in Your MySQLManager Class
Since you’re using a custom connection manager, double-check its logic:
- Did you load the correct driver class? Missing
Class.forName("com.mysql.cj.jdbc.Driver")(or the legacy driver for pre-8.0 servers) will prevent connections from being established. - Is the retry logic flawed? The error mentions "Attempted reconnect 3 times"—maybe your manager isn’t handling exceptions properly, or the retry delay is too short for the server to respond.
- Are credentials exact? Ensure the username/password in
MySQLManagermatches exactly what you use in IntelliJ (no extra spaces, case sensitivity matters!).
Quick Isolation Test
To narrow down the issue, run this minimal connection test outside your MySQLManager:
package Code; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class TestDBConnection { public static void main(String[] args) { // Paste the exact URL/username/password from IntelliJ's Database tool here String url = "jdbc:mysql://127.0.0.1:3306/your_database?serverTimezone=UTC&useSSL=false"; String user = "your_username"; String password = "your_password"; try (Connection conn = DriverManager.getConnection(url, user, password)) { System.out.println("Successfully connected to the database!"); } catch (SQLException e) { System.err.println("Connection failed: " + e.getMessage()); e.printStackTrace(); } } }
If this works, the problem is definitely in your MySQLManager implementation. If it fails, double-check the driver version and URL parameters again.
内容的提问来源于stack exchange,提问作者Jonas.H

