title: Solving a "communications link failure" with JDBC and MySQL
body: <p>I'm trying to connect to the local MySQL server but I keep getting an error.</p>
  
  <p>Here is the code.</p>
  
  <pre><code>public class Connect {
  
      public static void main(String[] args) {
          Connection conn = null;
  
          try {
              String userName = "myUsername";
              String password = "myPassword";
  
              String url = "jdbc:mysql://localhost:3306/myDatabaseName";
              Class.forName("com.mysql.jdbc.Driver").newInstance();
              conn = DriverManager.getConnection(url, userName, password);
              System.out.println("Database connection established");
          } catch (Exception e) {
              System.err.println("Cannot connect to database server");
              System.err.println(e.getMessage());
              e.printStackTrace();
          } finally {
              if (conn != null) {
                  try {
                      conn.close();
                      System.out.println("Database Connection Terminated");
                  } catch (Exception e) {}
              }
          }
      }
  }
  </code></pre>
  
  <p>and the errors :</p>
  
  <pre><code>Cannot connect to database server
  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.
  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(NativeConstructorAccessorImpl.java:39)
          at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
          at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
          at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
          at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
          at com.mysql.jdbc.MysqlIO.&lt;init&gt;(MysqlIO.java:344)
          at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2333)
          at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2370)
          at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2154)
          at com.mysql.jdbc.ConnectionImpl.&lt;init&gt;(ConnectionImpl.java:792)
          at com.mysql.jdbc.JDBC4Connection.&lt;init&gt;(JDBC4Connection.java:47)
          at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
          at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
          at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
          at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
          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(DriverManager.java:582)
          at java.sql.DriverManager.getConnection(DriverManager.java:185)
          at Connect.main(Connect.java:16)
      Caused by: java.net.ConnectException: Connection refused
          at java.net.PlainSocketImpl.socketConnect(Native Method)
          at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
          at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
          at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
          at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
          at java.net.Socket.connect(Socket.java:529)
          at java.net.Socket.connect(Socket.java:478)
          at java.net.Socket.&lt;init&gt;(Socket.java:375)
          at java.net.Socket.&lt;init&gt;(Socket.java:218)
          at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:257)
          at com.mysql.jdbc.MysqlIO.&lt;init&gt;(MysqlIO.java:294)
          ... 15 more
  </code></pre>
  
  <p>I've set the classpath, made sure my.cnf had the skip network option commented out. </p>
  
  <p>java version is 1.2.0_26 (64 bit)
  mysql 5.5.14
  mysql connector 5.1.17</p>
  
  <p>I made sure that the user had access to my database.</p>