Linux suse11 can log on to terminal to perform various operations normally. jdbc web program running on tomcat has an exception:
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 supported Ed any packets from the server.
Cause search
Normally, you can connect to msql suse and run the following command:
1. Run the command netstat-talnp to verify whether port 3306 is Enabled:
Test :~ # Netstat-talnp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
Tcp 0 0 0.0.0.0: 3306 0.0.0.0: * LISTEN 4620/mysqld
......
2. Run the command ps aux | grep mysql to check whether the mysql service is running
Test :~ # Ps aux | grep mysql
Root 4254 0.0 0.0 2940 1332? S/bin/sh/usr/local/mysql/bin/mysqld_safe -- datadir =/usr/local/mysql/data -- pid-file =/usr/local/mysql/ data/test. pid
Mysql 4620 0.1 0.9 301476 37428? Sl/usr/local/mysql/bin/mysqld -- basedir =/usr/local/mysql -- datadir =/usr/local/mysql/data -- plugin-dir =/usr /local/mysql/lib/plugin -- user = mysql -- log-error =/usr/local/mysql/data/test. err -- pid-file =/usr/local/mysql/data/test. pid -- socket =/usr/local/mysql. sock -- Ports = 3306
Root 8002 0.0 0.0 2264 680 pts/2 S + grep mysql
3. Check the process lsof-I: 3306 running on port 3306.
Test :~ # Lsof-I: 3306
Command pid user fd type device size/OFF NODE NAME
Mysqld 4620 mysql 12u IPv4 10559 0t0 TCP *: mysql (LISTEN)
Run the above command on the problematic Machine
1. netstat-talnp cannot see the tcp connection bound to port 3306
2. ps aux | grep mysql display service is running
3. Check whether mysql is running on port 3306 in lsof-I: 3306. The result does not show that mysql is not running on port 3306.
Conclusion: although the mysql service is running, it can be operated through teminal, but it is not running on port 3306, so it cannot be connected through the jdbc connection.
Then, check the network information. The skip-networking parameter in the mysql configuration is not commented. The parameter skip-networking is used to stop listening on the TCP/IP Port, all interactions with mysqld must be performed through Unix sockets or named pipelines.
The configuration file is annotated as follows:
# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# If all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (Via the "enable-named-pipe" option) will render mysqld useless!
Comment out the parameter skip-networking and restart the mysql service. The jdbc program can connect to mysql normally.
The Network encounters the same exception. other possible causes:
1.
The tomcat program connects to the mysql database of the server and reports
Com. mysql. jdbc. exceptions. jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driverhas not supported Ed any packets from the server.
An error occurs. Later, I used a java program to directly connect to the database. The same error occurs. The database connection and user name and password are all correct.
I have checked many situations on the Internet, and it is useless to set database user permissions or database connection wait time (wait_timeout.
The connection to the local database is okay. The local database version is 5.0.37, the server database version is 5.0.19, And the JDBC driver version is:
Mysql-connector-java-5.1.9-bin.jar.
This problem plagued a day, later think about changing the 5.0 driver to see how, the results on the Internet to find a mysql-connector-java-5.1.14-bin.jar driver, the results are OK.
2.
CommunicationsException: Communications link failure
If you get a SQLException: Connection refused or Connection timed out or a MySQL specific CommunicationsException: Communications link failure, then it means that the DB isn' t reachable at all. this can have one or more of the following causes:
IP address or hostname in jdbc url is wrong.
Hostname in jdbc url is not recognized by local DNS server.
Port number is missing or wrong in jdbc url.
DB server is down.
DB server doesn' t accept TCP/IP connections.
DB server has run out of connections.
Something in between Java and DB is blocking connections, e.g. a firewall or proxy.
To solve the one or the other, follow the following advices:
Verify and test them with ping.
Refresh DNS or use IP address in jdbc url instead.
Verify it based on my. cnf of MySQL DB.
Start the DB.
Verify if mysqld is started without the -- skip-networking option.
Restart the DB and fix your code accordingly that it closes connections in finally.
Disable firewall and/or configure firewall/proxy to allow/forward the port.
Refer:
Http://stackoverflow.com/questions/2983248/com-mysql-jdbc-exceptions-jdbc4-communicationsexception-communications-link-fai
Http://blog.csdn.net/gucapg/article/details/17509969
Http://4925054.blog.51cto.com/4915054/1154263
Zookeeper