CentOS7開始使得以往老版本系統服務的/etc/init.d的啟動腳本的方式就此改變,在CentOS7中所有對服務的管理都集中到了systemctl當中。
systemctl是一個系統管理守護程序、工具和庫的集合,用于取代以往的System V、service和chkconfig指令。
建立用于啟動MySQL的配置檔案
[[email protected] ~]# touch /usr/lib/systemd/system/mysqld.service
[[email protected] ~]# cd /usr/lib/systemd/system
編輯mysqld.service檔案,加入如下内容:
[[email protected] 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路徑,例如:
[[email protected] system]# which mysqld
/opt/mysql-5.7.18/bin/mysqld
通過systemctl方式啟動mysql5.7:
[[email protected] system]# systemctl start mysqld
檢查MySQL運作狀态:
[[email protected] system]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.18-log MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql>
mysql> \s
--------------
mysql Ver 14.14 Distrib 5.7.18, for linux-glibc2.5 (x86_64) using EditLine wrapper
Connection id: 6
Current database:
Current user: [email protected]
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.18-log MySQL Community Server (GPL)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /tmp/mysql.sock
Uptime: 45 sec
Threads: 3 Questions: 6 Slow queries: 0 Opens: 108 Flush tables: 1 Open tables: 101 Queries per second avg: 0.133
--------------
mysql>
關閉防火牆:
[[email protected] system]# systemctl stop firewalld.service
[[email protected] system]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.Fedoraproject.FirewallD1.service.
設定mysql的開機啟動:
[[email protected] system]# systemctl enable mysqld
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.
[[email protected] system]# systemctl list-unit-files | grep mysqld
mysqld.service enabled
取消mysql的開機自啟動:
[[email protected] system]# systemctl disable mysqld
Removed symlink /etc/systemd/system/multi-user.target.wants/mysqld.service.
[[email protected] system]# systemctl list-unit-files | grep mysqld
mysqld.service disabled