1
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class Anime {


    public static void main(String[] args) {

            String url = "jdbc:mysql://xx.xxx.xxx.xx:3306/wordpress";
            String user_name = "root";
            String pass = "mypassword_is_here";
        try {

            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection conn = DriverManager.getConnection(url,user_name, pass);

        } catch ( Exception e ) {

            e.printStackTrace();

        }


    }

}

And I keep getting this error

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:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:344)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2332)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
    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:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at Anime.main(Anime.java:17)
Caused by: java.net.ConnectException: Connection refused: 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 java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:257)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:294)
    ... 15 more

Now I've looked around stackoverflow and it seems like this is usually because the url isn't correct or sometimes there is a firewall issue or the wait_timeout variable in mysql is too low (it isn't in my case).

By the way, I am trying to connect to an external database on a centOS linux server running mySQL.

So I'm wondering how I can test the URL outside the program to really see if it's because of the URL or the firewall or any of that for the matter. If you guys got any solutions, please let me know.

Maaz
  • 4,193
  • 6
  • 32
  • 50
  • Just use the mysql.exe command line tool that comes with Mysql. Like this: mysql.exe -s -N -h hostname -P 3306 -D databasename -u username -ppassword – sasankad Jan 10 '14 at 03:54
  • Comprehensive answer is given to your question here http://stackoverflow.com/questions/2983248/com-mysql-jdbc-exceptions-jdbc4-communicationsexception-communications-link-fai – sasankad Jan 10 '14 at 03:59
  • @sasankad if you're talking about the answer where it gives you the reasons as to why you got the error and how to fix them. I've already looked at that answer and all the things seem to be right for me. – Maaz Jan 10 '14 at 04:01
  • i hope you've read the comments as well. ex: "MAMP/ MAMP Pro sets MAMP_skip-networking_MAMP by default. You've to disable this line in your my.cfn" – sasankad Jan 10 '14 at 04:02
  • Here is how my `my.cnf` file looks: http://pastebin.com/26UYygKW – Maaz Jan 10 '14 at 04:08

0 Answers0