gpt4 book ai didi

mysql - Docker:MySQL准备就绪时, “wait-for-it”脚本无法启动应用程序

转载 作者:行者123 更新时间:2023-12-02 19:18:10 25 4
gpt4 key购买 nike

MySQL服务准备好接收连接后,我将使用Docker-Compose和wait-for-it从Node.js启动后端。我的问题是脚本无法实现连接本身的准备。如果我将超时设置为20s,则可以使它正常工作,因为届时MySQL将在那时运行,但是在不同的环境下可能会发生变化,如果在特定环境下花费更多时间,则该应用程序将崩溃。我希望它定期检查它,因为我认为这是正确的事情。

这是我的docker-compose.yml:

version: "3.8"

services:

app:
image: pfmc
ports:
- 4005:4005
working_dir: /usr/src/app
command: sh -c './wait-for-it.sh -t 0 db:3306 -- npm start'
environment:
MYSQL_ROOT_PASSWORD: pfmc123
MYSQL_HOST: mysql
MYSQL_USER: root
MYSQL_PASSWORD: pfmc123
MYSQL_DATABASE: pfmc

mysql:
image: mysql:5
command: --default-authentication-plugin=mysql_native_password
restart: always
volumes:
- ./db-startup:/docker-entrypoint-initdb.d
environment:
MYSQL_ROOT_PASSWORD: pfmc123
MYSQL_DATABASE: pfmc

日志:
app_1    | wait-for-it.sh: waiting for db:3306 without a timeout
mysql_1 | 2020-05-25 19:59:58+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.30-1debian10 started.
mysql_1 | 2020-05-25 19:59:58+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
mysql_1 | 2020-05-25 19:59:58+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.30-1debian10 started.
mysql_1 | 2020-05-25 19:59:58+00:00 [Note] [Entrypoint]: Initializing database files

它停留在这一点上:
mysql_1  | 2020-05-25T20:00:08.243448Z 0 [Note] mysqld: ready for connections.
mysql_1 | Version: '5.7.30' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL)


非常感谢您的帮助。

最佳答案

您这里确实有一个问题:您的MySQL容器是一个名为mysql而不是db的服务,您应该将该服务改名为dbwait-for-it调用以轮询与mysql:3306的连接

总之,您的docker-compose.yml应该看起来像这样–我添加了带有说明@fixme的注释的修复程序:

version: "3.8"

services:

app:
image: pfmc
ports:
- 4005:4005
working_dir: /usr/src/app
command: sh -c './wait-for-it.sh -t 0 db:3306 -- npm start'
environment:
MYSQL_ROOT_PASSWORD: pfmc123
# A first @fixme is here
MYSQL_HOST: db
MYSQL_USER: root
MYSQL_PASSWORD: pfmc123
MYSQL_DATABASE: pfmc

# And a second @fixme is here
db:
image: mysql:5
command: --default-authentication-plugin=mysql_native_password
restart: always
volumes:
- ./db-startup:/docker-entrypoint-initdb.d
environment:
MYSQL_ROOT_PASSWORD: pfmc123
MYSQL_DATABASE: pfmc

关于mysql - Docker:MySQL准备就绪时, “wait-for-it”脚本无法启动应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62009969/

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