I've installed a stock mysql 5.5 installation, and while I can connect to the mysql service via the mysql command, and the service seems to be running, I cannot connect to it through spring+tomcat or from an external jdbc connector.
I'm using the following URL:
jdbc:mysql://myserver.com:myport/mydb
with proper username/password, but I receive the following message:
server.com: 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.
and tomcat throws:
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.
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
Which seems to be the same issue as if I try to connect externally.
This can happen for a variety of reasons. I just saw it myself a few weeks ago but I can't remember what the fix was for me.
1) Verify the address mysql is bound to, it's probably 127.0.0.1 (only) which I believe is the default (at least on standard Ubuntu server). You'll have to comment out the bind-address parameter in my.cnf to bind to all available addresses (you can't choose multiple, it's one or all).
2) If it is bound to 127.0.0.1 and you can't connect using "localhost", make sure it's not resolving to the IPv6 localhost address instead of IPv4. (or just use the IP address)
3) Double and triple-check the port that mysql is listening on.
4) Make sure you're using the right JDBC connector for your JDK.
5) Make sure you're not doing something really silly like starting mysql with --skip-networking.
I think my first suggestion has the most promise...in fact I think that's where I saw it recently...I was trying to connect to mysql remotely (also on Ubuntu 8.04).
I have had the same problem in two of my programs. My error was this:
I spend some days to solve this problem. I have tested many approaches that have been mentioned in different web sites, but non of them worked. Finally I changed my code and found out what was the problem. I'll try to tell you about different approaches and sum them up here.
While I was seeking the internet to find the solution for this error, I figured out that there are many solutions that worked for at least one person, but others say that it doesn't work for them! why there are many approaches to this error? It seems this error can occur generally when there is a problem in connecting to the server. Maybe the problem is because of the wrong query string or too many connections to the database.
So I suggest you to try all the solutions one by one and don't give up!
Here are the solutions that I found on the internet and for each of them, there is at least on person who his problem has been solved with that solution.
point: For the solutions that you need to change the MySQL settings, you can refer to the following not:
Linux: /etc/my.cnf
Windows: D:\Program Files\mysql\bin\my.ini
Here are the solutions:
Uncomment "bind-address" attribute or change it to one of the following Ips:
bind-address="127.0.0.1"
or
bind-address="0.0.0.0"
If there is a "skip-networking" line in your MySQL config file, make it comment by adding "#" sign at the beginning of that line.
Add these lines to the MySQL config file:
wait_timeout = number
interactive_timeout = number
connect_timeout = number
Make sure the Fire wall, or Anti virus soft wares don't block MySQL service.
Check your query string. your connection string should be some thing like this:
Make sure you don't have spaces in your string. All the connection string should be continues without any space characters.
Try to replace "localhost" with your port, like 127.0.0.1. Also try to add port number to your connection string, like:
Usually default port for MySQL is 3306.
Don't forget to change username and password to the username and password of your MySQL server.
"max_allowed_packet" is a variable in MySQL config file that indicates the maximum packet size, not the maximum number of packets. So it will not help to solve this error.
change TOMCAT6_SECURITY=yes to TOMCAT6_SECURITY=no
use validationQuery="select now()" to make sure each query has responses
Add this code to your connection string:
Although non of these solutions worked for me, I suggest you to try them. Because there are some people how solved their problem with following these steps.
But what solved my problem? My problem was that I had many SELECTs on database. Each time I created connection and then closed it. Although I closed the connection every time, but the system faced with many connections and gave me that error. What I did was that I defined my connection variable as a public (or private) variable for whole class and initialized it in the constructor. Then every time I just used that connection. It solved my problem and also increased my speed dramatically.
Conclusion
There is no simple and unique way to solve this problem. I suggest you to think about your own situation and choose above solutions. If you take this error at the beginning of the program and you are not able to connect to the database at all, you might have problem in your connection string. But If you take this error after several successful interaction to the database, the problem might be with number of connections and you may think about changing "wait_timeout" and other MySQL settings or rewrite your code how that reduce number of connections.
If you are running a Linux installation, you probably have lokkit blocking incoming communications except via SSH.
Log in as root, and run the command lokkit from the prompt, disable firewall and SElinux and see if you have the same problem.
Also check your permissions have been set correctly, so everything can write to the correct locations.
There is also this huge bug in version 5.1.9 of mysql-jdbc driver :
http://bugs.mysql.com/bug.php?id=47494
This can also be caused by wrong proxy settings. I had this problem trying to connect via jdbc to a MySQL instance running in a Parallels virtual appliance on my Mac. The jdbc connection uses the system-level network settings and since I was behind a SOCKS proxy I had to set the MySql host as a non-proxy host (e.g., on a Mac you can configure that in Settings->Network->Advanced->Proxies, and finally add the hostname or IP address in the "Bypass proxy settings for these Hosts and Domains").
MySQL Connector/J supports only TCP/IP Java does not support Unix domain sockets connectivity
If the MYSQL is started with skip-networking flag or if the MySQL is running behind the firewall, then TCP/IP option is disabled. So that Java cannot communicate with MySQL.
i had a very similar problem for almost a day, and it drove me crazy! but i managed to found the solution, and it was very, very simple, you just need to /etc/init.d/tomcat6 to change TOMCAT6_SECURITY=yes to TOMCAT6_SECURITY=no. it's not my solution, i found it here, as you can see, i am running ubuntu, hope this works.
I had the same problem. Changed the "bind-address" property in /etc/mysql/my.cnf file to 0.0.0.0, and it works. Corresponding line in my.cnf looks like this:
bind-address = 0.0.0.0
Before it was set to the outside ip address of the server, so it looked something like:
bind-address = 196.152.4.145
I think when it's set to the outside ip address and not the localhost loop, mysql server is just connected to the network card and does not listen to the connections from the local loop.
try your local address to bind address in my.cnf file
Connection con = null;