我创建了一个docker compose文件,以将MySql连接到SpringBoot应用程序。但是我得到这个错误:
com.mysql.jdbc.exceptions.jdbc4.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.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_242]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_242]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_242]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_242]
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:990) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:342) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2188) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2221) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2016) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:776) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_242]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_242]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_242]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_242]
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:386) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.1.jar!/:na]
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:353) ~[HikariCP-3.4.1.jar!/:na]
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:201) ~[HikariCP-3.4.1.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:473) [HikariCP-3.4.1.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:562) [HikariCP-3.4.1.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) [HikariCP-3.4.1.jar!/:na]
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112)
我的Dockerfile,我已经使用Maven安装创建了一个.jar文件:
FROM openjdk:8
EXPOSE 8083
ADD target/ProjectCrud.jar ProjectCrud.jar
ENTRYPOINT ["java", "-jar", "ProjectCrud.jar"]
docker-compose.yml:
version: '3'
services:
docker-mysql:
restart: always
container_name: docker-mysql
image: mysql
environment:
MYSQL_DATABASE: pfa
MYSQL_ROOT_PASSWORD: aze123qsd
MYSQL_USER: root
MYSQL_ROOT_HOST: '%'
volumes:
- ./sql:/docker-entrypoint-initdb.d
networks:
- spring-db
ports:
- "3306:3306"
healthcheck:
test: "/usr/bin/mysql --user=root --password=root--execute \"SHOW DATABASES;\""
interval: 2s
timeout: 20s
retries: 10
sign-up-app:
build: ./
expose:
- "10222"
ports:
- 10222:10222
environment:
WAIT_HOSTS: mysql:3306
depends_on:
- docker-mysql
networks:
- spring-db
networks:
spring-db:
我的application.properties:
spring.datasource.username=root
spring.datasource.password=aze123qsd
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://docker-mysql:3306/pfa?
useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
server.port=8089
我尝试使用docker default ip在本地计算机上运行我的spring boot应用程序,而在此docker-compose.yml文件中仅运行MySql容器,并且运行良好。但是当我尝试docker撰写文件时。我收到此错误
答案 0 :(得分:0)
这可能是因为Jar的构建失败,因为contextLoads()
的测试失败。我遇到了类似的问题。您可以参考this
这可能是因为测试在您的本地计算机上运行,并且无法将docker-mysql
作为参考。只能由同一网络中的容器引用。您可以修复此覆盖的测试属性,并使它们使用本地主机的数据库或内存数据库。
尝试在测试类@SpringBootTest(properties = {"spring.datasource.url=jdbc:mysql://localhost:3306/dbname"})
答案 1 :(得分:0)
我遇到类似的问题,并且有可能在主应用程序启动之前未创建数据库。就我而言,我删除了数据库卷:
然后运行docker-compose up-在mysql数据库应开始其工作之后,主应用程序应以代码-1退出
等待数据库创建,
通过按ctrl + c停止应用,最后再次运行docker-compose。
此后一切正常