[英]com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure in docker
Im trying to dockerize by project, but when i try to run docker-compose up, my database gets failure with errror: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure. Im trying to dockerize by project, but when i try to run docker-compose up, my database gets failure with errror: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure. I've tried many sollutions, and none was working.我尝试了许多解决方案,但没有一个有效。
My docker-compose.yml file:我的 docker-compose.yml 文件:
version: "3"
services:
database:
platform: linux/x86_64
container_name: 'mysql'
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_PASSWORD: p@ssword
MYSQL_USER: testUser
MYSQL_DATABASE: testDatabase
ports:
- "3306:3306"
networks:
- mysql-db
maildev:
image: maildev/maildev
ports:
- "1080:80"
web:
build: .
ports:
- "8080:8080"
networks:
- mysql-db
depends_on:
- database
environment:
SPRING_DATASOURCE_PASSWORD: p@ssword
SPRING_DATASOURCE_USER: testUser
SPRING_DATASOURCE_NAME: testDatabase
SPRING_DATASOURCE_URL: jdbc:mysql://database:3306/testDatabase
links:
- database
networks:
mysql-db:
driver: bridge
And my application.properties file:还有我的 application.properties 文件:
spring.datasource.password=p@ssword
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://database:3306/testDatabase
spring.datasource.username=testUser
spring.mail.host=localhost
spring.mail.port=1025
spring.mail.username=hello
spring.mail.password=hello
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.show_sql=true
spring.jpa.properties.format_sql=true
spring.jpa.logging.level.org.hibernate.type=trace
spring.jpa.logging.level.org.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.logging.mail.smtp.ssl.trust=*
spring.jpa.logging.mail.smtp.auth=true
spring.jpa.logging.mail.smtp.starttls.enable=true
spring.jpa.logging.mail.smtp.connectiontimeout=5000
spring.jpa.logging.mail.smtp.timeout=3000
spring.jpa.logging.mail.smtp.writetimeout=5000
logging.level.org.hibernate.SQL=TRACE
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
You have way more configuration than you need.你有比你需要的更多的配置。 I guess you are trying to solve the problem and trying any possible solution:)我猜您正在尝试解决问题并尝试任何可能的解决方案:)
So bellow is "cleanup up" version of your compose file.所以下面是您的撰写文件的“清理”版本。 I removed everything that is not essential (put anything back that you know you need).我删除了所有不必要的东西(把你知道你需要的东西放回去)。
Could you spot already why this config will work?你能发现为什么这个配置会起作用吗? ;) ;)
The problem is that you defined custom container_name mysql
for your database
service but in connection you still use database
name.问题是您为database
服务定义了自定义 container_name mysql
但在连接中您仍然使用database
名称。 So you need to choose one of them and use in connection: either remove container_name declaration from database (as in the code below) or change connection string to jdbc:mysql://mysql:3306/testDatabase
因此,您需要选择其中之一并在连接中使用:从数据库中删除 container_name 声明(如下面的代码所示)或将连接字符串更改为jdbc:mysql://mysql:3306/testDatabase
version: "3"
services:
database:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_PASSWORD: p@ssword
MYSQL_USER: testUser
MYSQL_DATABASE: testDatabase
maildev:
image: maildev/maildev
web:
build: .
environment:
SPRING_DATASOURCE_PASSWORD: p@ssword
SPRING_DATASOURCE_USER: testUser
SPRING_DATASOURCE_NAME: testDatabase
SPRING_DATASOURCE_URL: jdbc:mysql://database:3306/testDatabase
links:
- database
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.