This problem has been solved. Here is the list I followed to check the "Connection Refused " error.
- MySQL server status
- Is it running ?
- Is --skip-networking disabled ?
- Does it bind to address 0.0.0.0 ?
- Is the service blocked by any firewall?
- Did it raise any error / alert / warning in its log files?
- Client status
- can you reach the server ip / url?
- can you telnet the server with the port?
- can you log in mysql service using other mysql client software on the same machine through the same network route?
- check the firewall settings
- check mysql connector / driver !!
The answer to the question seems stupid: I used an out-dated mysql driver ( 5.1.9, the current latest version is 5.1.32).
Still I don't know why is the older version not working.
I setup a mysql server in a virtual machine hosted by virtualbox and I can successfully connect to it in host machine with command-line mysql client or with mysql workbench.
But I just cannot connect to it using mysql jdbc connector with the same ip and port.
I connected the host and the vm with nat and port forwarded mysql server's 3306 to host's 9936 port.
the java code I used to test the connection:
String url = "jdbc:mysql://127.0.0.1:9936/test";
Class.forName ("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(url, "usr", "pwd");
this is the error :
Exception in thread "main" 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.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
This is really wierd 'cause I can successfully telnet to it and connect to it with any mysql client I could find but the java code just do not work!
Update 2014 09 30
I have tried the following steps, not working:
- disable firewalls on both host and client machine
- bind mysql service address to 0.0.0.0
- make sure mysql does NOT skip networking
- make sure the account to connect to mysql is enabled and granted on '%' domain
here is the current status:
virtualbox settings:
I can successfully connect to it using mysql client:
But simple jdbc codes raise error:
add proof:
mysql-connector-java 5.1.9:
mysql-connector-java 5.1.32: