1

I'm trying to connect MySQL by passing an IP address as a parameter instead of localhost.

Class.forName("com.mysql.jdbc.Driver");          

try{
     String url = "jdbc:mysql://192.168.1.116:3306/db";
     DBConnection = DriverManager.getConnection(url, "root", "");
 }
 catch(Exception ex){ ex.printStackTrace(); }

The problem is the connection takes 1-2 minutes before showing this error.

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

By the way, I'm using XAMPP.

kaizen
  • 1,580
  • 4
  • 26
  • 50
  • 1
    Post more code. This contains no loop. – Elliott Frisch May 06 '15 at 03:07
  • Have you tried to add the port number before the /db part? such as jdbc:mysql://192.168.1.116:3306/db – Asura May 06 '15 at 03:13
  • @ElliottFrisch It doesn't have a loop. There's something that's causing this issue. And I couldn't figure it out. – kaizen May 06 '15 at 03:13
  • @Asura Yes, I tried. But It still doesn't work. – kaizen May 06 '15 at 03:15
  • have you looked if the port 3306 is currently being listened to on your computer? its possible the service/listener is not started; but still getting no exception is strange. – Asura May 06 '15 at 03:18
  • if it takes minutes - it's a network timeout. are you SURE you're on the same network? can you ping the database server? is the firewall on the database server letting you through? can you connect to that mysql server using the command line mysql client? – pala_ May 06 '15 at 03:25
  • @pala_ I'm testing it on the computer where I have my xampp installed. This computer has IP address 192.168.1.116 which is where i installed xampp. How can I ping the database server? Do I have to configure firewall settings? My computer is macbook air OS X yosemite – kaizen May 06 '15 at 03:45
  • mysql may not even be listening on that interface. it may only be listening on 127.0.0.1 -- you need to look at your my.cnf file to see what network(s) the server is listening on – pala_ May 06 '15 at 03:47
  • @pala_ I added bind-address=192.168.1.116 to my.cnf and the CommunicationsException disappeared. – kaizen May 06 '15 at 03:58
  • @pala_ Another problem arises. java.sql.SQLException: null, message from server: "Host '192.168.1.116' is not allowed to connect to this MySQL server" – kaizen May 06 '15 at 03:58
  • you need to grant your user permissions to connect from that host. read this: http://stackoverflow.com/questions/8348506/grant-remote-access-of-mysql-database-from-any-ip-address – pala_ May 06 '15 at 04:06

0 Answers0