使用docker-compose连接springboot到mysql的问题

问题描述 投票:0回答:1

我正在使用 springboot 与 maven 和 mysql 数据库开发一个小型应用程序。 我创建了一个可以工作的 Dockerfile,但是当我尝试执行 docker-compose up 时,从应用程序到数据库的连接被拒绝。

这是我的 docker-compose

version: '3.8'

services:
  app:
    image: jayaz98/angelodaleotest:0.0.1
    ports:
      - 8080:8080
    environment:
      SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/angelodaleoptdb?autoReconnect=true&useSSL=false
      SPRING_DATASOURCE_USERNAME: admin
      SPRING_DATASOURCE_PASSWORD: root
    depends_on:
      db:
        condition: service_healthy

  db:
    image: mysql:8.0.27
    environment:
      DATABASE_HOST: docker-mysql
      DATABASE_PORT: 3306
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: angelodaleoptdb
      MYSQL_USER: admin
      MYSQL_PASSWORD: root
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 10s
      retries: 5
      timeout: 5s

我的应用程序.属性

spring.application.name=project
spring.datasource.url=jdbc:mysql://localhost:3306/angelodaleoptdb?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.hikari.connectionTimeout=100000
spring.datasource.hikari.initializationFailTimeout=0
spring.datasource.hikari.maximumPoolSize=10
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
server.servlet.context-path=/angelodaleopt

这是我从应用程序收到的错误,带有数据库日志

2024-09-26 00:40:53 db-1   | 2024-09-25 22:40:53+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.27-1debian10 started.
2024-09-26 00:40:53 db-1   | 2024-09-25 22:40:53+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2024-09-26 00:40:53 db-1   | 2024-09-25 22:40:53+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.27-1debian10 started.
2024-09-26 00:40:53 db-1   | 2024-09-25 22:40:53+00:00 [Note] [Entrypoint]: Initializing database files
2024-09-26 00:40:53 db-1   | 2024-09-25T22:40:53.473629Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.27) initializing of server in progress as process 42
2024-09-26 00:40:53 db-1   | 2024-09-25T22:40:53.481794Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2024-09-26 00:40:53 db-1   | 2024-09-25T22:40:53.845476Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2024-09-26 00:40:54 db-1   | 2024-09-25T22:40:54.943102Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
2024-09-26 00:40:54 db-1   | 2024-09-25T22:40:54.943145Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
2024-09-26 00:40:54 db-1   | 2024-09-25T22:40:54.994521Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2024-09-26 00:40:58 db-1   | 2024-09-25 22:40:58+00:00 [Note] [Entrypoint]: Database files initialized
2024-09-26 00:40:58 db-1   | 2024-09-25 22:40:58+00:00 [Note] [Entrypoint]: Starting temporary server
2024-09-26 00:40:58 db-1   | 2024-09-25T22:40:58.361816Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.27) starting as process 91
2024-09-26 00:40:58 db-1   | 2024-09-25T22:40:58.373966Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2024-09-26 00:40:58 db-1   | 2024-09-25T22:40:58.506201Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2024-09-26 00:40:58 db-1   | 2024-09-25T22:40:58.719550Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
2024-09-26 00:40:58 db-1   | 2024-09-25T22:40:58.719583Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
2024-09-26 00:40:58 db-1   | 2024-09-25T22:40:58.720212Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2024-09-26 00:40:58 db-1   | 2024-09-25T22:40:58.720267Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2024-09-26 00:40:58 db-1   | 2024-09-25T22:40:58.722339Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2024-09-26 00:40:58 db-1   | 2024-09-25T22:40:58.734210Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: /var/run/mysqld/mysqlx.sock
2024-09-26 00:40:58 db-1   | 2024-09-25T22:40:58.734321Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.27'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server - GPL.
2024-09-26 00:40:58 db-1   | 2024-09-25 22:40:58+00:00 [Note] [Entrypoint]: Temporary server started.
2024-09-26 00:40:59 db-1   | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
2024-09-26 00:40:59 db-1   | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
2024-09-26 00:41:00 db-1   | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
2024-09-26 00:41:00 db-1   | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
2024-09-26 00:41:00 db-1   | 2024-09-25 22:41:00+00:00 [Note] [Entrypoint]: Creating database angelodaleoptdb
2024-09-26 00:41:00 db-1   | 2024-09-25 22:41:00+00:00 [Note] [Entrypoint]: Creating user admin
2024-09-26 00:41:00 db-1   | 2024-09-25 22:41:00+00:00 [Note] [Entrypoint]: Giving user admin access to schema angelodaleoptdb
2024-09-26 00:41:00 db-1   | 
2024-09-26 00:41:00 db-1   | 2024-09-25 22:41:00+00:00 [Note] [Entrypoint]: Stopping temporary server
2024-09-26 00:41:00 db-1   | 2024-09-25T22:41:00.658231Z 13 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.27).
2024-09-26 00:41:02 db-1   | 2024-09-25T22:41:02.352849Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.27)  MySQL Community Server - GPL.
2024-09-26 00:41:02 db-1   | 2024-09-25 22:41:02+00:00 [Note] [Entrypoint]: Temporary server stopped
2024-09-26 00:41:02 db-1   | 
2024-09-26 00:41:02 db-1   | 2024-09-25 22:41:02+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.
2024-09-26 00:41:02 db-1   | 
2024-09-26 00:41:02 db-1   | 2024-09-25T22:41:02.878815Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.27) starting as process 1
2024-09-26 00:41:02 db-1   | 2024-09-25T22:41:02.885905Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2024-09-26 00:41:03 db-1   | 2024-09-25T22:41:03.000695Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2024-09-26 00:41:03 db-1   | 2024-09-25T22:41:03.177047Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
2024-09-26 00:41:03 db-1   | 2024-09-25T22:41:03.177090Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
2024-09-26 00:41:03 db-1   | 2024-09-25T22:41:03.177808Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2024-09-26 00:41:03 db-1   | 2024-09-25T22:41:03.177859Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2024-09-26 00:41:03 db-1   | 2024-09-25T22:41:03.179999Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2024-09-26 00:41:03 db-1   | 2024-09-25T22:41:03.191853Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2024-09-26 00:41:03 db-1   | 2024-09-25T22:41:03.192079Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.27'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
2024-09-26 00:41:04 app-1  | 
2024-09-26 00:41:04 app-1  |   .   ____          _            __ _ _
2024-09-26 00:41:04 app-1  |  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
2024-09-26 00:41:04 app-1  | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
2024-09-26 00:41:04 app-1  |  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
2024-09-26 00:41:04 app-1  |   '  |____| .__|_| |_|_| |_\__, | / / / /
2024-09-26 00:41:04 app-1  |  =========|_|==============|___/=/_/_/_/
2024-09-26 00:41:04 app-1  |  :: Spring Boot ::                (v2.7.0)
2024-09-26 00:41:04 app-1  | 
2024-09-26 00:41:05 app-1  | 2024-09-25 22:41:05.008  INFO 1 --- [           main] i.a.project.ProjectApplication           : Starting ProjectApplication v0.0.1-SNAPSHOT using Java 11.0.16 on 4815623c35dd with PID 1 (/opt/opt/app.jar started by root in /opt)
2024-09-26 00:41:05 app-1  | 2024-09-25 22:41:05.014  INFO 1 --- [           main] i.a.project.ProjectApplication           : No active profile set, falling back to 1 default profile: "default"
2024-09-26 00:41:05 app-1  | 2024-09-25 22:41:05.977  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2024-09-26 00:41:06 app-1  | 2024-09-25 22:41:06.038  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 51 ms. Found 4 JPA repository interfaces.
2024-09-26 00:41:06 app-1  | 2024-09-25 22:41:06.587  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2024-09-26 00:41:06 app-1  | 2024-09-25 22:41:06.600  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-09-26 00:41:06 app-1  | 2024-09-25 22:41:06.601  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.63]
2024-09-26 00:41:06 app-1  | 2024-09-25 22:41:06.671  INFO 1 --- [           main] o.a.c.c.C.[.[.[/angelodaleopt]           : Initializing Spring embedded WebApplicationContext
2024-09-26 00:41:06 app-1  | 2024-09-25 22:41:06.671  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1579 ms
2024-09-26 00:41:06 app-1  | 2024-09-25 22:41:06.839  INFO 1 --- [           main] i.a.project.cors.SimpleCORSFilter        : SimpleCORSFilter init
2024-09-26 00:41:06 app-1  | 2024-09-25 22:41:06.974  INFO 1 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2024-09-26 00:41:07 app-1  | 2024-09-25 22:41:07.020  INFO 1 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.6.9.Final
2024-09-26 00:41:07 app-1  | 2024-09-25 22:41:07.215  INFO 1 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2024-09-26 00:41:07 app-1  | 2024-09-25 22:41:07.302  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2024-09-26 00:41:12 app-1  | 2024-09-25 22:41:12.471  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2024-09-26 00:42:52 app-1  | 2024-09-25 22:42:52.482  WARN 1 --- [           main] o.h.e.j.e.i.JdbcEnvironmentInitiator     : HHH000342: Could not obtain connection to query metadata
2024-09-26 00:42:52 app-1  | 
2024-09-26 00:42:52 app-1  | java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 100002ms.
2024-09-26 00:42:52 app-1  |    at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:696) ~[HikariCP-4.0.3.jar!/:na]
2024-09-26 00:42:52 app-1  |    at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:197) ~[HikariCP-4.0.3.jar!/:na]
2024-09-26 00:42:52 app-1  |    at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:162) ~[HikariCP-4.0.3.jar!/:na]
2024-09-26 00:42:52 app-1  |    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:128) ~[HikariCP-4.0.3.jar!/:na]
2024-09-26 00:42:52 app-1  |    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.6.9.Final.jar!/:5.6.9.Final]
2024-09-26 00:42:52 app-1  |    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:181) ~[hibernate-core-5.6.9.Final.jar!/:5.6.9.Final]
2024-09-26 00:42:52 app-1  |    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:68) ~[hibernate-core-5.6.9.Final.jar!/:5.6.9.Final]
2024-09-26 00:42:52 app-1  |    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) ~[hibernate-core-5.6.9.Final.jar!/:5.6.9.Final]
2024-09-26 00:42:52 app-1  |    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:101) ~[hibernate-core-5.6.9.Final.jar!/:5.6.9.Final]
2024-09-26 00:42:52 app-1  |    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263) ~[hibernate-core-5.6.9.Final.jar!/:5.6.9.Final]
2024-09-26 00:42:52 app-1  |    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237) ~[hibernate-core-5.6.9.Final.jar!/:5.6.9.Final]
2024-09-26 00:42:52 app-1  |    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214) ~[hibernate-core-5.6.9.Final.jar!/:5.6.9.Final]
2024-09-26 00:42:52 app-1  |    at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.injectServices(DefaultIdentifierGeneratorFactory.java:175) ~[hibernate-core-5.6.9.Final.jar!/:5.6.9.Final]
...

我尝试了我所知道的一切,但还不够

mysql spring-boot docker docker-compose hikaricp
1个回答
0
投票

您正在尝试在应用程序属性中使用

localhost

spring.datasource.url=jdbc:mysql://localhost:3306/angelodaleoptdb?useSSL=false&serverTimezone=UTC

使用服务名称

© www.soinside.com 2019 - 2024. All rights reserved.