2.5.9 Managing MySQL Server with systemd用systemd管理MySQL服务器

If you install MySQL using an RPM or Debian package on the following Linux platforms, server startup and shutdown is managed by systemd:如果在以下Linux平台上使用RPM或Debian软件包安装MySQL,则服务器启动和关闭由systemd管理:

If you install MySQL from a generic binary distribution on a platform that uses systemd, you can manually configure systemd support for MySQL following the instructions provided in the post-installation setup section of the MySQL 8.0 Secure Deployment Guide.如果在使用systemd的平台上从通用二进制发行版安装MySQL,则可以按照MySQL 8.0安全部署指南的安装后设置部分中提供的说明手动配置systemd对MySQL的支持。

If you install MySQL from a source distribution on a platform that uses systemd, obtain systemd support for MySQL by configuring the distribution using the -DWITH_SYSTEMD=1 CMake option. 如果从使用systemd的平台上的源发行版安装MySQL,请通过使用-DWITH_systemd=1 CMake选项配置发行版来获得对MySQL的systemd支持。See Section 2.9.7, “MySQL Source-Configuration Options”.请参阅第2.9.7节,“MySQL源配置选项”

The following discussion covers these topics:以下讨论涉及这些主题:

Note注意

On platforms for which systemd support for MySQL is installed, scripts such as mysqld_safe and the System V initialization script are unnecessary and are not installed. 在安装了支持MySQL的systemd的平台上,诸如mysqld_safe和System V初始化脚本之类的脚本是不必要的,并且没有安装。For example, mysqld_safe can handle server restarts, but systemd provides the same capability, and does so in a manner consistent with management of other services rather than by using an application-specific program.例如,mysqld_safe可以处理服务器重启,但systemd提供了相同的功能,并且以与其他服务管理一致的方式来处理,而不是使用特定于应用程序的程序。

Because systemd has the capability of managing multiple MySQL instances on platforms for which systemd support for MySQL is installed, mysqld_multi and mysqld_multi.server are unnecessary and are not installed.因为systemd能够在安装了systemd对MySQL的支持的平台上管理多个MySQL实例,所以mysqld_multimysqld_multi.server是不必要的,并且没有安装。

Overview of systemdsystemd概述

systemd provides automatic MySQL server startup and shutdown. systemd提供自动启动和关闭MySQL服务器。It also enables manual server management using the systemctl command. 它还使用systemctl命令启用手动服务器管理。For example:例如:

shell> systemctl {start|stop|restart|status} mysqld

Alternatively, use the service command (with the arguments reversed), which is compatible with System V systems:或者,使用service命令(参数颠倒),该命令与System V systems兼容:

shell> service mysqld {start|stop|restart|status}
Note注意

For the systemctl command (and the alternative service command), if the MySQL service name is not mysqld then use the appropriate name. 对于systemctl命令(以及备用service命令),如果MySQL服务名称不是mysqld,则使用适当的名称。For example, use mysql rather than mysqld on Debian-based and SLES systems.例如,在基于Debian和SLES的系统上使用mysql而不是mysqld

Support for systemd includes these files:对systemd的支持包括以下文件:

  • mysqld.service (RPM platforms), mysql.service (Debian platforms): systemd service unit configuration file, with details about the MySQL service.systemd服务单元配置文件,包含MySQL服务的详细信息。

  • mysqld@.service (RPM platforms), mysql@.service (Debian platforms): Like mysqld.service or mysql.service, but used for managing multiple MySQL instances.类似于mysqld.servicemysql.service,但用于管理多个mysql实例。

  • mysqld.tmpfiles.d: File containing information to support the tmpfiles feature. 包含支持tmpfiles功能的信息的文件。This file is installed under the name mysql.conf.此文件以mysql.conf的名称安装。

  • mysqld_pre_systemd (RPM platforms), mysql-system-start (Debian platforms): Support script for the unit file. 单元文件的支持脚本。This script assists in creating the error log file only if the log location matches a pattern (/var/log/mysql*.log for RPM platforms, /var/log/mysql/*.log for Debian platforms). 只有当日志位置与模式匹配时(RPM平台为/var/log/mysql*.log,Debian平台为/var/log/mysql/*.log),此脚本才有助于创建错误日志文件。In other cases, the error log directory must be writable or the error log must be present and writable for the user running the mysqld process.在其他情况下,对于运行mysqld进程的用户,错误日志目录必须是可写的,或者错误日志必须存在且可写。

Configuring systemd for MySQL为MySQL配置systemd

To add or change systemd options for MySQL, these methods are available:要添加或更改MySQL的systemd选项,可以使用以下方法:

  • Use a localized systemd configuration file.使用本地化的systemd配置文件。

  • Arrange for systemd to set environment variables for the MySQL server process.安排systemd为MySQL服务器进程设置环境变量。

  • Set the MYSQLD_OPTS systemd variable.设置MYSQLD_OPTS systemd变量。

To use a localized systemd configuration file, create the /etc/systemd/system/mysqld.service.d directory if it does not exist. 要使用本地化的systemd配置文件,请创建/etc/systemd/system/mysqld.service.d目录(如果不存在)。In that directory, create a file that contains a [Service] section listing the desired settings. 在该目录中,创建一个包含[Service]部分的文件,其中列出了所需的设置。For example:例如:

[Service]
LimitNOFILE=max_open_files
Nice=nice_level
LimitCore=core_file_limit
Environment="LD_PRELOAD=/path/to/malloc/library"
Environment="TZ=time_zone_setting"

The discussion here uses override.conf as the name of this file. 这里的讨论使用override.conf作为此文件的名称。Newer versions of systemd support the following command, which opens an editor and permits you to edit the file:较新版本的systemd支持以下命令,该命令可打开编辑器并允许您编辑文件:

systemctl edit mysqld  # RPM platforms
systemctl edit mysql   # Debian platforms

Whenever you create or change override.conf, reload the systemd configuration, then tell systemd to restart the MySQL service:无论何时创建或更改override.conf,请重新加载systemd配置,然后告诉systemd重新启动MySQL服务:

systemctl daemon-reload
systemctl restart mysqld  # RPM platforms
systemctl restart mysql   # Debian platforms

With systemd, the override.conf configuration method must be used for certain parameters, rather than settings in a [mysqld], [mysqld_safe], or [safe_mysqld] group in a MySQL option file:对于systemd,override.conf配置方法必须用于某些参数,而不是MySQL选项文件中[mysqld][mysqld_safe][safe_mysqld]组中的设置:

  • For some parameters, override.conf must be used because systemd itself must know their values and it cannot read MySQL option files to get them.对于某些参数,必须使用override.conf,因为systemd本身必须知道它们的值,并且不能读取MySQL选项文件来获取它们。

  • Parameters that specify values otherwise settable only using options known to mysqld_safe must be specified using systemd because there is no corresponding mysqld parameter.指定值的参数只能使用mysqld_safe已知的选项进行设置,必须使用systemd进行指定,因为没有相应的mysqld参数。

For additional information about using systemd rather than mysqld_safe, see Migrating from mysqld_safe to systemd.有关使用systemd而不是mysqld_safe的更多信息,请参阅从mysqld_safe迁移到systemd

You can set the following parameters in override.conf:您可以在override.conf中设置以下参数:

  • To set the number of file descriptors available to the MySQL server, use LimitNOFILE in override.conf rather than the open_files_limit system variable for mysqld or --open-files-limit option for mysqld_safe.要设置MySQL服务器可用的文件描述符数量,请在override.conf中使用LimitNOFILE,而不是mysqldopen_files_limit系统变量或mysqld_safe--open-files-limit选项。

  • To set the maximum core file size, use LimitCore in override.conf rather than the --core-file-size option for mysqld_safe.要设置最大核心文件大小,请在override.conf中使用LimitCore,而不是mysqld_safe--core-file-size选项。

  • To set the scheduling priority for the MySQL server, use Nice in override.conf rather than the --nice option for mysqld_safe.要设置MySQL服务器的调度优先级,请在override.conf中使用Nice,而不是mysqld_safe--nice选项。

Some MySQL parameters are configured using environment variables:一些MySQL参数是使用环境变量配置的:

  • LD_PRELOAD: Set this variable if the MySQL server should use a specific memory-allocation library.:如果MySQL服务器应使用特定的内存分配库,请设置此变量。

  • NOTIFY_SOCKET: This environment variable specifies the socket that mysqld uses to communicate notification of startup completion and service status change with systemd. :此环境变量指定mysqld用于与systemd通信启动完成和服务状态更改通知的套接字。It is set by systemd when the mysqld service is started. 它是在mysqld服务启动时由systemd设置的。The mysqld service reads the variable setting and writes to the defined location.mysqld服务读取变量设置并写入定义的位置。

    In MySQL 8.0, mysqld uses the Type=notify process startup type. 在MySQL 8.0中,mysqld使用Type=notify进程启动类型。(Type=forking was used in MySQL 5.7.) (MySQL 5.7中使用了Type=forking。)With Type=notify, systemd automatically configures a socket file and exports the path to the NOTIFY_SOCKET environment variable.Type=notify时,systemd自动配置套接字文件并将路径导出到NOTIFY_SOCKET环境变量。

  • TZ: Set this variable to specify the default time zone for the server.:设置此变量以指定服务器的默认时区。

There are multiple ways to specify environment variable values for use by the MySQL server process managed by systemd:有多种方法可以指定由systemd管理的MySQL服务器进程使用的环境变量值:

  • Use Environment lines in the override.conf file. override.conf文件中使用Environment行。For the syntax, see the example in the preceding discussion that describes how to use this file.有关语法,请参阅前面讨论中描述如何使用此文件的示例。

  • Specify the values in the /etc/sysconfig/mysql file (create the file if it does not exist). 指定/etc/sysconfig/mysql文件中的值(如果该文件不存在,则创建该文件)。Assign values using the following syntax:使用以下语法指定值:

    LD_PRELOAD=/path/to/malloc/library
    TZ=time_zone_setting

    After modifying /etc/sysconfig/mysql, restart the server to make the changes effective:修改/etc/sysconfig/mysql后,重新启动服务器以使更改生效:

    systemctl restart mysqld  # RPM platforms
    systemctl restart mysql   # Debian platforms

To specify options for mysqld without modifying systemd configuration files directly, set or unset the MYSQLD_OPTS systemd variable. 要在不直接修改systemd配置文件的情况下为mysqld指定选项,请设置或取消设置MYSQLD_OPTS systemd变量。For example:例如:

systemctl set-environment MYSQLD_OPTS="--general_log=1"
systemctl unset-environment MYSQLD_OPTS

MYSQLD_OPTS can also be set in the /etc/sysconfig/mysql file.MYSQLD_OPTS也可以在/etc/sysconfig/mysql文件中设置。

After modifying the systemd environment, restart the server to make the changes effective:修改systemd环境后,重新启动服务器以使更改生效:

systemctl restart mysqld  # RPM platforms
systemctl restart mysql   # Debian platforms

For platforms that use systemd, the data directory is initialized if empty at server startup. This might be a problem if the data directory is a remote mount that has temporarily disappeared: The mount point would appear to be an empty data directory, which then would be initialized as a new data directory. 对于使用systemd的平台,如果在服务器启动时数据目录为空,则会对其进行初始化。如果数据目录是临时消失的远程装载,则可能会出现问题:装载点似乎是空的数据目录,然后将其初始化为新的数据目录。To suppress this automatic initialization behavior, specify the following line in the /etc/sysconfig/mysql file (create the file if it does not exist):要抑制此自动初始化行为,请在/etc/sysconfig/mysql文件中指定以下行(如果该文件不存在,请创建该文件):

NO_INIT=true

Configuring Multiple MySQL Instances Using systemd使用systemd配置多个MySQL实例

This section describes how to configure systemd for multiple instances of MySQL.本节介绍如何为多个MySQL实例配置systemd。

Note注意

Because systemd has the capability of managing multiple MySQL instances on platforms for which systemd support is installed, mysqld_multi and mysqld_multi.server are unnecessary and are not installed.因为systemd能够在安装了systemd支持的平台上管理多个MySQL实例,所以mysqld_multimysqld_multi.server是不必要的,并且没有安装。

To use multiple-instance capability, modify the my.cnf option file to include configuration of key options for each instance. 要使用多实例功能,请修改my.cnf选项文件,以包含每个实例的关键选项配置。These file locations are typical:这些文件位置是典型的:

  • /etc/my.cnf or /etc/mysql/my.cnf (RPM platforms)

  • /etc/mysql/mysql.conf.d/mysqld.cnf (Debian platforms)

For example, to manage two instances named replica01 and replica02, add something like this to the option file:例如,要管理名为replica01和replica02的两个实例,请在选项文件中添加如下内容:

RPM platforms:RPM平台:

[mysqld@replica01]
datadir=/var/lib/mysql-replica01
socket=/var/lib/mysql-replica01/mysql.sock
port=3307
log-error=/var/log/mysqld-replica01.log

[mysqld@replica02]
datadir=/var/lib/mysql-replica02
socket=/var/lib/mysql-replica02/mysql.sock
port=3308
log-error=/var/log/mysqld-replica02.log

Debian platforms:Debian平台:

[mysqld@replica01]
datadir=/var/lib/mysql-replica01
socket=/var/lib/mysql-replica01/mysql.sock
port=3307
log-error=/var/log/mysql/replica01.log

[mysqld@replica02]
datadir=/var/lib/mysql-replica02
socket=/var/lib/mysql-replica02/mysql.sock
port=3308
log-error=/var/log/mysql/replica02.log

The replica names shown here use @ as the delimiter because that is the only delimiter supported by systemd.此处显示的副本名称使用@作为分隔符,因为这是systemd支持的唯一分隔符。

Instances then are managed by normal systemd commands, such as:然后,实例由正常的systemd命令管理,例如:

systemctl start mysqld@replica01
systemctl start mysqld@replica02

To enable instances to run at boot time, do this:要使实例在启动时运行,请执行以下操作:

systemctl enable mysqld@replica01
systemctl enable mysqld@replica02

Use of wildcards is also supported. 还支持使用通配符。For example, this command displays the status of all replica instances:例如,此命令显示所有副本实例的状态:

systemctl status 'mysqld@replica*'

For management of multiple MySQL instances on the same machine, systemd automatically uses a different unit file:为了管理同一台机器上的多个MySQL实例,systemd自动使用不同的单元文件:

  • mysqld@.service rather than mysqld.service (RPM platforms)

  • mysql@.service rather than mysql.service (Debian platforms)

In the unit file, %I and %i reference the parameter passed in after the @ marker and are used to manage the specific instance. 在单元文件中,%I%i引用在@标记之后传入的参数,用于管理特定实例。For a command such as this:对于这样的命令:

systemctl start mysqld@replica01

systemd starts the server using a command such as this:systemd使用如下命令启动服务器:

mysqld --defaults-group-suffix=@%I ...

The result is that the [server], [mysqld], and [mysqld@replica01] option groups are read and used for that instance of the service.

Note注意

On Debian platforms, AppArmor prevents the server from reading or writing /var/lib/mysql-replica*, or anything other than the default locations. To address this, you must customize or disable the profile in /etc/apparmor.d/usr.sbin.mysqld.

Note注意

On Debian platforms, the packaging scripts for MySQL uninstallation cannot currently handle mysqld@ instances. Before removing or upgrading the package, you must stop any extra instances manually first.

Migrating from mysqld_safe to systemd从mysqld_safe迁移到systemd

Because mysqld_safe is not installed on platforms that use systemd to manage MySQL, options previously specified for that program (for example, in an [mysqld_safe] or [safe_mysqld] option group) must be specified another way:

  • Some mysqld_safe options are also understood by mysqld and can be moved from the [mysqld_safe] or [safe_mysqld] option group to the [mysqld] group. This does not include --pid-file, --open-files-limit, or --nice. To specify those options, use the override.conf systemd file, described previously.

    Note注意

    On systemd platforms, use of [mysqld_safe] and [safe_mysqld] option groups is not supported and may lead to unexpected behavior.

  • For some mysqld_safe options, there are alternative mysqld procedures. For example, the mysqld_safe option for enabling syslog logging is --syslog, which is deprecated. To write error log output to the system log, use the instructions at Section 5.4.2.8, “Error Logging to the System Log”.

  • mysqld_safe options not understood by mysqld can be specified in override.conf or environment variables. For example, with mysqld_safe, if the server should use a specific memory allocation library, this is specified using the --malloc-lib option. For installations that manage the server with systemd, arrange to set the LD_PRELOAD environment variable instead, as described previously.