• 环境基于Centos7.6

1.确定mysql安装目录、mysqld服务与my.cnf文件位置

1
2
3
4
5
6
7
8
[root@localhost ~]# whereis mysql
mysql: /usr/lib64/mysql /usr/local/mysql /usr/local/mysql/bin/mysql

[root@localhost ~]# whereis mysqld
mysqld: /usr/local/mysql/bin/mysqld

[root@localhost local]# whereis my.cnf
my: /etc/my.cnf

2.新建systemctl配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
vi /usr/lib/systemd/system/mysql.service

[Unit]
Description=MySQL Server
Documentation=man:mysqld
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
#配置mysqld路径与my.cnf路径
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
#Restart=on-failure
#RestartPreventExitStatus=1
#PrivateTmp=false

3.设置开机启动

1
systemctl enable mysql

4.验证

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#重启服务器
init 6
#查看MySQL状态
[root@localhost ~]# systemctl status mysql
● mysqld.service - LSB: start and stop MySQL
Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
Active: active (running) since Wed 2021-05-26 18:20:26 CST; 9s ago
Docs: man:systemd-sysv-generator(8)
Process: 1592 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
Tasks: 28
Memory: 225.5M
CGroup: /system.slice/mysqld.service
├─1603 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql/date/3306 --pid...
└─1771 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql/d...

May 26 18:20:23 localhost.localdomain systemd[1]: Starting LSB: start and stop MySQL...
May 26 18:20:26 localhost.localdomain mysqld[1592]: Starting MySQL... SUCCESS!
May 26 18:20:26 localhost.localdomain systemd[1]: Started LSB: start and stop MySQL.

5.关闭开机自启

1
systemctl disable mysql

6.验证

1
2
3
4
5
6
7
8
9
10
11
12
#重启服务器
init 6
#查看MySQL状态
[root@localhost ~]# systemctl status mysql
● mysql.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysql.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:mysqld
http://dev.mysql.com/doc/refman/en/using-systemd.html
[root@localhost ~]# systemctl enable mysql
Created symlink from /etc/systemd/system/multi-user.target.wants/mysql.service to /usr/lib/systemd/
system/mysql.service.