For MySQL with multiple instance installations, the process of starting and stopping is relatively complex, and some simple scripts can be defined to simplify routine management.
# environment Variable Script
[[email protected] scripts]$ cat mysql_env.ini#set envMYSQL_USER=rootMYSQL_PASS=‘password123‘ #明文保存的密码,生产库中当然不可以这样使用。COPYDIR=‘/opt/mysql‘BASEDIR=‘/data/mysqldata‘#check parameterif [ $# -eq 0 ]then MYSQL_PORT=3306else MYSQL_PORT=$1fi[[email protected] scripts]$
* MySQL startup script
[[email protected] scripts]$ cat mysql_startup.sh#!/bin/bashsource /data/mysqldata/scripts/mysql_env.iniecho "Startup MySQL Service: localhost_"${MYSQL_PORT}${COPYDIR}/bin/mysqld_safe --defaults-file=${BASEDIR}/${MYSQL_PORT}/my.cnf &[[email protected] scripts]$
3# MySQL Stop script
[[email protected] scripts]$ cat mysql_shutdown.sh#!/bin/bashsource /data/mysqldata/scripts/mysql_env.iniecho "Shutdown MySQL Service: localhost_"${MYSQL_PORT}${COPYDIR}/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PASS} -S ${BASEDIR}/${MYSQL_PORT}/mysql.sock shutdown[[email protected] scripts]$
4# Fast Landing
[[email protected] scripts]$ cat mysqlcmd.sh#!/bin/bashsource /data/mysqldata/scripts/mysql_env.iniecho "Startup MySQL CMD: localhost_"${MYSQL_PORT}${COPYDIR}/bin/mysql -u${MYSQL_USER} -p${MYSQL_PASS} -S ${BASEDIR}/${MYSQL_PORT}/mysql.sock[[email protected] scripts]$
* To establish a soft connection
[[email protected] scripts]$ pwd/data/mysqldata/scripts[[email protected] scripts]$ ln -s mysqlcmd.sh mysqlcmd[[email protected] scripts]$ ln -s mysql_startup.sh mysqlstart [[email protected] scripts]$ ln -s mysql_shutdown.sh mysqlstop[[email protected] scripts]$ ls -ralttotal 32drwxr-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.inilrwxrwxrwx. 1 mysql mysql 11 Aug 28 20:30 mysqlcmd -> mysqlcmd.shlrwxrwxrwx. 1 mysql mysql 16 Aug 28 20:30 mysqlstart -> mysql_startup.shlrwxrwxrwx. 1 mysql mysql 17 Aug 28 20:31 mysqlstop -> mysql_shutdown.sh
6# Use Process
6.1# determine the current running MySQL instance, with Port partitioning
[[email protected] scripts]$ netstat -lnt | grep 330tcp6 0 0 :::3306 :::* LISTEN
You can see that the current 3306 is running, stop 3306
6.2# stop MySQL
[[email protected] scripts]$ mysqlstop 3306Shutdown MySQL Service: localhost_3306Warning: Using a password on the command line interface can be insecure.[[email protected] scripts]$
6.3# start MySQL
[[email protected] scripts]$ mysqlstart 3306Startup MySQL Service: localhost_3306[[email protected] 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[[email protected] scripts]$
6.4# using MySQL
[[email protected] scripts]$ mysqlcmd 3306Startup MySQL CMD: localhost_3306Warning: 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 1Server version: 5.6.31-log Source distributionCopyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.([email protected])[(none)]>
MySQL simple script start-stop