错误现象:
在开发的时候没有报错,能正常运行并且能连接服务器。但是部署到服务器报错。 错误如下:
2023-04-07 11:54:40.617 ERROR 7 --- [reate-511707818] com.alibaba.druid.pool.DruidDataSource : create connection SQLException, url: jdbc:mysql://192.168.0.10:3306/test-demo?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8, errorCode 0, state 08S01
com.mysql.cj.jdbc.exceptions.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.
.... Caused by: java.net.ConnectException: 连接超时 (Connection timed out)
解决方法:
将连接mysql的url中的useSSL=true修改为useSSL=false
原因:
useSSL=true会进行安全验证,一般通过证书或者令牌使用这种方式,如果是使用用户名和密码连接服务器时需要修改useSSL=false,此时mysql会通过用户名和密码来验证连接
特別注意:
以下两点最容易被忽略
(1)如果是使用的阿里云,腾讯云等服务器,mysql服务器之间要保证内网可访问。
(2)如果使用外网需要配置ip白名单
注意:本文归作者所有,未经作者允许,不得转载