MySQL manager with systemd

1. 使用systemd 管理MySQL

如果使用通用二进制安装发行版安装MySQL ,可以参考安装章节配置systemd 对mysql 的支持.
如果使用源码版本进行安装请添加编译项-DWITH_SYSTEMD=1

:::alert-warning

注意: 在systemd 类型的平台上安装MySQL ,mysql_safe 脚本是不需要的.所有的自动重启和诊断systemd 方式都已经做了.

因为systemd 支持管理多个MySQL 实例,所以mysqld_multi 和 mysqld_multi.server 都没必要装.
:::

1.1. 认识systemd

systemctl 用法:

systemctl {start|stop|restart|status} mysqld

system v 系统方式:

service mysqld {start|stop|restart|status}

对于systemd 的文件支持:

1.2. 配置systemd 的MySQL 支持

使用systemd 来管理MySQL 有三种方法.

使用本地化的systemd 配置文件,创建'/etc/systemd/system/mysqld.service.d 目录.如果它不存在.在该目录中,创建一个包含设置[Service] 的文件.

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

以下命令会打开一个oerride.conf 文件.
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 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

override.conf 配置方法中必须要用到某些参数,而不是MySQL 选项文件中的[mysqld]或mysqld_safe]

你可以在override.conf 文件中设置以下参数.

    [Service]
    PIDFile=/var/run/mysqld/mysqld-custom.pid
    ExecStart=
    ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld-custom.pid $MYSQLD_OPTS

一些MySQL 参数是使用环境变量配置的.

很多方法可以指定环境变量值,systemd 管理mysql 时使用.


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

在更改/etc/sysconfig/mysql 后,重启服务器生效

    systemctl restart mysqld  # RPM platforms
    systemctl restart mysql   # Debian platforms
systemctl set-environment MYSQLD\_OPTS="--general\_log=1"
systemctl unset-environment MYSQLD_OPTS

MYSQLD_OPTS 也可以设置为/etc/sysconfig/mysql 文件.

修改之后重启服务.


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

对于systemd 平台,如果服务器启动时为空,则初始化数据目录.
如果数据目是暂时消失的远程挂载.会出现问题,挂载点显示为一个空数据数据目录,然后初始化为一个新的数据目录.要抑制这种初始化的行为.使用/etc/sysconfig/mysql 文件中指定以下行

NO_INIT=true

1.3. 使用systemd 配置多个MySQL 实例

详细描述了怎么使用systemd 配置多个MySQL 实例.
:::alert-warning

Note
由于systemd 能够管理和配置多个MySQL 实例.所以msyql_multi 没有必要进行安装
:::

例如:想要两个replicat01replica02 可以在选项文件中添加如下内容.

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

这里显示复制名称使用"@" 作为分隔符,这是systemd 支持的唯一分隔符.

然后使用systemd 进行实例管理.

systemctl start mysqld@replica01
systemctl start mysqld@replica02

:::alert-warning
note:

  1. 启动之前需要关闭selinux 否则,MySQL 会出现文件目录权限不足的情况.
2019-07-29T06:11:04.168545Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.17) initializing of server in progress as pro
cess 5680
2019-07-29T06:11:14.545811Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ha8jsdPZuN+W
2019-07-29T06:11:19.545373Z 0 [System] [MY-013170] [Server] /usr/sbin/mysqld (mysqld 8.0.17) initializing of server has completed
2019-07-29T06:11:23.035233Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.17) starting as process 5731
2019-07-29T06:11:23.049201Z 1 [ERROR] [MY-012592] [InnoDB] Operating system error number 13 in a file operation.
2019-07-29T06:11:23.049244Z 1 [ERROR] [MY-012595] [InnoDB] The error means mysqld does not have the access rights to the directory.
2019-07-29T06:11:23.049281Z 1 [ERROR] [MY-012270] [InnoDB] os_file_get_status() failed on './ibdata1'. Can't determine file permission
s
2019-07-29T06:11:23.049354Z 1 [ERROR] [MY-010334] [Server] Failed to initialize DD Storage Engine
2019-07-29T06:11:23.049464Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
2019-07-29T06:11:23.049583Z 0 [ERROR] [MY-010119] [Server] Aborting
2019-07-29T06:11:23.050267Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.17)  MySQL Community Serve
r - GPL.
  1. 启动后自动初始化每个实例的目录且安装相关数据文件.
  2. 管理多个实例要把[mysqld] ,[mysql_safe] 配置文件全部注释,否则,会读取这些变量值.
    :::

如若要开机自启动:

systemctl enable mysqld@replica01
systemctl enable mysqld@replica02

systemd 支持通配符操作.


systemctl status 'mysqld@replica*'

对于同一台机器上有多个MySQL 实例管理, systemd自动使用不同的管理文件

在管理文件中,%I%I 引用在'@' 标记之后的传入参数,并用于管理特定的实例.

systemctl start mysqld@replica01

systemd 中的管理文件使用这样的命令行进行启动服务器.

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

结果是[server], [mysqld], and [mysqld@replica01] 配置中的参数将会用于启动的实例.

Note

在Debian 平台上,AppArmor 阻止在/var/lib/mysql-replica 目录之外创建数据文件.如果要解决这个问题.你必须在/etc/apparmor.d/usr.sbin.mysqld 中自定义或禁用配置文件.

Note

在Debian 平台上,无法使用MySQL 卸载脚本处理msyqld@"实例" .在删除或升级包之前,必须手动停止任何的额外的实例.

1.4. 从msyqld_safe 迁移到systemd 上.

请参照第一章节进行systemd mysql.server 服务编写.