您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本文主要给大家介绍通过简单脚本实现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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。