Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux MySQL 開機自動啟動三種方法:chkconfig 註冊服務、自訂啟動腳本 rc.d、完整 start/stop/restart 服務腳本範例

編輯:Linux 教學

Linux 讓 MySQL 開機自啟動主要透過將啟動指令加入系統服務。本文提供三種方法:標準 chkconfig 註冊、簡易自訂啟動腳本連結至 rc.d、功能完整的 start/stop/restart 服務管理腳本。

方法一:標準 chkconfig 註冊 (推薦)

cp /usr/local/mysql5/share/mysql/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 2345 mysqld on
chown mysql:mysql -R /usr/local/mysql5/
service mysqld start

方法二:簡易自訂啟動腳本

$ vi mysqldauto
#!/bin/sh
# Version: 0.1
/opt/mysql/bin/mysqld_safe --user=mysql &  # 修改為實際 mysqld_safe 路徑
$ chmod +x mysqldauto
$ mv mysqldauto /etc/rc.d/init.d/
$ ln -s /etc/rc.d/init.d/mysqldauto /etc/rc.d/rc3.d/S99mysqld

建立符號連結至 rc3.d/S99mysqld (S 開頭表啟動,99 表順序),Runlevel 3 進入時自動執行。

方法三:完整服務管理腳本 (支援 start/stop/restart)

#!/bin/sh
# Created by teddylu at 2012-12-12
port=3306
mysql_user="root"
mysql_pwd=""
CmdPath="/usr/local/mysql/bin"

function_start_mysql() {
  printf "Starting MySQL...\n"
  /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &
}
function_stop_mysql() {
  printf "Stoping MySQL...\n"
  ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown
}
function_restart_mysql() {
  printf "Restarting MySQL...\n"
  function_stop_mysql
  sleep 2
  function_start_mysql
}
case $1 in
  start) function_start_mysql ;;
  stop) function_stop_mysql ;;
  restart) function_restart_mysql ;;
  *) printf "Usage: /data/${port}/mysql {start|stop|restart}\n" ;;
esac

將腳本存為 /etc/init.d/mysql,chkconfig --add mysql 即可納入服務管理,支援 service mysql start/stop/restart 標準操作。

Copyright © Windows教程網 All Rights Reserved