Devansh Sharma
Devansh Sharma

Reputation: 331

MySQL Docker - Check if mysql server is up and running

I am trying to make an image that uses the MySQL image as its base and I want to create a database (and some other stuff) once I know the server is up. I have a bootstrap script as my entrypoint in which the first thing I do is run mysql image's entrypoint script in background as ./entrypoint.sh mysqld &.

Currently, what I do after this is start a loop with some sleep that uses mysqladmin --ping to check if the server is up. Once its up I start my other commands and start creating the database I want.

This works fine when mysql is already initialized (i.e. not the first time the container is started/entrypoint.sh is executed). But on the first run the image seems to create a temporary mysql server (mysqld --daemonize --skip-networking --default-time-zone=SYSTEM --socket="${SOCKET}" - from the entrypoint script), run some commands and then stop that server. Post that it starts the actual server. The problem I am facing is that the mysqladmin ping command actually reacts with a success to the temporary server itself and so my script tries to create the database which fails.

I've read many answers here that say to run the ping command (that I am using) to check if server is up, but none that I saw mention anything about this temporary server or how to detect if it is a temporary one.

Any clues as to how I can detect this ? Or if there is anything better I can do at any part of the flow I have mentioned above ?

I do have one hacky solution in mind right now. One involves updating the entrypoint script to use a different from default socket file for the temporary server, which I can then use to see if the server running is temporary or not, but I'd rather not resort to this if a standard solution exists.

Thanks!

Upvotes: 2

Views: 2905

Answers (2)

MobCode100
MobCode100

Reputation: 405

Just adding to danblack's answer for running health checks in docker-compose.yml:

healthcheck:
    test: ["CMD", "mysqladmin", "ping", "-p$$MYSQL_ROOT_PASSWORD","--protocol","tcp"]

This will prevent healthy status when running temporary server.

Upvotes: 2

danblack
danblack

Reputation: 14736

Because the initialization uses the --skip-networking, use --protocol tcp as an argument to mysqladmin to perform the ping on tcp, which is when the final instance is running.

Upvotes: 4

Related Questions