我已经用MySQL为我的SpringBoot应用程序编写了docker compose。当我最终运行docker-compose时,出现错误:
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:325) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
... 69 common frames omitted
Caused by: 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.
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:172) ~[mysql-connector-java-8.0.11.jar!/:8.0.11]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.11.jar!/:8.0.11]
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862) ~[mysql-connector-java-8.0.11.jar!/:8.0.11]
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444) ~[mysql-connector-java-8.0.11.jar!/:8.0.11]
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230) ~[mysql-connector-java-8.0.11.jar!/:8.0.11]
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226) ~[mysql-connector-java-8.0.11.jar!/:8.0.11]
at java.sql.DriverManager.getConnection(DriverManager.java:664) ~[na:1.8.0_111]
at java.sql.DriverManager.getConnection(DriverManager.java:208) ~[na:1.8.0_111]
at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:153) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:144) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:196) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:159) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
... 70 common frames omitted
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
起初,我认为原因是数据库,但我设法运行了应用程序的本地实例并连接到docker mysql。 接下来,我假设spring应用程序在启动并运行之前尝试连接到mysql,但是我重新启动了该应用程序(mysql已在运行)并遇到了相同的错误。 目前,我认为这可能是Windows10防火墙引起的,但我的想法不足。
Dockerfile:
FROM java:8
LABEL maintainer="maciej"
WORKDIR /app
COPY target/kamienica.jar /app/kamienica.jar
ENTRYPOINT ["java", "-jar","kamienica.jar", "--spring.profiles.active=docker"]
docker-compose.yml
version: '3'
services:
mysql-docker-container:
image: mysql:latest
environment:
- MYSQL_ROOT_PASSWORD=maciej
- MYSQL_DATABASE=kamienica
- MYSQL_USER=maciej
- MYSQL_PASSWORD=maciej
volumes:
- /data/mysql
ports:
- 3333:3306
container_name: kamienica-db
kamienica-app:
image: kamienica-image
build:
context: ./
dockerfile: Dockerfile
depends_on:
- mysql-docker-container
ports:
- 8080:8080
volumes:
- /data/spring-boot-app
container_name: kamienica-app
和application-docker.properties:
jdbc.url = jdbc:mysql://kamienica-db:3333/kamienica?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
jdbc.username = maciej
jdbc.password = maciej
hibernate.show_sql = true
hibernate.format_sql = true
hibernate.hbm2ddl.auto=create-drop
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.current_session_context_class=thread
hibernate.connection.characterEncoding=UTF-8
hibernate.connection.charSet=UTF-8
答案 0 :(得分:1)
尝试将URL更改为此属性文件
jdbc:mysql:// mysql-docker-container:3306 / kamienica
答案 1 :(得分:0)
jdbc:mysql:// [mysql-docker-container-name]:[mysql-port] / [db-name]
在您的docker-compose.yml中,mysql-docker-container名称为mysql-docker-container
因此,请按以下方式更改您的spring boot application.properties文件mysql网址
jdbc:mysql:// mysql-docker-container:3306 / kamienica