MySQL:【初级】管理脚本
本文的下述内容参考:君三思《涂抹MySQL》。
本文为读书笔记。
在这一章中,君三思给出了对MySQL管理的一些很有意思的尝试,主要用于数据库的启停,以及对MySQL终端的登录。
具体如下:
如果使用了君三思的脚本,你的MySQL体验是如下的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
[root@rhel71 scripts]# ps -ef | grep mysql root 95886 86442 0 20:46 pts/0 00:00:00 grep --color=auto mysql [root@rhel71 scripts]# [root@rhel71 scripts]# mysqld_startup.sh MySQL ---> STARTUP @ rhel71:3306 [root@rhel71 scripts]# 151203 20:47:03 mysqld_safe Logging to '/var/log/mysql/error.log'. 151203 20:47:03 mysqld_safe Starting mysqld daemon with databases from /mydata/mysql/data [root@rhel71 scripts]# [root@rhel71 scripts]# ps -ef | grep mysql root 95889 1 0 20:47 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --user=mysql --datadir=/mydata/mysql/data --log-error=/var/log/mysql/error.log mysql 96493 95889 0 20:47 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/mydata/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysql/error.log --open-files-limit=65535 --pid-file=/var/log/mysql/mysql.pid --socket=/var/run/mysql/mysql.sock --port=3306 root 96519 86442 0 20:47 pts/0 00:00:00 grep --color=auto mysql [root@rhel71 scripts]# [root@rhel71 scripts]# netstat -tupln | grep 3306 tcp6 0 0 :::3306 :::* LISTEN 96493/mysqld [root@rhel71 scripts]# [root@rhel71 scripts]# mysqlplus.sh Login MySQL Service @ rhel71:3306 mysql: [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 2 Server version: 5.7.7-rc-log Source distribution Copyright (c) 2000, 2015, 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. mysql> exit Bye [root@rhel71 scripts]# [root@rhel71 scripts]# mysqld_shutdown.sh MySQL ---> SHUTDOWN @ rhel71:3306 mysqladmin: [Warning] Using a password on the command line interface can be insecure. 151203 20:48:19 mysqld_safe mysqld from pid file /var/log/mysql/mysql.pid ended [root@rhel71 scripts]# |
如上,启停数据库和登录MySQL终端的操作被脚本化之后,效率与体验都有明显提升。
以下是以上几个脚本的具体实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
[root@rhel71 scripts]# pwd /mydata/mysql/scripts [root@rhel71 scripts]# ll total 16 -rwxr-xr-x 1 root root 355 Dec 3 20:37 mysqld_shutdown.sh -rwxr-xr-x 1 root root 237 Dec 3 20:37 mysqld_startup.sh -rwxr-xr-x 1 root root 137 Dec 3 20:46 mysql_env.ini -rwxr-xr-x 1 root root 228 Dec 3 20:43 mysqlplus.sh [root@rhel71 scripts]# [root@rhel71 scripts]# cat mysql_env.ini # set env for mysql MYSQL_USER=root MYSQL_PASS='Oracle123' # Check parameter if [ $# -ne 1 ] then HOST_PORT=3306 else HOST_PORT=$1 fi [root@rhel71 scripts]# [root@rhel71 scripts]# cat mysqld_startup.sh #!/bin/sh # adamhuan@2015 12 03 source /mydata/mysql/scripts/mysql_env.ini echo "MySQL ---> STARTUP @ "`hostname`":"${HOST_PORT} # do Startup mysqld_safe --user=mysql --datadir=/mydata/mysql/data --log-error=/var/log/mysql/error.log & [root@rhel71 scripts]# [root@rhel71 scripts]# cat mysqld_shutdown.sh #!/bin/sh # adamhuan@2015 12 03 source /mydata/mysql/scripts/mysql_env.ini echo "MySQL ---> SHUTDOWN @ "`hostname`":"${HOST_PORT} # do Startup # mysqld_safe --user=mysql --datadir=/mydata/mysql/data --log-error=/var/log/mysql/error.log # do Shutdown /usr/local/mysql/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PASS} -S /var/run/mysql/mysql.sock shutdown [root@rhel71 scripts]# [root@rhel71 scripts]# cat mysqlplus.sh #!/bin/sh # adamhuan created @ 20151203 source /mydata/mysql/scripts/mysql_env.ini echo "Login MySQL Service @ "`hostname`":"${HOST_PORT} /usr/local/mysql/bin/mysql -u${MYSQL_USER} -p${MYSQL_PASS} -S /var/run/mysql/mysql.sock [root@rhel71 scripts]# [root@rhel71 scripts]# |
————————————————
Done。