mysqladminコマンドでMySQLデーモンの起動状態を確認

概要

Amazon RDSやGoogle CloudSQLでMySQLインスタンスの起動や停止の検証を行っている際に、
デーモンが稼働状態に遷移したか確認したいときになにかよい方法はないかなーと思っていました。
そこで調べていたところmysqladminコマンドのpingオプションというものがあり、
それを使うことで簡単に確認出来たのでそのメモです。

mysqladmin pingコマンド

MySQLデーモンが起動中の状態の場合
$ mysqladmin ping -h 127.0.0.1
mysqld is alive
$

mysqld is aliveというメッセージが返ってきます。

終了ステータスは0です。

MySQLデーモンが停止中の状態の場合
$ mysqladmin ping -h 127.0.0.1
mysqladmin: connect to server at '127.0.0.1' failed
error: 'Can't connect to MySQL server on '127.0.0.1' (61)'
Check that mysqld is running on 127.0.0.1 and that the port is 3306.
You can check this by doing 'telnet 127.0.0.1 3306'
$

デーモンが起動していない場合上記のようなメッセージになります。

終了ステータスは1です。

MySQLデーモンは起動しているがパスワード認証がかかっている場合
$ mysqladmin ping -h 127.0.0.1; echo $?
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
mysqladmin: connect to server at '127.0.0.1' failed
error: 'Access denied for user 'root'@'172.17.0.1' (using password: YES)'
0
$

上記のように認証エラーになりますがデーモンは起動しているので終了ステータスは成功の0となるので注意が必要です。