1. 程式人生 > >Shell指令碼操作MySQL服務

Shell指令碼操作MySQL服務

1、編寫Shell指令碼

#! /bin/sh
case $1 in
    start)
        /usr/local/mysql/bin/mysqld_safe --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql &
        echo "MySQL service has stated."
        ;;
    stop)
        killall mysqld;
        echo "MySQL service has stoped."
        ;;
    restart)
        killall mysqld
        /usr/local/mysql/bin/mysqld_safe --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql &
        echo "MySQL service has restarted."
        ;;
    *)
        echo "No such argument."
        exit 1
        ;;
esac

2、建立軟連結,將指令碼作為服務執行
shell> sudo ln -s ~/workspace/shell/mysql/mysqld /etc/init.d/mysqld

3、啟動、停止、重啟MySQL服務

shell> sudo service mysqld start
shell> sudo service mysqld stop
shell> sudo service mysqld restart