4.10 Unix Signal Handling in MySQLMySQL中的Unix信号处理

On Unix and Unix-like systems, a process can be the recipient of signals sent to it by the root system account or the system account that owns the process. 在Unix和类Unix系统上,进程可以是root系统帐户或拥有该进程的系统帐户发送给它的信号的接收者。Signals can be sent using the kill command. 可以使用kill命令发送信号。Some command interpreters associate certain key sequences with signals, such as Control+C to send a SIGINT signal. 一些命令解释器将某些键序列与信号相关联,例如Control+C发送SIGINT信号。This section describes how the MySQL server and client programs respond to signals.本节描述MySQL服务器和客户端程序如何响应信号。

Server Response to Signals服务器对信号的响应

mysqld responds to signals as follows:mysqld对信号的响应如下:

  • SIGTERM causes the server to shut down. This is like executing a SHUTDOWN statement without having to connect to the server (which for shutdown requires an account that has the SHUTDOWN privilege).SIGTERM导致服务器关闭。这就像在不连接到服务器的情况下执行SHUTDOWN语句(关闭需要一个具有SHUTDOWN权限的帐户)。

  • SIGHUP causes the server to reload the grant tables and to flush tables, logs, the thread cache, and the host cache. SIGHUP使服务器重新加载授权表,并刷新表、日志、线程缓存和主机缓存。These actions are like various forms of the FLUSH statement. 这些动作就像各种形式的FLUSH声明。Sending the signal enables the flush operations to be performed without having to connect to the server, which requires a MySQL account that has privileges sufficient for those operations. 发送信号可以在不连接到服务器的情况下执行刷新操作,这需要一个具有足够权限的MySQL帐户来执行这些操作。Prior to MySQL 8.0.20, the server also writes a status report to the error log that has this format:在MySQL 8.0.20之前,服务器还会将状态报告写入错误日志,格式如下:

    Status information:
    
    Current dir: /var/mysql/data/
    Running threads: 4  Stack size: 262144
    Current locks:
    lock: 0x7f742c02c0e0:
    
    lock: 0x2cee2a20:
    :
    lock: 0x207a080:
    
    Key caches:
    default
    Buffer_size:       8388608
    Block_size:           1024
    Division_limit:        100
    Age_limit:             300
    blocks used:             4
    not flushed:             0
    w_requests:              0
    writes:                  0
    r_requests:              8
    reads:                   4
    
    handler status:
    read_key:           13
    read_next:           4
    read_rnd             0
    read_first:         13
    write:               1
    delete               0
    update:              0
    
    Table status:
    Opened tables:        121
    Open tables:          114
    Open files:            18
    Open streams:           0
    
    Memory status:
    <malloc version="1">
    <heap nr="0">
    <sizes>
      <size from="17" to="32" total="32" count="1"/>
      <size from="33" to="48" total="96" count="2"/>
      <size from="33" to="33" total="33" count="1"/>
      <size from="97" to="97" total="6014" count="62"/>
      <size from="113" to="113" total="904" count="8"/>
      <size from="193" to="193" total="193" count="1"/>
      <size from="241" to="241" total="241" count="1"/>
      <size from="609" to="609" total="609" count="1"/>
      <size from="16369" to="16369" total="49107" count="3"/>
      <size from="24529" to="24529" total="98116" count="4"/>
      <size from="32689" to="32689" total="32689" count="1"/>
      <unsorted from="241" to="7505" total="7746" count="2"/>
    </sizes>
    <total type="fast" count="3" size="128"/>
    <total type="rest" count="84" size="195652"/>
    <system type="current" size="690774016"/>
    <system type="max" size="690774016"/>
    <aspace type="total" size="690774016"/>
    <aspace type="mprotect" size="690774016"/>
    </heap>
    :
    <total type="fast" count="85" size="5520"/>
    <total type="rest" count="116" size="316820"/>
    <total type="mmap" count="82" size="939954176"/>
    <system type="current" size="695717888"/>
    <system type="max" size="695717888"/>
    <aspace type="total" size="695717888"/>
    <aspace type="mprotect" size="695717888"/>
    </malloc>
    
    Events status:
    LLA = Last Locked At  LUA = Last Unlocked At
    WOC = Waiting On Condition  DL = Data Locked
    
    Event scheduler status:
    State      : INITIALIZED
    Thread id  : 0
    LLA        : n/a:0
    LUA        : n/a:0
    WOC        : NO
    Workers    : 0
    Executed   : 0
    Data locked: NO
    
    Event queue status:
    Element count   : 0
    Data locked     : NO
    Attempting lock : NO
    LLA             : init_queue:95
    LUA             : init_queue:103
    WOC             : NO
    Next activation : never
  • As of MySQL 8.0.19, SIGUSR1 causes the server to flush the error log, general query log, and slow query log. 从MySQL 8.0.19开始,SIGUSR1会导致服务器刷新错误日志、常规查询日志和慢速查询日志。One use for SIGUSR1 is to implement log rotation without having to connect to the server, which requires a MySQL account that has privileges sufficient for those operations. SIGUSR1的一个用途是在不连接到服务器的情况下实现日志轮换,这需要一个具有足够权限进行这些操作的MySQL帐户。For information about log rotation, see Section 5.4.6, “Server Log Maintenance”.有关日志轮换的信息,请参阅第5.4.6节,“服务器日志维护”

    The server response to SIGUSR1 is a subset of the response to SIGHUP, enabling SIGUSR1 to be used as a more lightweight signal that flushes certain logs without the other SIGHUP effects such as flushing the thread and host caches and writing a status report to the error log.服务器对SIGUSR1的响应是对SIGHUP的响应的一个子集,使SIGUSR1能够被用作一个更“轻量级”的信号,在没有其他SIGHUP影响的情况下刷新某些日志,例如刷新线程和主机缓存以及向错误日志写入状态报告。

  • SIGINT normally is ignored by the server. Starting the server with the --gdb option installs an interrupt handler for SIGINT for debugging purposes. SIGINT通常被服务器忽略。使用--gdb选项启动服务器会为SIGINT安装一个中断处理程序,用于调试目的。See Section 5.9.1.4, “Debugging mysqld under gdb”.请参阅第5.9.1.4节,“在gdb下调试mysqld”

Client Response to Signals客户端对信号的响应

MySQL client programs respond to signals as follows:MySQL客户端程序对信号的响应如下:

  • The mysql client interprets SIGINT (typically the result of typing Control+C) as instruction to interrupt the current statement if there is one, or to cancel any partial input line otherwise. mysql客户端将SIGINT(通常是键入Control+C的结果)解释为指令,如果有当前语句,则中断当前语句,否则取消任何部分输入行。This behavior can be disabled using the --sigint-ignore option to ignore SIGINT signals.可以使用--sigint-ignore选项忽略SIGINT信号来禁用此行为。

  • Client programs that use the MySQL client library block SIGPIPE signals by default. These variations are possible:默认情况下,使用MySQL客户端库的客户端程序会阻止SIGPIPE信号。这些变化是可能的: