當你使用CentOS安裝并配置好MySQL數據庫后,你可能會發現每次啟動時都需要手動啟動MySQL服務。這樣不僅麻煩,還容易出現人為失誤。因此,我們需要配置MySQL自動啟動腳本以方便我們的使用。
下面我們來一步一步地創建一個MySQL自動啟動腳本。
1.創建啟動腳本文件
sudo touch /etc/init.d/mysql sudo chmod 755 /etc/init.d/mysql
2.編輯啟動腳本
sudo vi /etc/init.d/mysql
將以下代碼復制并粘貼到文件中:
#! /bin/sh # # mysql This shell script takes care of starting and stopping # MySQL databases. # # chkconfig: - 64 36 # description: MySQL database server. # processname: mysqld # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ ${NETWORKING} = "no" ] && exit 0 [ -x /usr/bin/mysqld_safe ] || exit 0 RETVAL=0 prog="MySQL" start() { # Start daemons. echo -n $"Starting $prog: " daemon /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/run/mysqld/mysqld.pid RETVAL=$? echo [ $RETVAL -eq 0 ] && touch /var/lock/subsys/mysql return $RETVAL } stop() { # Stop daemons. echo -n $"Stopping $prog: " killproc mysqld RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/mysql return $RETVAL } # See how we were called. case "$1" in start) start ;; stop) stop ;; status) status mysqld RETVAL=$? ;; restart|reload) stop start RETVAL=$? ;; *) echo $"Usage: $0 {start|stop|status|restart|reload}" RETVAL=1 esac exit $RETVAL
3.設置文件權限
sudo chmod 755 /etc/init.d/mysql
4.安裝和配置chkconfig
sudo yum install chkconfig sudo chkconfig --add mysql sudo chkconfig --level 345 mysql on
5.測試自動啟動腳本
sudo service mysql start sudo service mysql stop sudo service mysql restart sudo service mysql status
這樣,MySQL自動啟動腳本就配置好了。從現在開始,每次系統啟動時,MySQL服務都將自動啟動。