gpt4 book ai didi

mysql - 在docker-compose中使用MySQL初始化Hibernate时拒绝连接

转载 作者:行者123 更新时间:2023-12-02 19:00:42 26 4
gpt4 key购买 nike

我有一个用于Hibernate和MySQL 8服务器的SpringBoot应用程序的docker-compose。但是每当我通过docker-compose来启动我的MySQL服务器和SpringBoot应用程序时,我都会得到:

HHH000342: Could not obtain connection to query metadata : Communications link failure
...
Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]:
Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
...
Caused by: java.net.ConnectException: Connection refused

这是我的docker-compose.yaml:
version: '3'
services:
my-account-microservice-db:
container_name: my-account-microservice-db
volumes:
- /Users/syu/Desktop/my-mysql/storage/my-account-microservice-db:/var/lib/mysql
image: mysql:8.0.19
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: my-account
ports:
- "3307:3306"
networks:
- my-network

my-account-microservice:
container_name: my-account-microservice
image: my-account-microservice
build:
context: ./account-microservice
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://my-account-microservice-db:3307/my-account
SPRING_DATASOURCE_USERNAME: root
SPRING_DATASOURCE_PASSWORD: password
SPRING_DATASOURCE_DRIVER-CLASS-NAME: com.mysql.cj.jdbc.Driver
# SPRING.DATASOURCE.URL: jdbc:mysql://my-account-microservice-db:3307/my-account
# SPRING.DATASOURCE.USERNAME: root
# SPRING.DATASOURCE.PASSWORD: password
# SPRING.DATASOURCE.DRIVER-CLASS-NAME: com.mysql.cj.jdbc.Driver
# DATABASE_HOST: my-account-microservice-db
# DATABASE_USER: root
# DATABASE_PASSWORD: password
# DATABASE_PORT: 3307
depends_on:
- my-account-microservice-db
ports:
- "8090:8090"
networks:
- my-network
# links:
# - my-account-microservice-db:database

networks:
my-network:

和我的application.properties:
spring.application.name=account-microservice

server.port=8090

# ==============================================================
# = Data Source
# ==============================================================
# ******** Will probably put this on a central config server? would that work?
spring.datasource.url = jdbc:mysql://localhost:3307/my-account
spring.datasource.username = root
spring.datasource.password = password
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver

# ==============================================================
# = Show or not log for each sql query
# ==============================================================
spring.jpa.show-sql = true

# ==============================================================
# = Keep the connection alive if idle for a long time (needed in production)
# ==============================================================
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

# ==============================================================
# = Hibernate ddl auto (create, create-drop, update)
# = The SQL dialect makes Hibernate generate better SQL for the chosen database
# ==============================================================
spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl = true
spring.jpa.hibernate.ddl-auto = update

我已验证以下内容:
  • MySQL在SpringBoot应用程序启动之前启动
  • 使用-links尝试,但不起作用
  • 什么都没有使用端口3307
  • 我能够使用/bin/mysql -h0.0.0.0 -uroot -ppassword完美地连接到localhost:3307
  • 将它们放在同一网络上
  • 重新启动Docker
  • 通过在服务器上运行
  • 来确保root用户具有特权。
  • 与单个DockerFiles一起运行
  • 具有正确的mysql连接器和Hibernate依赖项

  • 但是我上面尝试过的所有方法都没有用。
    *虽然很奇怪,但是如果我不使用Docker自行启动应用程序,则我的SpringBoot应用程序 能够将连接到mysql数据库(仅使用IntelliJ的SpringBoot配置)

    我不知道我在做什么错。请帮忙^

    最佳答案

    您正在主机端口3307上暴露数据库容器的端口3306。但是,容器端口仍然是3306。因此,当您连接到数据库容器时,应该连接到端口3306。

    如果要从运行它的主机连接到数据库容器,请使用localhost:3307。但是,这不适用于主机上运行的容器。对于这些,请使用container:3306。

    关于mysql - 在docker-compose中使用MySQL初始化Hibernate时拒绝连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60498376/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com