docker Sping Boot 应用程序放入容器后似乎无法运行

6ovsh4lw  于 2022-12-03  发布在  Docker
关注(0)|答案(2)|浏览(157)

我正在尝试创建Docker容器,我尝试为MySql设置1,为Spring io设置另一个。DB容器运行正常,但Sping Boot 容器在某个点出现并退出。我搜索并尝试了许多方法,但似乎无法解决它,我得出的结论是,似乎数据库环境或www.example.com有问题aplication.properties,或者可能有问题别的地方。如果有人能指导我找到解决办法,我将非常感激。
application.properties:

spring.datasource.url=jdbc:mysql://DB_containerfile:3306/phase2?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
spring.datasource.username=testuser
spring.datasource.password= testuser@123
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver

停靠文件:

FROM openjdk:17
ADD target/springboot-crud-api-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","app.jar"]

docker-compose.yml:

version: '3.8'

services:
DB_containerfile:
image: mysql:latest
container_name: DB_containerfile
environment:
- MYSQL_ROOT_PASSWORD=////////////
- MYSQL_DATABASE=phase2
- MYSQL_USER=testuser
- MYSQL_PASSWORD=testuser@123

backend_containerfile:
image: backend_image
container_name: backend_containerfile
ports:
- 8080:8080
build:
context: ./
dockerfile: Dockerfile
depends_on:
- DB_containerfile

注意:我将在cmd“mysql -u root -p”上写入此命令时输入的密码分配给了MYSQL_ROOT_PASSWORD
Spring Boot 日志:

backend_containerfile  | 2022-12-02 06:02:54.757  WARN 1 --- [           main] com.zaxxer.hikari.util.DriverDataSource  : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
backend_containerfile  | 2022-12-02 06:02:56.040 ERROR 1 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.
backend_containerfile  | 
backend_containerfile  | java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed
backend_containerfile  |        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]  
backend_containerfile  |        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]
backend_containerfile  |        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:828) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]        
backend_containerfile  |        at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:448) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]
backend_containerfile  |        at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]        
backend_containerfile  |        at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]backend_containerfile  |        at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-4.0.3.jar!/:na]
backend_containerfile  |        at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:364) ~[HikariCP-4.0.3.jar!/:na]
backend_containerfile  |        at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-4.0.3.jar!/:na]
backend_containerfile  |        at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:476) ~[HikariCP-4.0.3.jar!/:na]
backend_containerfile  |        at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561) ~[HikariCP-4.0.3.jar!/:na]
backend_containerfile  |        at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-4.0.3.jar!/:na]
backend_containerfile  |        at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-4.0.3.jar!/:na]
backend_containerfile  |        at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.6.12.Final.jar!/:5.6.12.Final]

我尝试过:1)我试过Docker卷修剪。2)我试过删除

- MYSQL_USER=testuser
- MYSQL_PASSWORD=testuser@123
  spring.datasource.username=testuser
  spring.datasource.password= testuser@123\`

而且只跟

- MYSQL_ROOT_PASSWORD=////////////

3)我两个都试过了

spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver spring.datasource.driver-class-name= com.mysql.jdbc.Driver

4)我在spring.datasource.url中设置了allowPublicKeyRetrieval=true,我尝试了很多其他方法,但结果都是一样的。
抱歉,问了这么长的问题,但我真的想自己解决这个问题,但现在我需要别人的见解。谢谢你。

tzdcorbm

tzdcorbm1#

尝试在application.properties中使用此驱动程序类名

spring.datasource.driverClassName=com.mysql.jdbc.Driver

您是否尝试过只启动容器中的Mysql db并从IDE连接到它?

yuvru6vn

yuvru6vn2#

谢天谢地,我发现了这个问题,因为我还没有连接数据库和spring io。我会发布我的情况下的解决方案,以防它帮助别人。

spring.datasource.url=jdbc:mysql://DB_containerfile:3306/phase2?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
spring.datasource.username=testuser
spring.datasource.password= testuser@123
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
FROM openjdk:17
ADD target/*.jar app.jar
ENTRYPOINT ["java","-jar","app.jar"]

然后在项目终端中运行以下命令:

docker-compose down
mvn clean
mvn clean install -DskipTests=true
docker network ls
docker network create spring-net  //if not created
docker-compose up //then CTRL+C
docker network connect spring-net DB_containerfile
docker container inspect DB_containerfile
docker ps -a
docker-compose up

相关问题