我已经创建了一个从一个Spring的应用程序,但有一个错误,它不能访问mysql数据库,我有问题解决这个Docker图像。
我使用Dockerfile创建了一个映像
FROM openjdk:17-jdk-alpine
EXPOSE 8080
ARG JAR_FILE=./sample-service.jar
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
创建图像后,我创建了docker-compose.yml,这是我的docker-compose.yml:
version: "3"
services:
sample-service:
image: v2stechit/sample-service
ports:
- "8080:8080"
restart: always
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://localhost:3306/buddyto_mstr_local?useSSL=false
SPRING_DATASOURCE_USERNAME: root
SPRING_DATASOURCE_PASSWORD: root
networks:
- spring-mysql
depends_on:
- mysqldb
mysqldb:
image: mysql:8.0.29
networks:
- spring-mysql
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=buddyto_mstr_local
- MYSQL_USERNAME=root
- MYSQL_PASSWORD=root
ports:
- 3306:3306
networks:
spring-mysql:
但它没有在浏览器中打开,所以我检查了日志,得到了这个:
以下是Spring Docker容器日志:https://pastebin.com/raw/pjiscq4T
下面是mysql日志:
2022-11-30 05:18:01+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.29-1.el8 started.
2022-11-30 05:18:01+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2022-11-30 05:18:01+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.29-1.el8 started.
'/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
2022-11-30T05:18:01.727948Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.29) starting as process 1
2022-11-30T05:18:01.744335Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-11-30T05:18:01.892603Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-11-30T05:18:02.206174Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2022-11-30T05:18:02.206242Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2022-11-30T05:18:02.209090Z 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.
2022-11-30T05:18:02.251051Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2022-11-30T05:18:02.251175Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.29' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
我该如何解决这个问题?
我试着为mysql使用空的password环境变量。
尝试更改数据库名称,将mysql版本更改为5.6、8和8.0.29
我不知道下一步该做什么。
我期待着Spring的应用程序与mysql链接
2条答案
按热度按时间v1l68za41#
docker中的
localhost
将不会解析为服务器的localhost。相反,您需要指定mysql服务的名称mysqldb
。于是这就变成:
SPRING_DATASOURCE_URL: jdbc:mysql://msqldb:3306/buddyto_mstr_local?useSSL=false
您也可以尝试给出运行Docker的服务器的IP,而不是
mysqldb
watbbzwu2#
我终于修复了它。我做了一些清理配置文件,它是自动修复。非常感谢。