Solving a “communications link failure” with jdbc and mysql :Cannot connect to database server Commu
当出现:
Cannot connect to database server
Communications link failure
错误时,可以考虑下面的文章:
http://stackoverflow.com/questions/6865538/solving-a-communications-link-failure-with-jdbc-and-mysql
I'm trying to connect to the local mysql server but I keep getting an error here is the code
and the errors...
I've set the classpath, made sure my.cnf had the skip network option commented out. java version is 1.2.0_26 (64 bit) mysql 5.5.14 mysql connector 5.1.17 I made sure that the user had access to my database.
java mysql jdbc
|
|||||||||||||||
show 1 more comment |
4 Answers
up vote14down vote
|
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:
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. ConclusionThere 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. |
Solving a “communications link failure” with jdbc and mysql :Cannot connect to database server Commu相关推荐
- 【Mysql】Communications link failure,The last packet sent successfully to the server was 0 millisecond
项目背景是数据库和项目不在同一台服务器下,在启动时,突然遇到以下错误: Exception in thread "main" com.mysql.jdbc.exceptions.j ...
- com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure 数据库jdbc连接异常,数据库连接异 ...
- Communications link failure,The last packet successfully received from the serve
最近做测试,发现Mysql 过一段时间会无法连接,导致数据库数据不一至,极其郁闷. 使用Connector/J连接MySQL数据库,程序运行较长时间后就会报以下错误: Communications l ...
- Connection timed out: connect; Communications link failure
今天过来启动自己的项目出现如下的问题: Communications link failure The last packet sent successfully to the server was ...
- Communications link failure的解决办法
使用Connector/J连接MySQL数据库,程序运行较长时间后就会报以下错误: Communications link failure,The last packet successfully r ...
- MySQL数据库提示:Communications link failure,The last packet succe
Last modified:2013-10-08 14:16:47 ********************************************** web网站使用M ...
- com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 数据库报错
-- 查询mysql 数据库链接空闲时间持有最大空闲时间,单位为秒 SHOW VARIABLES WHERE VAriable_name = 'interactive_timeout'; -- 会出现 ...
- com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
昨天数据库做了一些改动,早上再打开的项目的时候就出现了这样的问题 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communicat ...
- Underlying cause: com.mysql.cj.jdbc.exceptions.CommunicationsException : Communications link failure
Hive试图schematool -dbType mysql -initSchema时候报错如下: Metastore connection URL: jdbc:mysql://Desktop ...
最新文章
- WPF的Timer控件的使用
- java批量执行查询sql语句_详解MyBatis直接执行SQL查询及数据批量插入
- boost::weak_from_raw相关的测试程序
- 人工智能——图像分析第二期练习
- 【转】无刷新验证用户名可用性
- YY的GCD(洛谷-P2257)
- HibernateDaoSupport类的使用(转)
- vba for wps 7.0_两表数据核对,WPS表格真香
- VR养狗,养的是寂寞还是潮流?
- UI/UE设计师修炼指南-CSDN公开课-专题视频课程
- 分水岭算法c语言,分水岭算法的概念及原理
- 关于Markdown编辑器
- 英语基础太差,到底能不能学好编程?
- linux安装beyondcompare
- 两个对象值相同(x.equals(y) == true),但却可有不同的hash code,这句话对不对
- 工信部:深入推进制造和网络强国建设
- 百度竞价排名曝光_百度爱采购的三大优势,及如何发优质商品。
- CSDN专属idea插件上线啦~~
- 排查OOM流程,直播界面
- 女学霸考692分想当“程序媛”,女生到底要不要学计算机?