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

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

MySQL 8.4 参考手册 / 2 安装 MySQL / 2.5 在 Linux 上安装 MySQL / 2.5.9 使用 systemd 管理 MySQL 服务器
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

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

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

  • RPM 软件包平台:RPM package platforms:

    • Enterprise Linux 变体版本 7 及更高版本Enterprise Linux variants version 7 and higher

    • SUSE Linux Enterprise Server 12 及更高版本SUSE Linux Enterprise Server 12 and higher

    • Fedora 29 及更高版本Fedora 29 and higher

  • Debian 系列平台:Debian family platforms:

    • Debian 平台Debian platforms

    • Ubuntu 平台Ubuntu platforms

如果您在使用 systemd 的平台上从通用二进制发行版安装 MySQL,则可以按照安装后设置中提供的说明手动配置 systemd 对 MySQL 的支持MySQL 8.4 安全部署指南部分。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.4 Secure Deployment Guide.

如果您在使用 systemd 的平台上从源发行版安装 MySQL,请通过使用 -DWITH_SYSTEMD=1 CMake 选项。请参阅第 2.8.7 节“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. See Section 2.8.7, “MySQL Source-Configuration Options”.

以下讨论涵盖以下主题:The following discussion covers these topics:

Note

在安装了 MySQL 的 systemd 支持的平台上,诸如 mysqld_safe 和 System V 初始化脚本是不必要的,不会安装。例如,mysqld_safe 可以处理服务器重新启动,但 systemd 提供相同的功能,并且以与其他服务管理一致的方式执行此操作,而不是使用特定于应用程序的程序。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. 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.

One implication of the non-use of mysqld_safe on platforms that use systemd for server management is that use of [mysqld_safe]或者[safe_mysqld] sections in option files is not supported and might lead to unexpected behavior.One implication of the non-use of mysqld_safe on platforms that use systemd for server management is that use of [mysqld_safe] or [safe_mysqld] sections in option files is not supported and might lead to unexpected behavior.

由于 systemd 能够在安装了 MySQL 的 systemd 支持的平台上管理多个 MySQL 实例,mysqld_multi 和 mysqld_multi.server 是不必要的,不会安装。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 概述Overview of systemd

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

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

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

$> service mysqld {start|stop|restart|status}
Note

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

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

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

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

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

  • mysqld_pre_systemd(RPM 平台)、mysql-system-start(Debian 平台):单元文件的支持脚本。仅当日志位置与某个模式匹配时,此脚本才协助创建错误日志文件(对于 RPM 平台为 /var/log/mysql*.log,对于 Debian 平台为 /var/log/mysql/*.log)。在其他情况下,错误日志目录必须可写,或者错误日志必须存在并且对于运行 mysqld 进程的用户来说是可写的。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). 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.

配置 systemd MySQLConfiguring systemd for MySQL

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

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

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

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

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

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

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

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

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

对于 systemd,某些参数必须使用 override.conf 配置方法,而不是 [mysqld]、[mysqld_safe] 或 [safe_mysqld] 组: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:

  • 对于某些参数,必须使用 override.conf,因为 systemd 本身必须知道它们的值,并且无法读取 MySQL 选项文件来获取它们。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.

  • 指定值的参数,否则只能使用已知选项设置mysqld_safe 必须使用 systemd 指定,因为没有相应的 mysqld 参数。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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

对于使用 systemd 的平台,如果服务器启动时数据目录为空,则会对其进行初始化。如果数据目录是暂时消失的远程挂载,这可能会出现问题:挂载点将显示为空数据目录,然后将其初始化为新的数据目录。要禁止这种自动初始化行为,请在 /etc/sysconfig/mysql 文件中指定以下行(如果该文件不存在,则创建该文件):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. To suppress this automatic initialization behavior, specify the following line in the /etc/sysconfig/mysql file (create the file if it does not exist):

NO_INIT=true

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

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

Note

因为 systemd 具有管理多个 MySQL 实例的功能在安装了 systemd 支持的平台上,mysqld_multi 和 mysqld_multi.server 是不必要的,不会安装。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.

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

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

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

例如,管理两个名为 replica01 和 replica02,将类似内容添加到选项文件中:For example, to manage two instances named replica01 and replica02, add something like this to the option file:

RPM 平台:RPM platforms:

[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 平台:Debian platforms:

[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

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

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

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*'

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

  • mysqld@.service 而不是 mysqld.service(RPM 平台)mysqld@.service rather than mysqld.service (RPM platforms)

  • mysql@.service 而不是 mysql.service (Debian 平台)mysql@.service rather than mysql.service (Debian platforms)

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

systemctl start mysqld@replica01

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

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

结果是读取 [server]、[mysqld] 和 [mysqld@replica01] 选项组并将其用于该服务实例。The result is that the [server], [mysqld], and [mysqld@replica01] option groups are read and used for that instance of the service.

Note

在 Debian 平台上,AppArmor 会阻止服务器读取或写入 /var/lib/mysql-replica* 或默认位置以外的任何位置。要解决此问题,您必须在 /etc/apparmor.d/usr.sbin.mysqld.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

在 Debian 平台上,MySQL 卸载的打包脚本当前无法处理 mysqld@ 实例。在删除或升级软件包之前,您必须先手动停止任何额外的实例。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.

从 mysqld_safe 迁移到 systemdMigrating from mysqld_safe to systemd

因为 mysqld_safe 未安装在使用 systemd 管理 MySQL 的平台上,之前为该程序指定的选项(例如,在 [mysqld_safe] 或 [safe_mysqld] 选项组中)必须以其他方式指定: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:

  • 某些mysqld_safe 选项也可以被 mysqld 理解,并且可以从 [mysqld_safe] 或 [safe_mysqld] 选项组添加到 [mysqld] 组。这不包括--pid-file,--open-files-limit,或--nice。要指定这些选项,请使用 override.conf systemd 文件(如前所述)。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

    在 systemd 平台上,不支持使用 [mysqld_safe] 和 [safe_mysqld] 选项组,并且可能会导致意外行为。On systemd platforms, use of [mysqld_safe] and [safe_mysqld] option groups is not supported and may lead to unexpected behavior.

  • 对于某些mysqld_safe 选项,还有替代的mysqld 程序。例如,用于启用 syslog 日志记录的 mysqld_safe 选项为 --syslog,已弃用。要将错误日志输出写入系统日志,请使用第 7.4.2.8 节“错误记录到系统日志”中的说明。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 7.4.2.8, “Error Logging to the System Log”.

  • mysqld_safe mysqld 可以在 override.conf 或环境变量中指定。例如,对于 mysqld_safe,如果服务器应使用特定的内存分配库,则使用 --malloc-lib 选项。对于使用 systemd 管理服务器的安装,请按照前面所述设置 LD_PRELOAD 环境变量。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.