2.5.9 Managing MySQL Server with systemd (original) (raw)

2.5.9 Managing MySQL Server with systemd

If you install MySQL using an RPM or Debian package on the following Linux platforms, server startup and shutdown is managed by 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 theMySQL Secure Deployment Guide.

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. SeeSection 2.8.7, “MySQL Source-Configuration Options”.

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. 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] or[safe_mysqld] sections in option files is not supported and might lead to unexpected behavior.

Because systemd has the capability of managing multiple MySQL instances on platforms for which systemd support for MySQL is installed, mysqld_multi andmysqld_multi.server are unnecessary and are not installed.

Overview of systemd

systemd provides automatic MySQL server startup and shutdown. It also enables manual server management using thesystemctl command. For example:

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

Alternatively, use the service command (with the arguments reversed), which is compatible with System V systems:

$> 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. For example, use mysql rather than mysqld on Debian-based and SLES systems.

Support for systemd includes these files:

Configuring systemd for MySQL

To add or change systemd options for MySQL, these methods are available:

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"

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

Whenever you create or changeoverride.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

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:

For additional information about using systemd rather thanmysqld_safe, seeMigrating from mysqld_safe to systemd.

You can set the following parameters inoverride.conf:

Some MySQL parameters are configured using environment variables:

There are multiple ways to specify environment variable values for use by the MySQL server process managed by systemd:

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

After modifying /etc/sysconfig/mysql, restart the server to make the changes effective:

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

To specify options for mysqld without modifying systemd configuration files directly, set or unset theMYSQLD_OPTS systemd variable. 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.

After modifying the systemd environment, restart the server to make the changes effective:

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. 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

Configuring Multiple MySQL Instances Using systemd

This section describes how to configure systemd for multiple instances of MySQL.

Note

Because systemd has the capability of managing multiple MySQL instances on platforms for which systemd support is installed,mysqld_multi andmysqld_multi.server are unnecessary and are not installed.

To use multiple-instance capability, modify themy.cnf option file to include configuration of key options for each instance. These file locations are typical:

For example, to manage two instances namedreplica01 and replica02, add something like this to the option file:

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

The replica names shown here use @ as the delimiter because that is the only delimiter supported by systemd.

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

For management of multiple MySQL instances on the same machine, systemd automatically uses a different unit file:

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 starts the server using a command such as this:

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 handlemysqld@ instances. Before removing or upgrading the package, you must stop any extra instances manually first.

Migrating from mysqld_safe to 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: