This problem has been solved. Here is the list I followed to check the "Connection Refused " error.
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:
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:
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:
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 (eg by firewall applications)?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.