`
love~ruby+rails
  • 浏览: 849441 次
  • 性别: Icon_minigender_1
  • 来自: lanzhou
社区版块
存档分类
最新评论

How NOT to test that mysqld is alive

阅读更多

I had a call from a new customer last week. They had issues with their MySQL server. Apparently, it was repeatingly crashing, every few hours. To have their production system kept alive, they used a script to periodically see if MySQL was still alive, and if not – start it.

I was first thinking in directions of old installations; wrong memory allocations (too little memory for large content; to large memory allocation for weak machine). When I reached the customer’s premises, I quickly reviewed general settings, while explaining some basic configuration guidelines. There were peculiarities (e.g. query_cache_limit being larger than query_cache_size), but nothing to obviously suggest a reason for crash.

I then observed the error log. Took some time to find it, since the log_error parameter appeared twice in the my.cnf file; first one appearing at the very beginning, the second (overwriting the first) was far into the file.

Sure enough, there were a lot of startup messages. And… shutdown messages. In fact, the log looked something like:

090923 17:38:15  mysqld started
090923 17:38:16  InnoDB: Started; log sequence number 0 3707779031
090923 17:38:16 [Note] /usr/local/mysql/bin/mysqld: ready for connections.
Version: '5.0.77-log'  socket: '/tmp/mysql50.sock'  port: 3306  MySQL Community Server (GPL)
090923 19:53:41 [Note] /usr/local/mysql/bin/mysqld: Normal shutdown
090923 19:53:56  mysqld started
090923 19:53:56  InnoDB: Started; log sequence number 0 5288400927
090923 19:53:56 [Note] /usr/local/mysql/bin/mysqld: ready for connections.
Version: '5.0.77-log'  socket: '/tmp/mysql50.sock'  port: 3306  MySQL Community Server (GPL)
090929 22:38:48 [Note] /usr/local/mysql/bin/mysqld: Normal shutdown
090923 22:38:58  mysqld started
090923 22:38:58  InnoDB: Started; log sequence number 0 7102832776
090923 22:38:58 [Note] /usr/local/mysql/bin/mysqld: ready for connections.
Version: '5.0.77-log'  socket: '/tmp/mysql50.sock'  port: 3306  MySQL Community Server (GPL)
...

(The above is just a sample, not the original file).

Well, the log says there’s a lot of normal shutdowns.

Looking at the script

Next it was time to look at the script which was supposed to verify MySQL was up and running – else wise start it. And it went something like this:

while [ 1 ];
  if [`ps aux | grep mysqld | wc -l` -lt 2]
    then /etc/init.d/mysql restart
  ...
  sleep 60

The script was looking for all processes, then grepping for mysqld, counting number of lines in output. It expected 2 lines: one for the mysqld process, one for the grep mysqld process itself.

If you don’t know what’s wrong with this, a very brief explanation about how pipelines work in unix work it at place:

Pipelined processes do not execute one after another, or one before another. They are all executed at once. So, “ps aux | grep mysqld | wc -l” immediately spawns ps, grep, wc, then sets the standard output of one to the standard input of the other (most simplified description I could think of).

It is likely that grep will outrun ps in the race for starting up. So grep is started, already waiting for input from ps, which then notices both mysqld is alive, but also grep mysqld, lists them both along with other processes, to be filtered by grep, to be counted by wc (returning two lines count).

But this is just because ps is heavier than grep. It doesn’t have to be like that.

The less common scenario

Every once and again, ps outruns grep in the race for starting up. It would list the active processes, and no “grep” would appear in the listing. grep would then run, filtering the result of ps, then to be counted by wc — oh, there is only one line now! The script assumes this means mysqld is down (since it assumed grep is always there), and restarts mysqld.

So, the script which was supposed to take care of MySQL crashes, was actually causing them (though no crash occurred).

Better ways to test that MySQL is alive

Look for the pid file. This is the standard (that’s how the mysql service works). Look for the unix socket file. Both require that you parse the my.cnf file to learn where these files are.

If you’re reluctant to read the configuration files, other options are at hand. OK, look for the process; but use pgrep mysqld. No need to list all processes, then grep them.

And best way, that will set your mind at ease even if you’re worried that “mysql is running but not responding; it is stuck”: connect to MySQL and issue SELECT 1, SELECT NOW(), SELECT something. This would be the ultimate test that MySQL is up, listening, responding and valid.

分享到:
评论

相关推荐

    mysqld_exporter安装包mysqld_exporter-0.10.0.linux-amd64.tar.gz

    MySQLd_Exporter是一款用于监控MySQL服务状态的工具,它能够暴露MySQL服务器的指标,以便于集成到各种监控系统中,如Prometheus。本安装包“mysqld_exporter-0.10.0.linux-amd64.tar.gz”是针对Linux AMD64架构的...

    mysqld_exporter-0.13.0.windows-amd64.zip

    mysqld_exporter是Prometheus用于监控MySQL指标的一个导出器,支持对MySQL 5.5以上进行监控。下面是mysqld_exporter的安装与配置: 1、登录MySQL,创建一个账号用于Prometheus监控获取数据 CREATE USER 'exporter'...

    mysqld_exporter安装包mysqld_exporter-0.12.0.linux-amd64.tar.gz

    mysqld_exporter安装包mysqld_exporter-0.12.0.linux-amd64.tar.gz

    mysqld_exporter-0.12.1.linux-amd64.tar.gz

    例如,`./mysqld_exporter --config.file=<path_to_config_file>`。 4. **配置Prometheus**:在Prometheus的配置文件中,添加一个job来指向MySQL Exporter的服务端点,如`target: 'localhost:9104'`(默认端口),...

    mysqld_exporter-0.10.0.linux-amd64.tar.gz

    - 运行:执行解压后的可执行文件`./mysqld_exporter --config.my-cnf=/path/to/config.cnf`,其中`/path/to/config.cnf`是包含MySQL连接信息的配置文件。 2. 配置: - 配置文件:创建一个包含MySQL连接信息的配置...

    prometheus组件-mysqld-exporter-0.15.0.linux-amd64.tar

    在给定的压缩包文件“prometheus组件-mysqld-exporter-0.15.0.linux-amd64.tar”中,我们关注的是mysqld-exporter,这是Prometheus的一个组件,专门用于监控MySQL数据库的性能和状态。 mysqld-exporter是Prometheus...

    mysqld.exe报错

    标题中的“mysqld.exe报错”提示我们讨论的核心是MySQL服务器进程运行时遇到的问题。mysqld.exe是MySQL数据库服务的主进程,负责处理所有客户端的连接请求和执行SQL语句。当mysqld.exe出现错误时,可能是由于多种...

    mysqld数据库的编码设置

    mysqld数据库的编码设置,亲测,可用,设置成utf-8编码。

    mysqld_exporter-0.14.0.windows-amd64.zip

    《MySQLd_Exporter 0.14.0 for Windows on AMD64平台详解》 MySQLd_Exporter是一款专为MySQL数据库监控设计的工具,它基于Prometheus Exporter框架,能够将MySQL服务器的状态和性能指标暴露出来,以便于系统管理员...

    mysqld_exporter-0.14.0.linux-amd64.tar.gz

    《MySQL监控利器:mysqld_exporter详解》 在IT运维领域,监控系统的重要性不言而喻,尤其对于数据库管理而言,实时、准确地掌握数据库的运行状态是保障业务正常运行的关键。mysqld_exporter作为一款专为MySQL设计的...

    zabbix-server is not running解决方法.docx

    ### zabbix-server is not running 解决方法 #### 一、问题背景 在部署Zabbix监控系统的过程中,可能会遇到“zabbix-server is not running”的错误提示。这种情况通常发生在虚拟机重启之后,原本正常运行的Zabbix...

    mysql 5.7.x 所需mysqld.service文件

    mysql 5.7.x 所需mysqld.service文件

    Redhat7 DB2报The 32 bit library file libstdc++.so.5 is not found on the system解决

    ### Redhat7 DB2安装时遇到“The 32 bit library file libstdc++.so.5 is not found on the system”问题的解决方案 在部署数据库管理系统(DBMS)时,经常会出现各种兼容性或缺失库的问题。例如,在Redhat 7上安装...

    Mysqld配置选项详细介绍

    Mysqld 配置选项详细介绍 Mysqld 是 MySQL 数据库管理系统的核心组件之一,负责处理数据库查询、管理数据库文件、维护数据库安全等任务。为了确保 MySQL 数据库的稳定运行和高效性能,需要对 mysqld 程序的配置选项...

    MySQL不停地自动重启的解决方法

    例如,`mysqld(my_print_stacktrace+0x2c)[0xed481c]`这类信息表示在处理特定函数时发生了错误。通过这些信息,可以尝试追踪到导致崩溃的具体操作,进一步分析是由于代码逻辑还是库文件的问题。 3. **资源监控**: ...

    mysql error 1130 hy000:Host’localhost’解决方案

    ERROR 1130 (HY000): Host ‘localhost’ is not allowed to connect to this MySQL server www.jb51.net 出现原因: mysql只有一个root用户,修改root密码后选了MD5,提交后,重新 登陆出现“Host ‘localhost’ is...

    MySQL启动报错问题InnoDB:Unable to lock/ibdata1 error

    【MySQL启动报错问题InnoDB:Unable to lock/ibdata1 error】是一个常见的MySQL服务器启动时遇到的问题。这个问题通常表明MySQL的InnoDB存储引擎无法获取对`ibdata1`文件的锁,`ibdata1`是InnoDB用来存储数据和系统表...

    mysqld_exporter-master.zip

    "mysqld_exporter-master.zip"是这个工具的源代码压缩包,包含了最新稳定版的mysqld_exporter项目。 在MySQL数据库管理中,监控是至关重要的,因为它可以帮助管理员识别潜在的性能问题、资源瓶颈和异常行为。mysqld...

    mysqld -nt.exe

    解决没有mysqld.exe -install的问题,下载直接打开可以开启mysql服务,如果不小心删除mysql服务用这个也可以。

Global site tag (gtag.js) - Google Analytics