在开发项目工程时,工程之前都是连接到局域网的测试数据库,运行没有问题。但是由于需要将项目部署到外网的服务器上,同时mysql数据库也是搭建在外网的服务器上,
当进行本地开发时,由于电脑是连接在本地的局域网内,需要连接到外网的数据库上,于是就遇到了问题:
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
com.mysql.jdbc.CommunicationsException
MESSAGE: Communications link failure due to underlying exception: ** BEGIN NESTED EXCEPTION **
java.net.SocketException
MESSAGE: Connection reset
STACKTRACE:java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:113)
at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:160)
at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:188)
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1910)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2304)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2803)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3170)
at com.mysql.jdbc.Connection.configureClientCharacterSet(Connection.java:2436)
at com.mysql.jdbc.Connection.initializePropsFromServer(Connection.java:3913)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2683)
at com.mysql.jdbc.Connection.<init>(Connection.java:1531)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
……** END NESTED EXCEPTION **
Last packet sent to the server was 203 ms ago.
STACKTRACE:
从错误可以看出,应该是数据库连接问题,于是各种谷歌、百度,终于解决问题:
1.首先要确保你能连接上远程数据库:如果远程服务器所在的电脑连接了路由器或是交互机则需要进行相应的端口映射
2.数据库连接测试没有问题后,找到mysql路径下的my.ini文件,然后再在[mysqld]下添加
wait_time=172800
interactive-timeout=172800
3.重启下mysql服务