1. 程式人生 > 其它 >CentOS下MySQL開機自啟動服務開啟和關閉

CentOS下MySQL開機自啟動服務開啟和關閉

CentOS 6

使用chkconfig 命令設定自啟動服務,chkconfig命令主要用來更新、啟動、停止、查詢系統服務在不同執行等級的狀態。謹記chkconfig不是立即自動禁止或啟用一個服務,它只是簡單的改變了符號連線。

語法:

chkconfig [--add][--del][--list] [系統服務]
chkconfig [--level <執行級別>] [系統服務] [on/off/reset]

引數:

   --add 增加所指定的系統服務,讓chkconfig指令得以管理它,並同時在系統啟動的敘述檔案內增加相關資料。
   --del 刪除所指定的系統服務,不再由chkconfig指令管理,並同時在系統啟動的敘述檔案內刪除相關資料。
   --list
顯示所有系統服務在不同執行級別的狀態資訊。如果指定了服務名,那麼只顯示指定的服務在不同執行級的狀態 --level<等級代號>[on/off/reset]  指定系統服務要在哪一個執行等級中開啟或關畢。 0表示:表示關機 1表示:單使用者模式 2表示:無網路連線的多使用者命令列模式 3表示:有網路連線的多使用者命令列模式 4表示:不可用 5表示:帶圖形介面的多使用者模式 6表示:重新啟動 

設定mysql開機自啟動:

cd /usr/local/mysql
cp support-files/mysql.server /etc/init.d/mysql.server

chkconfig
mysql.server on #設定mysqld在執行定級2、3、4、5下為on chkconfig --level 35 mysql.server on #設定mysqld在執行等級3、5為開啟狀態 chkconfig --list mysql.server #列出mysqld服務狀態 chkconfig --list #列出所有的系統服務狀態

  

CentOS 7

使用systemctl命令設定自啟動服務:

cd /usr/lib/systemd/system
touch mysqld.service 

編輯內容如下

vim mysqld.service 
# Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # systemd service file for MySQL forking server # [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 Type=forking PIDFile=/usr/local/mysql/run/mysqld.pid # Disable service start and stop timeout logic of systemd for mysqld service. TimeoutSec=0 # Execute pre and post scripts as root PermissionsStartOnly=true # Needed to create system tables #ExecStartPre=/usr/bin/mysqld_pre_systemd # Start main service ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/run/mysqld.pid $MYSQLD_OPTS # Use this to switch malloc implementation EnvironmentFile=-/etc/sysconfig/mysql # Sets open_files_limit LimitNOFILE = 65535 Restart=on-failure RestartPreventExitStatus=1 PrivateTmp=false

載入

systemctl daemon-reload
systemctl enable mysqld.service
systemctl is-enabled mysqld

啟動服務:systemctl start xxx.service
關閉服務:systemctl stop xxx.service
重啟服務:systemctl restart xxx.service
顯示服務的狀態:systemctl status xxx.service
在開機時啟用服務:systemctl enable xxx.service
在開機時禁用服務:systemctl disable xxx.service
檢視服務是否開機啟動:systemctl is-enabled xxx.service
檢視已啟動的服務列表:systemctl list-unit-files|grep enabled
檢視啟動失敗的服務列表:systemctl --failed