0

I can connect to MySQL if the code is:

conn = DriverManager.getConnection(
                "jdbc:mysql://localhost/db","root", "test");

but as soon as I change it to one of these (first is my IP Address)

conn = DriverManager.getConnection(
                "jdbc:mysql://82.41.85.161/db","root", "test");

conn = DriverManager.getConnection(
                "jdbc:mysql://127.0.01/db","root", "test");

It takes a while for something to happen, and all that does happen is a lack of connection.

SQLException: Communications link failure

I've tried changing the bind address in the CNF file and putting a "#" in front of it but nothing happens. I'm currently working purely in "MySQLWorkbench" and the end goal here is to actually connect from another computer to this database. Any help would be much appreciated.

4 Answers4

1

You need to ensure that MySQL is set up to allow remote connections, and that your port is accessible. Users in the database can also be allowed/banned from being accessed externally.

Will
  • 810
  • 6
  • 21
  • `Users in the database can also be allowed/banned from being accessed externally.` In that case there should be an error returned by mysql, not a connection failure – Hanky Panky Jan 22 '16 at 10:41
  • That is true, I didn't consider that. – Will Jan 22 '16 at 10:42
  • I've allowed remote connections, but now it says: Check your SSH connection settings and whether the SSH server is up. Error: timed out –  Jan 22 '16 at 10:47
1
127.0.01

Is invalid. It should be

127.0.0.1

if localhost works fine there is usually never any reason for 127.0.0.1 not to work.

As far as the other one is concerned i'm sure your public interface does not allow mysql port access.

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
0

you forgot a dot here:

conn = DriverManager.getConnection(
                "jdbc:mysql://127.0.01/db","root", "test");

you should try this one:

conn = DriverManager.getConnection(
                "jdbc:mysql://127.0.0.1/db","root", "test");
theDima
  • 746
  • 4
  • 12
0

First, you have a typo while setting the address in you connection to the loopback address. It should read 127.0.0.1 not 127.0.01.

conn = DriverManager.getConnection("jdbc:mysql://127.0.01/db","root", "test");

Secondly, remote access to MySQL database servers is disabled by default for security reasons. You must tell your server to bind itself to a given IP address and listen for connections.

For this you must edit your my.cnf file, which you might find in one of the following locations:

/etc/my.cnf
/etc/mysql/my.cnf
$MYSQL_HOME/my.cnf
[datadir]/my.cnf
~/.my.cnf

Alternatively you can use find to locate the file you want to edit:

$ find / -iname "my.cnf" -print 2>/dev/null
/etc/mysql/my.cnf

Then edit the my.cnf and add the following:

bind-address    = 82.41.85.161

Make sure that the line skip-networking is commented by using # before it like this: #skip-networking

Also verify that your firewall isn't blocking connections on port 3306 by using iptables -L -n | grep :PORT.

Finally restart mysql with /etc/init.d/mysql restart.

jdsalaro
  • 378
  • 2
  • 10