在etc/systemd/system 下面
[root@localhost ~]# touch /etc/systemd/system/mysqld.service
[root@localhost ~]# cd /etc/systemd/system
编辑mysqld.service文件,加入如下内容:
[root@localhost system]# vi mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
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
ExecStart=/opt/mysql-5.7.18/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
保存退出
备注:ExecStart=/opt/mysql-5.7.18/bin/mysqld (此处请对应修改为MySQL程序所在的路径)
查找mysqld路径,例如:
[root@localhost system]# which mysqld
/opt/mysql-5.7.18/bin/mysqld
通过systemctl方式启动mysql5.7:
[root@localhost system]# systemctl start mysqld
开机自启动使用systemctl enable mysqld
看到这个(Created symlink from ......)表示加入开机自启成功,服务器重新启动,该服务也会直接开启,不需要再手动开启该服务,很方便。