I'm having trouble connecting to my database. I think there is a setting somewhere that I don't know about and needs to be changed. I have some really basic code here:
public static void main(String[] args)
{
try
{
Connection con = DriverManager.getConnection("jdbc:mysql://IP:3306/TABLENAME?autoReconnect=true","USERNAME", "PASSWORD");
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
Obviously with the login info. And it causes this error:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Caused by: java.net.ConnectException: Connection refused
I've looked through about 10 different posts about the same problem and tried their solutions and still couldn't get it to work.
This problem occurs when the MySQL service is not running and you're trying to connect to the mysql database.
To resolve this in Windows,
Start >> type 'services' and open it.
Find the MySQL service. Probably with name MySQLxx. xx being the version.
eg. MySQL56, MySQL57 etc
Start the service and now try to connection to database.
Increase Mysql max connection,
SET GLOBAL max_connections = 1500;
If still getting an error then queries might be taking too long to respond.
In that case increase attempt while creating jdbc connection,
&maxReconnects=XX
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With