If you are facing this error, then it indicates that you are connecting to a MySQL database from a java application either directly using JDBC or through a framework such as Hibernate, iBatis or Spring.
This post will list out various probable causes for this error and the steps that you may follow for its resolution.

Reason
In order to connect to a MySQL database from a java application, either a connection string of the format jdbc:mysql://<databaseip>:<port>/databasename is used or a properties file that specifies different connection parameters such as database server URL, port, database name etc. is utilized.

Error clearly tells us that the application is trying to connect to a specified database but it could not.
There may be several reasons for this as listed below.

  • Database server ip and port are provided in the URL and are incorrect.
  • MySQL server instance is not running at that ip and port.
  • Database given in the URL or properties does not exist.
  • There is a firewall on the database machine which is blocking incoming connections.
  • If you are using a host name in place of database server ip, then it is incorrect or not known to the local DNS server.
  • MySQL server is running with --skip-networking option enabled.

Solution
For resolving the error, follow the below check list.

  • Check the database server ip and port in the provided connection details and verify that MySQL server is listening at the these values.
  • Firewall on the target machine is off or you have added exceptions in the firewall for the given port.
  • Test that the target system is accessible from the source machine using ping command.
  • Restart MySQL server.
  • Check if you are able to connect to the database from MySQL command line client or MySQL workbench.
  • If you are using localhost, then try using system ip in the connection string.
  • Check if MySQL server is not started with --skip-networking option. If it is, then the server will not allow remote connections.
    In order to check the current value for this option, execute SHOW VARIABLES LIKE 'skip_networking'; command. If it shows ON, then turn it OFF first.

There are bright chances that after following above steps, the error will be resolved.
If any other solution worked for you, do not forget to mention it in the comments below.
Hit the clap!!!

Leave a Reply