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

后端 未结 14 910
猫巷女王i
猫巷女王i 2020-11-30 22:33

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 sc

相关标签:
14条回答
  • 2020-11-30 23:30

    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
    
    0 讨论(0)
  • 2020-11-30 23:33

    I've found that using the mysqladmin ping approach isn't always reliable, especially if you're bringing up a new DB. In that case, even if you're able to ping the server, you might be unable to connect if the user/privilege tables are still being initialized. Instead I do something like the following:

    while ! docker exec db-container mysql --user=foo --password=bar -e "SELECT 1" >/dev/null 2>&1; do
        sleep 1
    done
    

    So far I haven't encountered any problems with this method. I see that something similar was suggested by VinGarcia in a comment to one of the mysqladmin ping answers.

    0 讨论(0)
提交回复
热议问题