0

This problem has been solved. Here is the list I followed to check the "Connection Refused " error.

  1. 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?
  2. 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:

  1. disable firewalls on both host and client machine
  2. bind mysql service address to 0.0.0.0
  3. make sure mysql does NOT skip networking
  4. make sure the account to connect to mysql is enabled and granted on '%' domain

here is the current status:

virtualbox settings: virtualbox settings

I can successfully connect to it using mysql client: mysql client success

But simple jdbc codes raise error: jdbc error


add proof:

mysql-connector-java 5.1.9: Error with 5.1.9

mysql-connector-java 5.1.32: Success with 5.1.32

lowatt
  • 363
  • 2
  • 18
  • Your connection string points to 127.0.0.1, which is usually mapped to localhost. Shouldn't you use the VM IP address? And what NAT are you talking about? If the VM is to be accessed only from inside your LAN, just set up the virtual network interface to have an IP in the LAN – Raffaele Aug 30 '14 at 07:52
  • @Raffaele according to https://www.virtualbox.org/manual/ch06.html#network_nat the only way we can reach a vm's port A from host is to port-forward it to host's port B. So requests to host's port B is forwarded to vm's port A. In this case, requests to 127.0.0.1:9936 is forwarded to vm's 3306 port. The config must be correct since I CAN connect to the mysql server with other clients. – lowatt Aug 30 '14 at 07:59
  • 1
    Look at 6.5 "Bridged networking". It's a simple settings that requires only a click on a checkbox and maybe will solve your problem – Raffaele Aug 30 '14 at 09:10
  • @Raffaele thx. But I chose to use NAT because the host is a laptop... you konw, the sub net IP prefix is constantly changing. NAT with port-forwading should be best plan. – lowatt Aug 30 '14 at 13:56
  • @Raffaele The problem is not how to share network to the vm but why in Java code this NAT plan do not work. – lowatt Aug 30 '14 at 13:57
  • @Lowatt.Pi - Well you did everything from your side perfectly. But In Some Cases you should check incoming request is blocking or not by the Anti Virus software ( Eset Node 32, Avast and etc...) – Sarvaratchagan Sep 04 '14 at 08:43
  • An old version of the MySQL driver .jar can't cause a `ConnectException.` You changed something else at the same time. – user207421 Sep 05 '14 at 03:14
  • @EJP please check the proofs I just added in the post. I never thought it could, too. – lowatt Sep 05 '14 at 07:07
  • They aren't proofs. They are just listings of what you thought you changed. A proper proof would have to demonstrate conclusively that you didn't change anything *else;* that the MySQL server was running in both cases; etc etc. There are plenty of plausible explanations. Don't accept an implausible one. – user207421 Sep 06 '14 at 01:24
  • @EJP well, the truth is that I only changed this line of pom.xml, and waited maven to reload. And I repeated this process several times and the result is the same : 5.1.9 fails and 5.1.32 success. Believe it or not, I don't need to lie to you. If you have any other constructive method to find out what's wrong, I'm happy to try and feed you back the result. – lowatt Sep 07 '14 at 09:14
  • @EJP and I don't know why so obsessed that "an older version of .jar" cannot cause the problem. If you don't know how they wrote those codes, how can you be so sure. If you cannot accept the truth, i suggest you set up a virtual box and try it yourself. – lowatt Sep 07 '14 at 09:19

1 Answers1

3

I have MariaDB in an ArchLinux VM (host is Windows 8.1), configuration excerpt /etc/mysql/my.cnf for networking follows:

#skip-networking
bind-address = 0.0.0.0

This is my VirtualBox NAT config:

enter image description here

I also granted remote connect privileges:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION

With this setup I am able to connect using MySQL clients from the host on port 9936.

This is my gradle build:

apply plugin: 'java'

apply plugin: 'application'
mainClassName = "Main"

repositories {
    mavenCentral()
}

dependencies {
    runtime 'mysql:mysql-connector-java:5.1.32'
}

This is my Main.java in the root package:

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

public class Main {
    public static void main(String[] args) {
        try {
            String url = "jdbc:mysql://127.0.0.1:9936/test";
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection conn = DriverManager.getConnection(url, "root", null);
            System.out.println("Connection successful!");
        } catch(Throwable t) {
            t.printStackTrace();
        }
    }
}

Running it from the host with gradle run produces:

Connection successful!

Can you reproduce such a successful situation using this exact same setup? Is Java allowed to use network connections (e.g. by firewall applications)?

circlespainter
  • 836
  • 5
  • 8
  • not working for me. there are a few differences between our scenarios: 1 I used ubuntu for both the host and the client 2 I used mysql server 5.6, 3 I used maven 3.1.0 and maybe 4 I used java 8. thx all the same! – lowatt Sep 03 '14 at 11:07
  • I'm using Java 8 too. Ok, so things that come to my mind: OS/distro differences or network-level blocking either on the host or on the guest: I'd rule it out as you can connect through other means (unless Java uses some OS-specific networking features differently than other tools); build tool: shouldn't be relevant; minor Java version differences: could be, I'm using .20; MySQL driver/server differences: could be, using 5.1.32 driver and MariaDB on the server; application-level firewall on the host or guest: could be – circlespainter Sep 03 '14 at 11:24
  • Java policies: I suppose they could be as well but I'm no expert there – circlespainter Sep 03 '14 at 11:30
  • This http://stackoverflow.com/questions/2121829/mysql-jdbc-communications-link-failure could have some additional insights, it seems to suggest a few more things, like the Java MySQL driver version (or even position where it was downloaded) being a possible cause. Which version are you using? – circlespainter Sep 03 '14 at 14:07
  • 1
    OMG. I changed the "mysql-connector-java" version from 5.1.9 to 5.1.32, everything is ok now! Thank you so much!! – lowatt Sep 04 '14 at 06:20