用java连接mysql数据库,出现具体错误如下:
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: ** BEGIN NESTED EXCEPTION ** java.net.ConnectException MESSAGE: Connection refused: connect STACKTRACE: java.net.ConnectException: Connection refused: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:520) at java.net.Socket.connect(Socket.java:470) at java.net.Socket.<init>(Socket.java:367) at java.net.Socket.<init>(Socket.java:209) at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256) at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271) at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771) at com.mysql.jdbc.Connection.<init>(Connection.java:1555) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285) at java.sql.DriverManager.getConnection(DriverManager.java:525) at java.sql.DriverManager.getConnection(DriverManager.java:171) at demo.ConnectDB.main(ConnectDB.java:13) ** END NESTED EXCEPTION ** Last packet sent to the server was 3 ms ago. at com.mysql.jdbc.Connection.createNewIO(Connection.java:2847) at com.mysql.jdbc.Connection.<init>(Connection.java:1555) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285) at java.sql.DriverManager.getConnection(DriverManager.java:525) at java.sql.DriverManager.getConnection(DriverManager.java:171) at demo.ConnectDB.main(ConnectDB.java:13)
经排查是我自己再安装mysql的时候把tcp/ip协议关闭了,具体在安装过程中的截图如下:
记得在这里勾选
另外可能还有以下原因:
(1)服务器端以及运行Java程序的客户端是否启用了防火墙;
(2)JDBC的驱动是否正确合适;
(3)是否启用了IPv6?如果现在使用的IPv6,那么换成IPv4再试试看;
如果上述情况都排除之后,还是出现如上的异常错误,那么很有可能是你电脑所处的网络存在问题,你可以尝试一下换个地方上网运行这个包含JDBC的Java程序,或者换个时间再次运行
经查看相关资料,发现问题的原因如下:
Mysql服务器默认的“wait_timeout”是8小时,如果connection空闲超过8个小时,Mysql将自动断开该 connection。在C3P0 pools中的connections如果空闲超过8小时,Mysql将其断开,而C3P0中的该connection仍然存在,如果这时有 Client请求connection,C3P0将该断开的Connection提供给Client,将会造成上面的异常。
可以这么解决该问题:
使得Connection pools中connection的lifetime与Mysql的wait_timeout时间达到一致,或者测试Connection pools中connection的有效性。
在wait_timeout的时间为8小时的情况下:
C3P0增加以下配置信息:
//set to 'SELECT 1'
preferredTestQuery = 'SELECT 1'
//set to something much less than wait_timeout, prevents connections from going stale
idleConnectionTestPeriod = 18000
//set to something slightly less than wait_timeout, preventing 'stale' connections from being//handed out
maxIdleTime = 25000
//if you can take the performance 'hit', set to "true"
testConnectionOnCheckout = true
-----------------------------------------------------------------------------------------------------------------------------------
MySQL连接如果8小时未使用,在查询使用到该连接会报:
异常名称:com.mysql.jdbc.CommunicationsException
异常信息: Communications link failure due to underlying exception
如果是MySQL5以前的版本,需要修改连接池配置中的URL,添加autoReconnect=true
如果是MySQL5 以后的版本,需要修改my.cnf(或者my.ini)文件,在[mysqld]后面添加
wait_timeout = 172800
interactive-timeout = 172800
单位都是秒,记得必须都添加,否则不起作用,通过show variables查看wait_timeout的值