歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux管理 >> Linux問題解決 >> linux中mysql開機自動啟動3種方法

linux中mysql開機自動啟動3種方法

日期:2017/3/6 10:13:55   编辑:Linux問題解決
開機啟動主要是把啟動命令加入到linux的啟動服務中去就可以實現了,具體給各位整理兩段代碼。

第一種手工簡單加入即可實現開機自動啟動mysql,後面是沒事做寫的一段shell代碼。

mysql設為linux服務

代碼如下

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


下面是我們的一個簡單的啟動腳本v0.1 mysqldauto

代碼如下 $vi mysqldauto
#!/bin/sh
# Version: 0.1 by [email protected]
/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

這樣我們就把創建的mysqldauto腳本放到了/etc/rc.d/rc3.d/下面(注意這裡使用了link的方式),mysqld可以自動啟動了。


啟動腳本

代碼如下

#!/bin/sh
################################################
#Created by teddylu at 2012-12-12
#Used for mysql start,stop,restart
################################################

#init
port=3306
mysql_user="root"
mysql_pwd=""
CmdPath="/usr/local/mysql/bin"

#startup function
function_start_mysql()
{
printf "Starting MySQL...\n"
/bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &
}

#stop function
function_stop_mysql()
{
printf "Stoping MySQL...\n"
${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown
}

#restart function
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

Copyright © Linux教程網 All Rights Reserved