Is there a good way to find that MySQL is "ready?"

I am not a MySQL expert.

I have a script that installs MySQL, starts mysqld , and then uses mysql to initialize.

Currently, to do this work, I enter a loop that (sorry for the pseudo code mixing multiple languages):

 mysqld_safe /* ... */ & /* ampersand to start in background so we can continue */ while(fileDoesNotExist("/tmp/mysql.sock")) { sleepFor100ms(); } mysql -u root /* and so forth */ initialize.sql 

This works (!), But has several problems:

  • the poll is funny funny
  • I'm not smart enough about MySQL to find out if looking at this hard-coded path /tmp/mysql.sock smart at all.

And yet this is much simpler than trying (for example) to consume and analyze stdout (or is it stderr?) mysqld_safe to find out if the server has started.

My narrow question is if there is a way to start mysqld lock: can I issue any command that locks until the database starts and then exits (and the PID file remains, maybe there remains) and has a companion command stops? (Or maybe it allows me to read the PID file and issue my own SIGTERM?)

My broader question is: am I on the right track, or are there any completely different and simpler ones (it would be β€œeasier” for me, it’s easy, I’m not interested in installing a tool kit like Puppet or DbMaintain / Liquibase or something else ) approach to solving the problem that I have formulated? That is, starting with a .gz file containing MySQL, install custom MySQL and initialize the database?

+7
mysql
source share
1 answer

Check out the init script shell for mysqld. They do the polling in the wait_for_pid () function.

This function checks for the presence of the pid file, and if it does not already exist, it sleeps for one whole second, and then retries. There, the default timeout is 900 seconds, after which he refuses to wait and concludes that he is not going to start (and displays the completely useless message "Server is shutting down without updating the PID file").

You do not need to guess where the pid file is. If you run mysqld_safe, you must specify where it should create the pid file using the --pid-file option.

One tricky part is that the pid file is not created until mysqld initializes. This may take some time if it should perform a disaster recovery using InnoDB log files and the log files are large. Thus, it may happen that 900 seconds of the timeout are not long enough and you get a false error, although mysqld successfully starts one minute after the timeout.

You can also read the error log or the console output of mysqld. In the end, he should output a line that says "ready for connections."

To read until you get this line, and then stop reading, you can use:

 tail -f | sed -e '/ready for connections/q' 
+5
source share

All Articles