Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know when my docker mysql container is up and mysql is ready for taking queries?

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 
like image 228
haren Avatar asked Aug 26 '14 10:08

haren


People also ask

How do I know if MySQL is running?

We check the status with the systemctl status mysql command. We use the mysqladmin tool to check if MySQL server is running. The -u option specifies the user which pings the server. The -p option is a password for the user.

How do I run a MySQL Docker container?

Here are the steps you can follow to install the Dockerhub MySQL Container: Step 1: Pull the Docker Image for MySQL. Step 2: Deploy and Start the MySQL Container. Step 3: Connect with the Docker MySQL Container.

How do I log into MySQL on a docker container?

Run docker container with volume mount option docker run -it -v /host:/shared <mysql image> . Then change mysql configuration file /etc/my. cnf and change socket entry in the file to socket=/shared/mysql. sock.


1 Answers

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 
like image 85
flamemyst Avatar answered Sep 24 '22 21:09

flamemyst