我的Docker的mysql容器什么时候开始,mysql是否准备好进行查询? [英] How do I know when my docker mysql container is up and mysql is ready for taking queries?

查看:175
本文介绍了我的Docker的mysql容器什么时候开始,mysql是否准备好进行查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在部署几个不同的docker容器,mysql是第一个。一旦数据库启动并且继续构建其他容器,我想运行脚本。脚本已经失败了,因为它正在尝试运行entrypoint脚本,它设置了mysql(从这个官方的mysql容器),仍然在运行。




sudo docker run --name mysql -e MYSQL_ROOT_PASSWORD = MY_ROOT_PASS -p 3306: 3306 -d mysql
[..]等待mysql准备好[...]
mysql -h 127.0.0.1 -P 3306 -u root --password = MY_ROOT_PASS< MY_SQL_SCRIPT.sql



有没有办法等待docker容器内的entrypoiny mysql安装脚本的信号? Bash睡眠似乎是一个次优解决方案。



编辑:为这样的bash脚本。不是最优雅和有力的暴力,而是像一个魅力。可能有人会发现有用的。




OUTPUT =无法连接
while [[$ OUTPUT = = *无法连接*]]
do
OUTPUT = $(mysql -h $ APP_IP -P:$ APP_PORT -u yyy --password = xxx< ./my_script.sql 2& ;& 1)
done

解决方案

您可以安装mysql -client包,并使用mysqladmin ping目标服务器。使用多个码头箱容器时很有用。结合睡眠并创建一个简单的等待循环:

  while! mysqladmin ping -h$ DB_HOST--silent;做
睡眠1
完成


I am deploying a few different docker containers, mysql being the first one. I want to run scripts as soon as database is up and proceed to building other containers. The script has been failing because it was trying to run when the entrypoint script, which sets up mysql (from this official mysql container), was still running.

sudo docker run --name mysql -e MYSQL_ROOT_PASSWORD=MY_ROOT_PASS -p 3306:3306 -d mysql [..] wait for mysql to be ready [..] mysql -h 127.0.0.1 -P 3306 -u root --password=MY_ROOT_PASS < MY_SQL_SCRIPT.sql

Is there a way to wait for a signal of an entrypoiny mysql setup script finishing inside the docker container? Bash sleep seems like a suboptimal solution.

EDIT: Went for a bash script like this. Not the most elegant and kinda brute force but works like a charm. Maybe someone will find that useful.

OUTPUT="Can't connect" while [[ $OUTPUT == *"Can't connect"* ]] do OUTPUT=$(mysql -h $APP_IP -P :$APP_PORT -u yyy --password=xxx < ./my_script.sql 2>&1) done

解决方案

You can install mysql-client package and use mysqladmin to ping target server. Useful when working with multiple docker container. Combine with sleep and create a simple wait-loop:

while ! mysqladmin ping -h"$DB_HOST" --silent; do
    sleep 1
done

这篇关于我的Docker的mysql容器什么时候开始,mysql是否准备好进行查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆