13.7.8.8 RESTART Statement语句

RESTART

This statement stops and restarts the MySQL server. 此语句停止并重新启动MySQL服务器。It requires the SHUTDOWN privilege.它需要SHUTDOWN权限。

One use for RESTART is when it is not possible or convenient to gain command-line access to the MySQL server on the server host to restart it. RESTART的一个用途是当无法或不方便通过命令行访问服务器主机上的MySQL服务器来重启它时。For example, SET PERSIST_ONLY can be used at runtime to make configuration changes to system variables that can be set only at server startup, but the server must still be restarted for those changes to take effect. 例如,SET PERSIST_ONLY可在运行时用于对系统变量进行配置更改,这些更改只能在服务器启动时设置,但仍必须重新启动服务器才能使这些更改生效。The RESTART statement provides a way to do so from within client sessions, without requiring command-line access on the server host.RESTART语句提供了一种从客户端会话中执行此操作的方法,而不需要在服务器主机上进行命令行访问。

Note注意

After executing a RESTART statement, the client can expect the current connection to be lost. 在执行RESTART语句后,客户机可以预期当前连接将丢失。If auto-reconnect is enabled, the connection is reestablished after the server restarts. 如果启用了自动重新连接,则会在服务器重新启动后重新建立连接。Otherwise, the connection must be reestablished manually.否则,必须手动重新建立连接。

A successful RESTART operation requires mysqld to be running in an environment that has a monitoring process available to detect a server shutdown performed for restart purposes:成功的RESTART操作需要mysqld在一个环境中运行,该环境中有一个可用于检测为重启目的而执行的服务器关闭的监视进程:

These platforms provide the necessary monitoring support for the RESTART statement:这些平台为RESTART语句提供了必要的监控支持:

To configure a monitoring environment such that mysqld enables the RESTART statement:要配置监控环境,以便mysqld启用RESTART语句,请执行以下操作:

  1. Set the MYSQLD_PARENT_PID environment variable to the value of the process ID of the process that starts mysqld, before starting mysqld.在启动mysqld之前,将MYSQLD_PARENT_PID环境变量设置为启动mysqld的进程的进程ID的值。

  2. When mysqld performs a shutdown due to use of the RESTART statement, it returns exit code 16.mysqld由于使用RESTART语句而执行关机时,它返回退出代码16。

  3. When the monitoring process detects an exit code of 16, it starts mysqld again. 当监控进程检测到退出代码为16时,它会再次启动mysqldOtherwise, it exits.否则,它就会退出。

Here is a minimal example as implemented in the bash shell:下面是在bash shell中实现的一个简单示例:

#!/bin/bash

export MYSQLD_PARENT_PID=$$

export MYSQLD_RESTART_EXIT=16

while true ; do
  bin/mysqld mysqld options here
  if [ $? -ne $MYSQLD_RESTART_EXIT ]; then
    break
  fi
done

On Windows, the forking used to implement RESTART makes determining the server process to attach to for debugging more difficult. 在Windows上,用于实现RESTART的分叉使确定要连接以进行调试的服务器进程变得更加困难。To alleviate this, starting the server with --gdb suppresses forking, in addition to its other actions done to set up a debugging environment. 为了缓解这种情况,除了设置调试环境所做的其他操作外,用--gdb启动服务器会抑制分叉。In non-debug settings, --no-monitor may be used for the sole purpose of suppressing forking the monitor process. 在非调试设置中,--no-monitor可能仅用于抑制监视器进程分叉。For a server started with either --gdb or --no-monitor, executing RESTART causes the server to simply exit without restarting.对于以--gdb--no-monitor启动的服务器,执行RESTART会导致服务器直接退出而不重新启动。

The Com_restart status variable tracks the number of RESTART statements. Com_restart状态变量跟踪RESTART语句的数量。Because status variables are initialized for each server startup and do not persist across restarts, Com_restart normally has a value of zero, but can be nonzero if RESTART statements were executed but failed.因为状态变量在每次服务器启动时都会初始化,并且不会在重启期间持续存在,所以Com_restart的值通常为零,但如果RESTART语句被执行但失败,则状态变量可以为非零。