我的Docker的mysql容器什么时候开始,mysql是否准备好进行查询? [英] How do I know when my docker mysql container is up and mysql is ready for taking queries?
问题描述
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屋!