0

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?

Mike
  • 809
  • 6
  • 20
  • What do you mean by this, and how's it relate to connecting to the database? "my program can be edited by my friends from their PC's outside my LAN" – David Ehrmann Oct 15 '16 at 17:00
  • This does not make any sense whatsoever. If you want your friends to be able to edit your program from outside your LAN, or even within your LAN, host your source code on GitHub or BitBucket. What you do with your mariadb connection has absolutely nothing to do with friends editing your program. – Mike Nakis Oct 15 '16 at 17:01
  • Ignore all the Java bits for a second. What happens if you do `telnet ` (or `nc `). That exception is just a connection timeout exception, so it looks like you're either on a different network or the port is firewalled. – David Ehrmann Oct 15 '16 at 17:02
  • firewall or not set up for remote connection. bind-address, skip-networking, daemon restart. [nixCraft](http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html) – Drew Oct 15 '16 at 17:09
  • @DavidEhrmann I edited question. The point is not how my friends can edit my program, but how anyone using it can connect to my db. Telnet gives me this: 'telnet: Unable to connect to remote host: Resource temporarily unavailable' – Mike Oct 15 '16 at 17:10
  • If your test has both machines on the private side of your router then trying to reach your server machine from your client machine via the public IP address may simply not be possible. (I've owned a few routers that supported it and others that didn't.) – Gord Thompson Oct 15 '16 at 20:10
  • @GordThompson What options should I seek in router settings? – Mike Oct 15 '16 at 20:18
  • I don't recall seeing any router settings to control that behavior, it was just something that the router supported or it didn't. When working with the latter I would often use an entry in the HOSTS file on the client machine to map the DNS name to the local (private) IP address of the server. – Gord Thompson Oct 15 '16 at 21:16
  • Take a look at [this](http://stackoverflow.com/questions/6865538/solving-a-communications-link-failure-with-jdbc-and-mysql), might help! – N00b Pr0grammer Oct 17 '16 at 09:27
  • I was there and did that already. Problem is network related. I think i will give up and use xampp – Mike Oct 17 '16 at 20:31

1 Answers1

0

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.

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
  • I got both lines commented out, tried to put my external IP - didn't help. My firewall is inactive. Router is forwarding 3306 to my server machine – Mike Oct 18 '16 at 12:26