返回

如何从停靠的spring boot应用程序连接docker db?

发布时间:2022-04-19 05:31:54 328
# java# spring# java# 数据库# sql

我是docker方面的初学者,并努力学习,但我太难了。

当我只从docker compose运行db时。yml并运行名为dockerdeneme直接从IDE连接到db,没问题。但当我试着从docker compose开始运行时。yml文件,则应用程序无法连接到数据库。我的码头工人。yml文件是:

version: '3.9'

services:
  mysql:
    platform: linux/amd64
    image: mysql:8
    container_name: mysqlcustomerdb
    domainname: nishcustomer
    hostname: localhost
    ports:
      - '3306:3306'
    volumes:
      - ~/apps/mysql:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=1234
      - MYSQL_PASSWORD=1234
      - MYSQL_DATABASE=mysqlcustomerdb
      - DATABASE_HOST=mysqlcustomerdb
      - MYSQL_ROOT_HOST=%
      - lower_case_table_names=1
      - init-connect='GRANT CREATE USER ON . TO 'root'@'%';FLUSH PRIVILEGES;'

  dockerdeneme:
    container_name: dockerdeneme
    domainname: nishcustomer
    hostname: localhost
    image: dockerdeneme
    build: ./
    ports:
      - '8080:8080'
    environment:
      - MYSQL_ROOT_PASSWORD=1234
      - MYSQL_PASSWORD=1234
      - MYSQL_USER=root
      - MYSQL_DATABASE=mysqlcustomerdb
    depends_on:
      - mysql
volumes:
  cache:
    driver: local

结果docker ps是:

CONTAINER ID   IMAGE           COMMAND                  CREATED             STATUS             PORTS                               NAMES
fb0037e10702   mysql:8         "docker-entrypoint.s…"   22 seconds ago      Up 21 seconds      0.0.0.0:3306->3306/tcp, 33060/tcp   mysqlcustomerdb

如果我使用以下应用程序从IDE运行应用程序。那么它运行良好:

spring.datasource.url=jdbc:mysql://localhost:3306/mysqlcustomerdb
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

它是失败的停靠应用程序的输出:

2022-04-14 21:28:56.106  INFO 1 --- [           main] c.e.d.DockerdenemeApplication            : Starting DockerdenemeApplication v0.0.1-SNAPSHOT using Java 11.0.14.1 on localhost with PID 1 (/app.jar started by root in /)

2022-04-14 21:28:56.107  INFO 1 --- [           main] c.e.d.DockerdenemeApplication            : No active profile set, falling back to 1 default profile: "default"

2022-04-14 21:28:56.467  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.

2022-04-14 21:28:56.493  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 21 ms. Found 1 JPA repository interfaces.

2022-04-14 21:28:57.024  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)

2022-04-14 21:28:57.037  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]

2022-04-14 21:28:57.037  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.60]

2022-04-14 21:28:57.105  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext

2022-04-14 21:28:57.105  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 937 ms

2022-04-14 21:28:57.250  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...

2022-04-14 21:28:57.256  WARN 1 --- [           main] com.zaxxer.hikari.util.DriverDataSource  : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.

2022-04-14 21:28:58.334 ERROR 1 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.


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:174) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]

我觉得我在端口映射或主机名/域名方面出了问题,但我被卡住了。

有人能告诉我我做错了什么,端口或主机名等吗?

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(2)
按点赞数排序
用户头像