大网数据广告活动
服务器运维问答

Check if MySQL server is alive

· 52CE 我爱测

How can I check the readiness of a MySQL database that has a user and a password already set? Without password the command should be: mysqladmin ping -h localhost Bin in my test environment the MySQL database already has a passwo…

问题原文

原帖链接 · Socrates · CC BY-SA 4.0

How can I check the readiness of a MySQL database that has a user and a password already set?

Without password the command should be:

mysqladmin ping -h localhost

Bin in my test environment the MySQL database already has a password set in the environment variable MYSQL_ROOT_PASSWORD named abc123. Simply providing the password with -p doesn't seem to do the trick:

mysqladmin ping -h localhost -p abc123

On average the MySQL database takes about 20 seconds to start.

社区高分解答

原帖链接 · Bob · CC BY-SA 4.0

Check the manual please https://dev.mysql.com/doc/refman/8.0/en/mysqladmin.html

You can supply an account/user name and password with the --user and --password command line switches to log on.

Depending on your needs you might simply check the exit code of a mysqladmin ping without supplying any credentials as that already gives you a database server status :

ping

Check whether the server is available. The return status from mysqladmin is 0 if the server is running, 1 if it is not. This is 0 even in case of an error such as Access denied, because this means that the server is running but refused the connection, which is different from the server not running.

See https://stackoverflow.com/q/25503412 for some other considerations regarding the use of mysqladmin ping as a health check

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-01-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。