您好,登錄后才能下訂單哦!
本文主要給大家介紹通過簡單腳本實現MYSQL的起停,文章內容都是筆者用心摘選和編輯的,具有一定的針對性,對大家的參考意義還是比較大的,下面跟筆者一起了解下通過簡單腳本實現MYSQL的起停吧。
對于多instance安裝的MYSQL來說,起停的過程相對復雜,可以定義一些簡單的腳本來簡化日常的管理。
1# 環境變量腳本
[mysql@mysql01 scripts]$ cat mysql_env.ini
#set env
MYSQL_USER=root
MYSQL_PASS='password123' #明文保存的密碼,生產庫中當然不可以這樣使用。
COPYDIR='/opt/mysql'
BASEDIR='/data/mysqldata'
#check parameter
if [ $# -eq 0 ]
then
MYSQL_PORT=3306
else
MYSQL_PORT=$1
fi
[mysql@mysql01 scripts]$
2# MYSQL啟動腳本
[mysql@mysql01 scripts]$ cat mysql_startup.sh
#!/bin/bash
source /data/mysqldata/scripts/mysql_env.ini
echo "Startup MySQL Service: localhost_"${MYSQL_PORT}
${COPYDIR}/bin/mysqld_safe --defaults-file=${BASEDIR}/${MYSQL_PORT}/my.cnf &
[mysql@mysql01 scripts]$
3# MYSQL停止腳本
[mysql@mysql01 scripts]$ cat mysql_shutdown.sh
#!/bin/bash
source /data/mysqldata/scripts/mysql_env.ini
echo "Shutdown MySQL Service: localhost_"${MYSQL_PORT}
${COPYDIR}/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PASS} -S ${BASEDIR}/${MYSQL_PORT}/mysql.sock shutdown
[mysql@mysql01 scripts]$
4# 快速登陸
[mysql@mysql01 scripts]$ cat mysqlcmd.sh
#!/bin/bash
source /data/mysqldata/scripts/mysql_env.ini
echo "Startup MySQL CMD: localhost_"${MYSQL_PORT}
${COPYDIR}/bin/mysql -u${MYSQL_USER} -p${MYSQL_PASS} -S ${BASEDIR}/${MYSQL_PORT}/mysql.sock
[mysql@mysql01 scripts]$
5# 建立軟連接
[mysql@mysql01 scripts]$ pwd
/data/mysqldata/scripts
[mysql@mysql01 scripts]$ ln -s mysqlcmd.sh mysqlcmd
[mysql@mysql01 scripts]$ ln -s mysql_startup.sh mysqlstart
[mysql@mysql01 scripts]$ ln -s mysql_shutdown.sh mysqlstop
[mysql@mysql01 scripts]$ ls -ralt
total 32
drwxr-xr-x. 5 mysql mysql 44 Aug 27 03:33 ..
-rwxrw-r--. 1 mysql mysql 189 Aug 28 20:14 mysql_startup.sh
-rwxrw-r--. 1 mysql mysql 220 Aug 28 20:19 mysql_shutdown.sh
-rwxrw-r--. 1 mysql mysql 202 Aug 28 20:23 mysqlcmd.sh
-rw-r--r--. 1 mysql mysql 12288 Aug 28 20:26 .mysql_shutdown.sh.swp
-rw-rw----. 1 mysql mysql 176 Aug 28 20:26 mysql_env.ini
lrwxrwxrwx. 1 mysql mysql 11 Aug 28 20:30 mysqlcmd -> mysqlcmd.sh
lrwxrwxrwx. 1 mysql mysql 16 Aug 28 20:30 mysqlstart -> mysql_startup.sh
lrwxrwxrwx. 1 mysql mysql 17 Aug 28 20:31 mysqlstop -> mysql_shutdown.sh
6# 使用過程
6.1# 確定當前運行中的MYSQL實例,以端口劃分
[mysql@mysql01 scripts]$ netstat -lnt | grep 330
tcp6 0 0 :::3306 :::* LISTEN
可以看到當前3306正在運行,停止3306
6.2# 停止MYSQL
[mysql@mysql01 scripts]$ mysqlstop 3306
Shutdown MySQL Service: localhost_3306
Warning: Using a password on the command line interface can be insecure.
[mysql@mysql01 scripts]$
6.3# 啟動MYSQL
[mysql@mysql01 scripts]$ mysqlstart 3306
Startup MySQL Service: localhost_3306
[mysql@mysql01 scripts]$ 180828 21:33:49 mysqld_safe Logging to '/data/mysqldata/3306/data/../mysql-error.log'.
180828 21:33:49 mysqld_safe Starting mysqld daemon with databases from /data/mysqldata/3306/data
[mysql@mysql01 scripts]$
6.4# 使用MYSQL
[mysql@mysql01 scripts]$ mysqlcmd 3306 Startup MySQL CMD: localhost_3306 Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.31-log Source distribution Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
(root@localhost)[(none)]>
看完以上關于通過簡單腳本實現MYSQL的起停,很多讀者朋友肯定多少有一定的了解,如需獲取更多的行業知識信息 ,可以持續關注我們的行業資訊欄目的。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。