MySQL systemd on Linux 7

過去在Linux 6系列上用tar包安裝MySQL,要設定成系統服務很簡單,只要把mysql.server放在/etc/init.d裡面即可:
cp support-files/mysql.server /etc/init.d/mysqld


但在Linux 7系列中service的角色已被systemd給取代(雖然service還是能用),但要設開機自動啟動chkconfig就會有問題:
[root@mysql-course support-files]# chkconfig --list mysqld

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

所以可以的話,我還是盡量設法弄成Linux 7相容的方式比較好
看了手冊的教學:https://dev.mysql.com/doc/refman/5.7/en/server-management-using-systemd.html
實在是太高深莫測了,我功力還不到家,看不太懂阿
後來看這篇http://superuser.com/questions/384365/systemctl-enable-differs-from-systemctl-start-how的說明,試做一下:
[root@mysql-course ~]# systemctl status mysqld.service
mysqld.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)

[root@mysql-course ~]# vim /etc/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql
Type=forking
PermissionsStartOnly=true
ExecStart=/opt/percona57/bin/mysql.server start
ExecStop=/opt/percona57/bin/mysql.server stop
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false

[root@mysql-course ~]# systemctl daemon-reload
[root@mysql-course ~]# systemctl status mysqld.service
mysqld.service - MySQL Server
   Loaded: loaded (/etc/systemd/system/mysqld.service; disabled)
   Active: inactive (dead)

[root@mysql-course ~]# systemctl enable mysqld.service
ln -s '/etc/systemd/system/mysqld.service' '/etc/systemd/system/multi-user.target.wants/mysqld.service'
[root@mysql-course ~]# systemctl start mysqld.service
[root@mysql-course ~]# systemctl status mysqld.service
mysqld.service - MySQL Server
   Loaded: loaded (/etc/systemd/system/mysqld.service; enabled)
   Active: active (running) since Thu 2016-09-29 17:36:57 CST; 7s ago
  Process: 2858 ExecStart=/opt/percona57/bin/mysql.server start (code=exited, status=0/SUCCESS)
 Main PID: 2873 (mysqld_safe)
   CGroup: /system.slice/mysqld.service
           ├─2873 /bin/sh /opt/percona57/bin/mysqld_safe --datadir=/opt/percona57/data --pid-file=/opt/percona57/data/...
           └─3656 /opt/percona57/bin/mysqld --basedir=/opt/percona57 --datadir=/opt/percona57/data --plugin-dir=/opt/p...

Sep 29 17:36:57 mysql-course mysql.server[2858]: Starting MySQL (Percona Server).... SUCCESS!
Sep 29 17:36:57 mysql-course systemd[1]: Started MySQL Server.

[root@mysql-course ~]# systemctl is-enabled mysqld.service 
enabled
[root@mysql-course ~]# systemctl is-active mysqld.service 
active

這樣就算是設定好啦,可以測試看看直接以systemctl指令開關MySQL
[root@mysql-course ~]# systemctl stop mysqld.service
[root@mysql-course ~]# systemctl is-active mysqld.service
inactive
[root@mysql-course ~]# systemctl start mysqld.service
[root@mysql-course ~]# systemctl is-active mysqld.service
active
[root@mysql-course ~]# systemctl restart mysqld.service 
[root@mysql-course ~]# systemctl is-active mysqld.service 
active

看文章裡有的人是直接用mysqld來設為ExecStart的指令,實際測試後也是能正常啟用:
[root@mysql-course ~]# vim /etc/systemd/system/mysqld.service
ExecStart=/opt/percona57/bin/mysqld --defaults-file=/etc/my.cnf --datadir=/opt/percona57/data --socket=/tmp/mysql.sock
#ExecStart=/opt/percona57/bin/mysql.server start
#ExecStop=/opt/percona57/bin/mysql.server stop

[root@mysql-course percona57]# vim /etc/systemd/system/mysqld.service 
[root@mysql-course percona57]# systemctl daemon-reload 
[root@mysql-course percona57]# systemctl start mysqld.service 

^C -->每次都會卡在這裡,都要用crtl+c離開很困擾阿
[root@mysql-course percona57]# systemctl status mysqld.service 
mysqld.service - MySQL Server
   Loaded: loaded (/etc/systemd/system/mysqld.service; enabled)
   Active: activating (start) since Thu 2016-09-29 17:59:05 CST; 26s ago
  Control: 7103 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─7103 /opt/percona57/bin/mysqld --defaults-file=/etc/my.cnf --datadir=/opt/percona57/data --socket=/tmp/my...

[root@mysql-course percona57]# systemctl stop mysqld.service 
[root@mysql-course percona57]# systemctl status mysqld.service 
mysqld.service - MySQL Server
   Loaded: loaded (/etc/systemd/system/mysqld.service; enabled)
   Active: inactive (dead) since Thu 2016-09-29 18:01:21 CST; 1s ago
  Process: 7167 ExecStart=/opt/percona57/bin/mysqld --defaults-file=/etc/my.cnf --datadir=/opt/percona57/data --socket=/tmp/mysql.sock& (code=exited, status=0/SUCCESS)


總結:
跟官方手冊的內容相比,這個設定過程很簡單,但不知道未來實際執行上會不會有什麼問題
另外就是設定mysqld的那個雖然也可以啟動和關閉,但看那個狀態是Active: activating (start)而不是active (running)就不是很舒服阿~~~
我個人還是習慣用mysql.server的這個腳本來當作啟動服務。

留言

這個網誌中的熱門文章

【工作筆記】SQL Timeout追蹤

MySQL監控軟體MEM及PMM介紹

MySQL Router 設定檔說明