简体   繁体   中英

Java jdbc connect to mariadb on remote lan server

I know this has been asked a lot but after trying many solution found here I am stil unable to connect to my db.

I'm working on PC and I want to connect to mariadb mysql server on another machine in my LAN. But I want to do it in such a way that my program can connect to my mysql server from any place. Not only from this machine where mysql is or PC's in LAN.

I thought using my external IP and forwarding mysql port on router will do the trick, but I'm getting 'Communication failure' errors.

So far I made sure:

  • -user, pass, db name are correct
  • -port is default 3306
  • -in mysql config I disabled bind-adress
  • -in mysql my user is allowed to connect from any host and from my external IP just to be sure...
  • -database is empty
  • -other databases are working, php can connect but it's executed on same server so it's connecting to 'localhost'
  • -DriverManager.getConnection("jdbc:mysql://MY_IP:3306/my_DB","javauser", "javauserpass");
  • -on machine where mysql server is installed (UbuntuServer) iptables arent configured
  • -I installed xampp and created db there. I can connect to this db using 'localhost' in login query string. But that makes my program to operate only locally.
  • -Inactive firewall doesn't change anything - still no connection

Error:

-------- MySQL JDBC Connection Testing ------------
MySQL JDBC Driver Registered!
Connection Failed! Check output console
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(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:989)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:341)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2251)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2083)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:806)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at Main.main(Main.java:25)
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:300)
    ... 15 more

My example, testing program is taken from here: https://www.mkyong.com/jdbc/how-to-connect-to-mysql-with-jdbc-driver-java/

So where am I making mistake?

Check for the MySQL/MariaDB bind-address variable in the configuration file for the database. Then configure it appropriately . It's probably set up so that it's only binding to localhost.

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