MySQL Docker - 检查 mysql 服务器是否已启动并正在运行

2022-01-15 00:00:00 shell docker docker-compose mariadb mysql

我正在尝试制作一个使用 MySQL 映像作为其基础的映像,并且一旦我知道服务器已启动,我想创建一个数据库(和其他一些东西).我有一个引导脚本作为我的入口点,我要做的第一件事是在后台运行 mysql 映像的入口点脚本作为 ./entrypoint.sh mysqld &.

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 &.

目前,我在此之后要做的就是启动一个循环并使用 mysqladmin --ping 来检查服务器是否启动.一旦启动,我将启动其他命令并开始创建我想要的数据库.

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.

当 mysql 已经初始化(即不是第一次启动容器/entrypoint.sh 执行)时,这可以正常工作.但在第一次运行时,图像似乎创建了一个临时 mysql 服务器(mysqld --daemonize --skip-networking --default-time-zone=SYSTEM --socket="${SOCKET}" - 从入口点脚本),运行一些命令,然后停止该服务器.发布它启动实际的服务器.我面临的问题是 mysqladmin ping 命令实际上对临时服务器本身做出了成功的反应,因此我的脚本尝试创建失败的数据库.

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.

我在这里阅读了很多答案,说运行 ping 命令(我正在使用)来检查服务器是否已启动,但我没有看到任何关于此临时服务器或如何检测它是否是临时服务器的内容一个.

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 ?

我现在确实有一个 hacky 解决方案.一个涉及更新入口点脚本以使用与临时服务器的默认套接字文件不同的文件,然后我可以使用它来查看正在运行的服务器是否是临时的,但如果存在标准解决方案,我宁愿不诉诸于此.

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.

谢谢!

推荐答案

由于初始化使用--skip-networking,所以使用--protocol tcp作为参数到 mysqladmin 以在 tcp 上执行 ping,也就是最终实例运行时.

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.

相关文章