Docker撰写服务没有连接到mysql容器



我试图通过docker-compose将我的服务连接到MySQL docker容器。连接被拒绝。我尝试运行MySQL docker映像并将我正在运行的服务连接到本地。连接已经建立,一切正常。问题是当我想同时运行服务和MySQL容器时,连接被拒绝。我创建了自定义网络,并尝试了许多解决方案,但它已经花了我2天!

docker组合配置如下:

version : '3.8'
services:
healthcare:
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:8080"
depends_on:
- mysql
restart: always
environment:
RDS_URL: mysql
networks:
- saman-network

mysql:
image: mysql:8-oracle
container_name:
mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: hospital_db
MYSQL_USER: saman
MYSQL_PASSWORD: saman
volumes:
- myvolume:/var/lib/mysql
ports:
- "3308:3306"
networks:
- saman-network
volumes:
myvolume:

networks:
saman-network:

应用程序。

server.port=8080
spring.application.name=healthcare
spring.datasource.url=jdbc:mysql://${RDS_URL:localhost}:3308/hospital_db
spring.datasource.username=saman
spring.datasource.password=saman
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

得到的错误:

springboot-hospitalmng-sample-healthcare-1  |   ... 46 common frames omitted
springboot-hospitalmng-sample-healthcare-1  | Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure

springboot-hospitalmng-sample-healthcare-1  |   ... 59 common frames omitted
springboot-hospitalmng-sample-healthcare-1  | Caused by: java.net.ConnectException: Connection refused
springboot-hospitalmng-sample-healthcare-1  |   at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
springboot-hospitalmng-sample-healthcare-1  |   at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672) ~[na:na]
springboot-hospitalmng-sample-healthcare-1  |   at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542) ~[na:an]

springboot-hospitalmng-sample-healthcare-1  | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
springboot-hospitalmng-sample-healthcare-1  | 2023-01-05T14:20:37.165Z ERROR 1 --- [           main] j.LocalContainerEntityManagerFactoryBean : Failed to initialize JPA EntityManagerFactory: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution

我想这就是问题所在。Mysql运行在端口3306上,您在3308上的主机上公开它。但是从配置中,您使用RDS_URL但端口3308通过docker网络连接,它应该是3306而不是

最新更新