文档首页
MySQL 8.4 参考手册
8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

7.1.19 服务器关闭过程7.1.19 The Server Shutdown Process

服务器关闭过程如下进行:The server shutdown process takes place as follows:

  1. 关闭过程被启动。The shutdown process is initiated.

    这可以通过多种方式启动。例如,具有SHUTDOWN 权限的用户可以执行 mysqladmin shutdown 命令。mysqladmin 可以在 MySQL 支持的任何平台上使用。也可以使用其他特定操作系统的关闭启动方法:在 Unix 上,当服务器接收到 SIGTERM 信号时会关闭。作为服务运行在 Windows 上的服务器,在服务管理器指示它关闭时也会关闭。This can occur initiated several ways. For example, a user with the SHUTDOWN privilege can execute a mysqladmin shutdown command. mysqladmin can be used on any platform supported by MySQL. Other operating system-specific shutdown initiation methods are possible as well: The server shuts down on Unix when it receives a SIGTERM signal. A server running as a service on Windows shuts down when the services manager tells it to.

  2. 如果有必要,服务器会创建一个关闭线程。The server creates a shutdown thread if necessary.

    根据关闭是如何启动的,服务器可能会创建一个线程来处理关闭过程。如果关闭是由客户端请求的,则会创建一个关闭线程。如果关闭是由于收到SIGTERM信号引起的,信号线程可能会自己处理关闭,也可能会创建一个单独的线程来处理。如果服务器尝试创建关闭线程但无法创建(例如,如果内存耗尽),它会发出诊断消息,该消息会显示在错误日志中:Depending on how shutdown was initiated, the server might create a thread to handle the shutdown process. If shutdown was requested by a client, a shutdown thread is created. If shutdown is the result of receiving a SIGTERM signal, the signal thread might handle shutdown itself, or it might create a separate thread to do so. If the server tries to create a shutdown thread and cannot (for example, if memory is exhausted), it issues a diagnostic message that appears in the error log:

    Error: Can't create thread to kill server
    
  3. 服务器停止接受新连接。The server stops accepting new connections.

    为了防止在关机过程中启动新的活动,服务器通过关闭其通常用于监听连接的网络接口的处理程序来停止接受新的客户端连接:包括 TCP/IP 端口、Unix 套接字文件、Windows 命名管道以及 Windows 上的共享内存。To prevent new activity from being initiated during shutdown, the server stops accepting new client connections by closing the handlers for the network interfaces to which it normally listens for connections: the TCP/IP port, the Unix socket file, the Windows named pipe, and shared memory on Windows.

  4. 服务器终止当前活动。The server terminates current activity.

    对于每个与客户端连接关联的线程,服务器会断开与客户端的连接并将线程标记为已终止。线程在注意到自己被标记后便会终止。空闲连接的线程会很快终止。当前正在处理语句的线程会定期检查自己的状态,因此终止得较慢。有关线程终止的更多信息,请参见第 15.7.8.4 节,“KILL 语句”,特别是有关在MyISAM表上执行已终止的REPAIR TABLE或OPTIMIZE TABLE操作的说明。For each thread associated with a client connection, the server breaks the connection to the client and marks the thread as killed. Threads die when they notice that they are so marked. Threads for idle connections die quickly. Threads that currently are processing statements check their state periodically and take longer to die. For additional information about thread termination, see Section 15.7.8.4, “KILL Statement”, in particular for the instructions about killed REPAIR TABLE or OPTIMIZE TABLE operations on MyISAM tables.

    对于有未完成事务的线程,该事务会被回滚。如果线程正在更新非事务性表,则像多行 UPDATE 或 INSERT 操作可能会导致表部分更新,因为该操作可能在完成之前终止。For threads that have an open transaction, the transaction is rolled back. If a thread is updating a nontransactional table, an operation such as a multiple-row UPDATE or INSERT may leave the table partially updated because the operation can terminate before completion.

    如果服务器是复制源服务器,它会像对待其他客户端线程一样对待与当前连接的副本相关的线程。也就是说,每个线程都会被标记为已终止,并在下次检查其状态时退出。If the server is a replication source server, it treats threads associated with currently connected replicas like other client threads. That is, each one is marked as killed and exits when it next checks its state.

    如果服务器是一个副本服务器,在将客户端线程标记为已终止之前,它会停止复制 I/O 线程和 SQL 线程(如果它们处于活动状态)。允许 SQL 线程完成其当前语句(以避免导致复制问题),然后停止。如果此时 SQL 线程正处于事务中,服务器会等待直到当前复制事件组(如果有)执行完成,或者直到用户发出 KILL QUERY 或 KILL CONNECTION 语句。另请参见 第 15.4.2.5 节,“STOP REPLICA 语句”。由于非事务性语句无法回滚,为了保证崩溃安全的复制,应仅使用事务性表。If the server is a replica server, it stops the replication I/O and SQL threads, if they are active, before marking client threads as killed. The SQL thread is permitted to finish its current statement (to avoid causing replication problems), and then stops. If the SQL thread is in the middle of a transaction at this point, the server waits until the current replication event group (if any) has finished executing, or until the user issues a KILL QUERY or KILL CONNECTION statement. See also Section 15.4.2.5, “STOP REPLICA Statement”. Since nontransactional statements cannot be rolled back, in order to guarantee crash-safe replication, only transactional tables should be used.

    Note

    为了保证副本的崩溃安全,必须在启用 --relay-log-recovery 的情况下运行副本。To guarantee crash safety on the replica, you must run the replica with --relay-log-recovery enabled.

    另请参见 第 19.2.4 节,“中继日志和复制元数据存储”)。See also Section 19.2.4, “Relay Log and Replication Metadata Repositories”).

  5. 服务器关闭或关闭存储引擎。The server shuts down or closes storage engines.

    在此阶段,服务器刷新表缓存并关闭所有打开的表。At this stage, the server flushes the table cache and closes all open tables.

    每个存储引擎都会对其管理的表执行必要的操作。InnoDB 会将其缓冲池刷写到磁盘(除非 innodb_fast_shutdown 为 2)、将当前的 LSN 写入表空间,并终止其自身的内部线程。MyISAM 会刷新表的任何待处理的索引写入。Each storage engine performs any actions necessary for tables that it manages. InnoDB flushes its buffer pool to disk (unless innodb_fast_shutdown is 2), writes the current LSN to the tablespace, and terminates its own internal threads. MyISAM flushes any pending index writes for a table.

  6. 服务器退出。The server exits.

为了向管理流程提供信息,服务器返回以下列表中描述的退出代码之一。括号中的短语表示在使用 systemd 管理服务器的平台上,systemd 对该代码采取的动作。To provide information to management processes, the server returns one of the exit codes described in the following list. The phrase in parentheses indicates the action taken by systemd in response to the code, for platforms on which systemd is used to manage the server.

  • 0 = 成功终止(不进行重启)0 = successful termination (no restart done)

  • 1 = 终止失败(不进行重启)1 = unsuccessful termination (no restart done)

  • 2 = 终止失败(进行重启)2 = unsuccessful termination (restart done)