0

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

I have a java program that connects to a mysql database using jdbc. It was working before and now it is not. This is the error in the tomcat catalonia.out log :

Cannot connect to database 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.GeneratedConstructorAccessor8.newInstance(Unknown Source)
    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.readPacket(MysqlIO.java:696)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1102)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2336)
    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.GeneratedConstructorAccessor10.newInstance(Unknown Source)
    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 web.DBConnectionDesign.<init>(DBConnectionDesign.java:33)
    at web.SaveDatabase2.doPost(SaveDatabase2.java:126)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:619)
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4   bytes, read 0 bytes before connection was unexpectedly lost.
    at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2552)
    at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:620)
    ... 30 more

here is the code that tries to connect with the database

public DBConnectionDesign() {
// Create a database connection
    String account = "account";
String password = "password";
String server = "localhost:3306";
String database = "database";
try {
    Class.forName("com.mysql.jdbc.Driver");

    con = DriverManager.getConnection
        ( "jdbc:mysql://" + server, account,password);
        System.out.println ("Database connection established");
    stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,     ResultSet.CONCUR_UPDATABLE);
    stmt.executeQuery("USE " + database);
    // get the column names; column indexes start from 1        
    } catch (SQLException e) {
        System.err.println ("Cannot connect to database server");
        e.printStackTrace();
    } 
    catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

}

I can access the database with the same user name and password from the terminal. I also have a wordpress site that can access another mysql database on the same server. This is my.conf:

[client]
port            = 3306
socket          = /var/run/mysqld/mysqld.sock

[mysqld_safe]
socket          = /var/run/mysqld/mysqld.sock
nice            = 0

[mysqld]

user            = mysql
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
skip-external-locking

bind-address            = 127.0.0.1
key_buffer              = 16M
max_allowed_packet      = 16M
thread_stack            = 192K
thread_cache_size       = 8

myisam-recover         = BACKUP

query_cache_limit       = 1M
query_cache_size        = 16M

general_log_file        = /var/log/mysql/mysql.log
general_log             = 1

log_error                = /var/log/mysql/error.log

expire_logs_days        = 10
max_binlog_size         = 100M

[mysqldump]
quick
quote-names
max_allowed_packet      = 16M

[mysql]

[isamchk]
key_buffer              = 16M

If anyone has ideas of other error logs to check or how to properly use tcpdump to look at requests I would really appreciate it. Right now, I have no idea why I can't connect to the server:the port number is correct, and the user name and password are correct too. How can I find out the reason I cannot connect to the server?

thanks!

Community
  • 1
  • 1
Timnit Gebru
  • 303
  • 1
  • 7
  • 15
  • try editing `/etc/mysql/hosts.allow` and add a line `mysqld : 127.0.0.1 : allow` – RanRag Jan 12 '12 at 23:25
  • thanks for the suggestion. I had that already and also have that line on /etc/hosts.allow. Still didn't make a difference. Do you know how I can find out whether --skip-networking is enabled or not? I had commented out on my.conf and restarted mysql so I'm assuming its disabled but want to make sure – Timnit Gebru Jan 12 '12 at 23:33
  • have a look here http://www.linuxquestions.org/questions/linux-newbie-8/mysql-not-listening-on-port-3306-a-370210/ – RanRag Jan 12 '12 at 23:44
  • which driver version you are using beacuse the problem you mentioned is also a bug in the jdbc driver. – RanRag Jan 12 '12 at 23:53
  • try this http://aaron-kelley.net/blog/2010/01/mysql-tomcat-6-and-ubuntu-9-10-blah/ – RanRag Jan 13 '12 at 00:03
  • also I have now confirmed that skip-networking is off and netstat shows: tcp 0 0 127.0.0.1:3306 0.0.0.0:*LISTEN – Timnit Gebru Jan 13 '12 at 00:05
  • Are you in fact running that Java code and the MySQL server on the same box? (You don't explicitly say so.) On the same box that runs the Java code, you could try `telnet localhost 3306` to make sure the networking is okay. – jbindel Jan 13 '12 at 01:35
  • yes. I'm running them on the same box. I finally just restored from backup and it works. I'll update if I figure out out what was wrong – Timnit Gebru Jan 13 '12 at 02:14
  • check out:: http://stackoverflow.com/questions/2983248/com-mysql-jdbc-exceptions-jdbc4-communicationsexception-communications-link-fai – Fahim Parkar Jan 14 '12 at 16:45

0 Answers0