I'm trying to connect my Java application to a local MySQL instance, but I keep hitting the "Could not create connection to database server" error. I've double-checked my username and password, and the MySQL service is definitely running on port 3306. Is this likely a driver version mismatch, a timezone configuration issue, or perhaps something related to the bind-address in my my.cnf file? I’m using Connector/J 8.0, and any help on the specific connection string parameters would be appreciated.
3 answers
This error often occurs in MySQL 8.0+ because the driver requires an explicit server time zone or a specific SSL configuration. To fix this, try appending ?useSSL=false&serverTimezone=UTC to your JDBC connection URL. Additionally, ensure that your MySQL user has the correct privileges to connect from 'localhost' or '%'. If you are using an older version of the JDBC driver with a newer MySQL server, the authentication plugin (caching_sha2_password) might be causing the failure. Updating your Connector/J dependency to the latest version usually resolves the handshake issue. Lastly, verify that your firewall isn't blocking port 3306, even if you are connecting locally, as some security software can intercept these requests.
Have you checked your my.ini or my.cnf file to see if the bind-address is set to 127.0.0.1? If it is, and you are trying to connect via the actual IP address of the machine instead of 'localhost', the server will reject the connection attempt immediately.
You likely just need to add serverTimezone=UTC to your connection string. MySQL 8 is very strict about this, and the driver will fail to create a session if the zone isn't defined.
I agree with Brenda. I spent hours on this last month. It’s also worth checking if your MySQL service account has expired or if the "max_connections" limit has been reached on the server side, as both can trigger the same generic connection failure message in the Java console.
Marcus, that was a lifesaver! I was trying to connect using my local network IP while the server was restricted to the loopback address. For anyone else struggling, you can change the bind-address to 0.0.0.0 to allow connections from any interface, but please be careful with security if your server is exposed to the internet. After changing the config file, remember to restart the MySQL service for the changes to take effect, or you'll keep seeing the same error despite the "fix."