简体   繁体   中英

jdbc cannot connect to mysql behind nat with port forwarding

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: 虚拟机设置

I can successfully connect to it using mysql client: mysql客户端成功

But simple jdbc codes raise error: jdbc错误


add proof:

mysql-connector-java 5.1.9: 5.1.9错误

mysql-connector-java 5.1.32: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM