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管理:
RPM package platforms:RPM软件包平台:
Enterprise Linux variants version 7 and higher企业Linux变体版本7及更高版本
SUSE Linux Enterprise Server 12 and higherSUSE Linux Enterprise Server 12及更高版本
Fedora 29 and higherFedora29及以上
Debian family platforms:Debian系列平台:
Debian platformsDebian平台
Ubuntu platforms
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 如果从使用systemd的平台上的源发行版安装MySQL,请通过使用-DWITH_SYSTEMD=1
CMake option. -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:以下讨论涉及这些主题:
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_multi和mysqld_multi.server是不必要的,并且没有安装。
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}
For the systemctl command (and the alternative service command), if the MySQL service name is not 对于systemctl命令(以及备用service命令),如果MySQL服务名称不是mysqld,则使用适当的名称。mysqld
then use the appropriate name. For example, use 例如,在基于Debian和SLES的系统上使用mysql而不是mysqld。mysql
rather than mysqld
on Debian-based and SLES systems.
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.service
或mysql.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 (只有当日志位置与模式匹配时(RPM平台为/var/log/mysql*.log
for RPM platforms, /var/log/mysql/*.log
for Debian platforms). /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进程的用户,错误日志目录必须是可写的,或者错误日志必须存在且可写。
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 要使用本地化的systemd配置文件,请创建/etc/systemd/system/mysqld.service.d
directory if it does not exist. /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 对于systemd,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
配置方法必须用于某些参数,而不是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 要设置MySQL服务器可用的文件描述符数量,请在LimitNOFILE
in override.conf
rather than the open_files_limit
system variable for mysqld or --open-files-limit
option for mysqld_safe.override.conf
中使用LimitNOFILE
,而不是mysqld的open_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 要设置MySQL服务器的调度优先级,请在Nice
in override.conf
rather than the --nice
option for mysqld_safe.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 在MySQL 8.0中,mysqld使用Type=notify
process startup type. Type=notify
进程启动类型。((MySQL 5.7中使用了Type=forking
was used in 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 要在不直接修改systemd配置文件的情况下为mysqld指定选项,请设置或取消设置MYSQLD_OPTS
systemd variable. 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
This section describes how to configure systemd for multiple instances of MySQL.本节介绍如何为多个MySQL实例配置systemd。
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_multi和mysqld_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:replica0
1和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.
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
.
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.
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.
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.