【docker-compose】使用docker-compose启动spring-boot+mysql,服务内连数据库报错:create connection SQLException, url: jdbc:mysql://mysql:33061/swapping?useSSL=false&useUnicode=true&characterEncoding=UTF-8, errorCode 0,

时间:2024-12-26 16:37:14

完整配置在这里,

https://www.cnblogs.com/sxdcgaq8080/p/10070948.html

启动报错如下:

2018-12-05 01:04:05.569 ERROR 1 --- [reate-259219561] com.alibaba.druid.pool.DruidDataSource   : create connection SQLException, url: jdbc:mysql://mysql:33061/swapping?useSSL=false&useUnicode=true&characterEncoding=UTF-8, errorCode 0, state 08S01
swapping_1 |
swapping_1 | com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
swapping_1 |
swapping_1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
swapping_1 | at sun.reflect.GeneratedConstructorAccessor49.newInstance(Unknown Source) ~[na:na]
swapping_1 | at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_111]
swapping_1 | at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_111]
swapping_1 | at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
swapping_1 | at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:990) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
swapping_1 | at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:342) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
swapping_1 | at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2188) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
swapping_1 | at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2221) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
swapping_1 | at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2016) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
swapping_1 | at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:776) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
swapping_1 | at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
swapping_1 | at sun.reflect.GeneratedConstructorAccessor46.newInstance(Unknown Source) ~[na:na]
swapping_1 | at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_111]
swapping_1 | at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_111]
swapping_1 | at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
swapping_1 | at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:386) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
swapping_1 | at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
swapping_1 | at com.alibaba.druid.filter.FilterChainImpl.connection_connect(FilterChainImpl.java:149) ~[druid-1.1.9.jar!/:1.1.9]
swapping_1 | at com.alibaba.druid.filter.FilterAdapter.connection_connect(FilterAdapter.java:786) ~[druid-1.1.9.jar!/:1.1.9]
swapping_1 | at com.alibaba.druid.filter.FilterEventAdapter.connection_connect(FilterEventAdapter.java:38) ~[druid-1.1.9.jar!/:1.1.9]
swapping_1 | at com.alibaba.druid.filter.FilterChainImpl.connection_connect(FilterChainImpl.java:143) ~[druid-1.1.9.jar!/:1.1.9]
swapping_1 | at com.alibaba.druid.filter.FilterAdapter.connection_connect(FilterAdapter.java:786) ~[druid-1.1.9.jar!/:1.1.9]
swapping_1 | at com.alibaba.druid.filter.FilterChainImpl.connection_connect(FilterChainImpl.java:143) ~[druid-1.1.9.jar!/:1.1.9]
swapping_1 | at com.alibaba.druid.filter.stat.StatFilter.connection_connect(StatFilter.java:218) ~[druid-1.1.9.jar!/:1.1.9]
swapping_1 | at com.alibaba.druid.filter.FilterChainImpl.connection_connect(FilterChainImpl.java:143) ~[druid-1.1.9.jar!/:1.1.9]
swapping_1 | at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1515) ~[druid-1.1.9.jar!/:1.1.9]
swapping_1 | at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1578) ~[druid-1.1.9.jar!/:1.1.9]
swapping_1 | at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:2466) ~[druid-1.1.9.jar!/:1.1.9]
swapping_1 | Caused by: java.net.ConnectException: Connection refused (Connection refused)
swapping_1 | at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_111]
swapping_1 | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_111]
swapping_1 | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_111]
swapping_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_111]
swapping_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_111]
swapping_1 | at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_111]
swapping_1 | at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
swapping_1 | at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:301) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
swapping_1 | ... 22 common frames omitted

原因是因为:

虽然是spring boot和mysql使用docker-compose部署,但服务本身不识别mysql容器,应该使用实际的IP做连接!!

最重要的是,如果你打包重新上传,多次的执行了

docker-compose up
docker-compose stop
docker-compose rm

之后依旧报这个错,原因可能是因为,虽然多次的停止了启动,重启,移除,但是 实际上你的镜像文件并没有移除。

需要

docker images

查看spring boot服务的镜像文件,使用命令移除掉,然后重新上传jar包并重新创建镜像才可以。

docker rmi 镜像名