1. 程式人生 > >Ubuntu 18.04 使用Systemd管理MySQL 5.6

Ubuntu 18.04 使用Systemd管理MySQL 5.6

 

 

轉自:https://blog.csdn.net/skykingf/article/details/45225981

如何用Systemd管理 general 包安裝的MySQL呢? 

首先看看yum安裝的MySQL提供的Systemd管理指令碼

$ cat /usr/lib/systemd/system/mysqld.service 
#
# Simple MySQL systemd service file
#
# systemd supports lots of fancy features, look here (and linked docs) for a full list: 
#   http://www.freedesktop.org/software/systemd/man/systemd.exec.html
#
# Note: this file ( /usr/lib/systemd/system/mysql.service )
# will be overwritten on package upgrade, please copy the file to 
#
#  /etc/systemd/system/mysql.service 
#  
# to make needed changes.
# 
# systemd-delta can be used to check differences between the two mysql.service files.
#
 
[Unit]
Description=MySQL Community Server
After=network.target
After=syslog.target
 
[Install]
WantedBy=multi-user.target
Alias=mysql.service
 
[Service]
User=mysql
Group=mysql
 
# Execute pre and post scripts as root
PermissionsStartOnly=true
 
# Needed to create system tables etc.
ExecStartPre=/usr/bin/mysql-systemd-start pre
 
# Start main service
ExecStart=/usr/bin/mysqld_safe
 
# Don't signal startup success before a ping works
ExecStartPost=/usr/bin/mysql-systemd-start post
 
# Give up if ping don't get an answer
TimeoutSec=600
 
Restart=always
PrivateTmp=false

適當修改 /lib/systemd/system/mysqld.service 如下:

After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
Alias=mysql.service
[Service]
User=mysql
Group=mysql
PermissionsStartOnly=true
ExecStart=/usr/local/mysql/bin/mysqld_safe
TimeoutSec=600
Restart=always
PrivateTmp=false

  然後

ln -s /lib/systemd/system/mysqld.service /etc/systemd/mysqld.service

ln -s /lib/systemd/system/mysqld.service /etc/systemd/mysqld.service

然後 使用 systemctl 設定 mysqld.service自動啟動

systemctl enable mysqld.service

  檢視是否設定成:

 

systemctl 常見用法:

開機啟動:

systemctl enable ***.service

停止開機啟動:

systemctl disable ***.service

啟動/停止/重啟服務:

systemctl start/stop/restart ***.service

查詢服務狀態:

systemctl status ***.service

列出系統服務,-t後用tab鍵可以關聯出所有支援的unit型別

查詢某項服務是否active,以sshd.serice為例

systemctl is-active sshd.service

直接檢視某項服務的配置檔案,以mysqld.serice為例

 

檢視環境變數