Cannot Connect to MySQL Using JDBC - Connection Timeout - Ubuntu 9.04

I run Ubuntu and end up trying to connect Tomcat to my MySQL database using JDBC.

It worked previously , but after rebooting the instance was now unable to connect.

  • Both Tomcat 6 and MySQL 5.0.75 are on the same machine
  • Connection string: jdbc: mysql: /// localhost: 3306
  • I can connect to MySQL at the command line using the command mysql
  • The my.cnf file is pretty standard (available on request) has a binding address: 127.0.0.1
  • I cannot use Telnet for the MySQL port, even though netstat says MySQL is listening
  • I have one IpTables rule to forward 80 -> 8080 and no firewall that I know of.

I am new to this and I'm not sure what else to check. I do not know if I should search in / etc / interfaces, and if I did what I need to search. This is strange because it worked, but after rebooting, so I have to change something ... :).

I understand that a timeout indicates that the server is not responding, and I assume, because the request does not actually go through. I installed MySQL via apt-get and Tomcat manually.

MySqld Processes

root@88:/var/log/mysql#  ps -ef | grep mysqld
root     21753     1  0 May27 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe
mysql    21792 21753  0 May27 ?        00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-external-locking --port=3306 --socket=/var/run/mysqld/mysqld.sock
root     21793 21753  0 May27 ?        00:00:00 logger -p daemon.err -t mysqld_safe -i -t mysqld
root     21888 13676  0 11:23 pts/1    00:00:00 grep mysqld

Netstat

root@88:/var/log/mysql# netstat -lnp | grep mysql
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      21792/mysqld
unix  2      [ ACC ]     STREAM     LISTENING     1926205077 21792/mysqld        /var/run/mysqld/mysqld.sock

Toy Connection Class

root@88:~# cat TestConnect/TestConnection.java

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

public class TestConnection {

  public static void main(String args[]) throws Exception {
    Connection con = null;

    try {
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      System.out.println("Got driver");
      con = DriverManager.getConnection(
                "jdbc:mysql:///localhost:3306",
                "uname", "pass");
      System.out.println("Got connection");

      if(!con.isClosed())
        System.out.println("Successfully connected to " +
          "MySQL server using TCP/IP...");

    } finally {
        if(con != null)
          con.close();
    }
  }
}

Toy Connection Class Out

Note. This is the same error that I get from Tomcat.

root@88:~/TestConnect# java -cp mysql-connector-java-5.1.12-bin.jar:. TestConnection
Got driver
                Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 1 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:409)
        at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1122)            
        at TestConnection.main(TestConnection.java:14)
Caused by: 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:409)
        at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1122)
        at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:344)
        at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2181)
        ... 12 more
Caused by: java.net.ConnectException: Connection timed out
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        ... 13 more

Telnet Output

root@88:~/TestConnect# telnet localhost 3306
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection timed out
+5
source share
4 answers

Netstat , MySQL 3306 . telnet 127.0.0.1, , - sudo ufw default allow, , sudo ufw allow 3306 , - .

+2

/etc/hosts? ?

127.0.0.1   localhost

telnet:

telnet 127.0.0.1 3306 or telnet 127.0.0.1:3306

JDBC :

jdbc:mysql://127.0.0.1:3306
+1

jdbc:mysql://localhost:3306

jdbc:mysql:///localhost:3306

unix/linux / unix, tcp/ip. , 3306 .

0

getConnection, , , ? ( "///" .)

, :

String dbUrl = "jdbc: mysql://localhost: 3306/[db]? user = [user] & password = [password]"; Connection conn = DriverManager.getConnection(dbUrl);

mysql.com:

http://dev.mysql.com/doc/refman/5.0/es/connector-j-usagenotes-basic.html

0

All Articles