chrisg
chrisg

Reputation: 41705

Connect to MySQL with JDBC over network

I am trying to connect to MySQL database over a network. I have installed MySQL, and the service is running on the default port. I have also install the SQL connector to the jar file and added java JDK to the server machine. I am able to connect to my local database using the code:

private String dbUrl = "jdbc:mysql://localhost/DatabaseName";
private String dbClass = "com.mysql.jdbc.Driver";

But when I try and connect to it over the network, with the IP address (eg: 192.168.1.45):

private String dbUrl = "jdbc:mysql://192.168.1.45/DatabaseName";
private String dbClass = "com.mysql.jdbc.Driver";

I get the connection error:

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Does anyone know what this issue is? Do I need to add a different address? I have added the default port with the address but cannot get it to work.

Thanks for any help.

Upvotes: 2

Views: 10246

Answers (3)

Joshua Martell
Joshua Martell

Reputation: 7212

You can test the server setup using the MySQL Workbench or mysql client which will narrow down the problem. It's also sometimes useful to just see if the server's there:

telnet host 3306

It'll tell you the version number of the server and some other binary junk. Enough to know your host is listening.

Upvotes: 0

ccheneson
ccheneson

Reputation: 49410

By default, MySQL doesnt allow remote access and only allow local access. You will have to modify your /etc/mysql/my.cnf config (on Linux) with:

bind-address = 192.168.1.45 // Your Server IP address
# skip-networking // This should be commented .

See the whole procedure here.

Upvotes: 3

stacker
stacker

Reputation: 69002

  1. Check the my.cnf [mysqld] settings for the parameters port, bind-address, socket, to make sure these aren't causing problems.
  2. Check the files /etc/hosts, /etc/hosts.deny to make sure everything is ok.
  3. Check firewall applications
  4. Check to make sure whatever directory mysqld's sockets are have the appropriate permissions.
  5. Check to make sure that security settings within the mysql database (user table) permit access from your remote host.
  6. Make sure you can telnet OK to localhost 3306, 127.0.0.1 3306, and whatever other IP address your machine is configured to (use ifconfig to find out).

Upvotes: 2

Related Questions