之前写过文章介绍MySQL多实例,今天把脚本放出来,只写了start和stop的标本一些检查、重新启动懒的写了,随便用用吧。希望对大家有帮助!
0 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
#!/bin/bash # Author:Byrd # Version:0.1 # Site:note.t4x.org # Contact:root#t4x.org . /etc/init.d/functions MSL_USER="root" MSL_PASS="123456" BIN_DIR="/usr/local/mysql/bin" CNF_FILE="/usr/local/mysql/data/3306/my.cnf" PID_FILE="/usr/local/mysql/data/3306/data/`hostname`.pid" MSL_SOCK="/usr/local/mysql/data/3306/mysql.sock" CHK_PROC=`netstat -tunlp|grep mysql|wc -l` BAK_DIR="/back/mysql" POS_FILE="/back/mysql/Database/$(date +%F)/file-position-$(date +%F).log" MSL_ADMN="${BIN_DIR}/mysql -u${MSL_USER} -p${MSL_PASS} -S ${MSL_SOCK}" MSL_DUMP="${BIN_DIR}/mysqldump -u${MSL_USER} -p${MSL_PASS} -S ${MSL_SOCK}" MSL_STAT="${BIN_DIR}/mysqld_safe --defaults-file=${CNF_FILE}" PATH="$mysql_Bin:/sbin:/usr/sbin:/bin:/usr/bin" export PATH start() { # pidfile and sock must existence if [ -s ${PID_FILE} -a -S ${MSL_SOCK} ]; then action "MySQL is running" /bin/true # sock does not exist elif [ ! -S ${MSL_SOCK} ];then if [ -s ${PID_FILE} ];then PID=`cat ${PID_FILE}` if (kill -0 $PID 2>/dev/null);then #Check for the existence of the process.pid exists or not # echo "Shutting down MySQL" kill ${PID} sleep 3 ${MSL_STAT} >/dev/null 2>&1 & RETURN_VALUE=$? [ "${RETURN_VALUE}" -eq 0 ] && action "Restarting MySQL" /bin/true || action "mysql err ${RETURN_VALUE}" /bin/false fi elif [ "${CHK_PROC}" -eq 0 ]; then sleep 4 ${MSL_STAT} >/dev/null 2>&1 & RETURN_VALUE=$? [ "${RETURN_VALUE}" -eq 0 ] && action "Starting MySQL" /bin/true || action "mysql err ${RETURN_VALUE}" /bin/false else #kill `ps aux|grep basedir|grep -v grep|awk '{print $2}'` sleep 5 ${MSL_STAT} >/dev/null 2>&1 & RETURN_VALUE=$? [ "${RETURN_VALUE}" -eq 0 ] && action "Starting MySQL" /bin/true || action "mysql err ${RETURN_VALUE}" /bin/false fi # pidfile does not exist or others else kill `ps aux|grep basedir|grep -v grep|awk '{print $2}'` sleep 6 ${MSL_STAT} >/dev/null 2>&1 & RETURN_VALUE=$? [ "${RETURN_VALUE}" -eq 0 ] && action "Starting MySQL" /bin/true || action "mysql err ${RETURN_VALUE}" /bin/false fi } stop() { if [ -s "${PID_FILE}" ];then PID=`cat ${PID_FILE}` if (kill -0 ${PID} 2>/dev/null);then kill $PID sleep 3 action "Shutting down MySQL" /bin/true fi elif [ -S ${MSL_SOCK} ]; then kill `ps aux|grep basedir|grep -v grep|awk '{print $2}'` sleep 4 PID=`cat ${PID_FILE}` kill $PID action "Shutting down MySQL" /bin/true else action "MySQL server process #$PID is not running!" /bin/true rm -f $pid_File fi } case "$1" in start) start && exit 0 $1 ;; stop) stop || exit 2 ;; restart) stop start ;; *) echo $"Usage: $0 {start|stop|restart}" exit 2 esac exit $? |
备份数据库表:
0 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 |
#!/bin/bash # Author:Byrd # Version:0.1 # Site:note.t4x.org # Contact:root#t4x.org . /etc/init.d/functions MSL_USER="root" MSL_PASS="123456" SEE_MAIL="root#t4x.org" BIN_DIR="/usr/local/mysql/bin" CNF_FILE="/usr/local/mysql/data/3306/my.cnf" PID_FILE="/usr/local/mysql/data/3306/data/`hostname`.pid" MSL_SOCK="/usr/local/mysql/data/3306/mysql.sock" CHK_PROC=`netstat -tunlp|grep mysql|wc -l` BAK_DIR="/back/mysql" POS_FILE="/back/mysql/Database/$(date +%F)/file-position-$(date +%F).log" MSL_ADMN="${BIN_DIR}/mysql -u${MSL_USER} -p${MSL_PASS} -S ${MSL_SOCK}" MSL_DUMP="${BIN_DIR}/mysqldump -u${MSL_USER} -p${MSL_PASS} -S ${MSL_SOCK}" MSL_STAT="${BIN_DIR}/mysqld_safe --defaults-file=${CNF_FILE}" PATH="$mysql_Bin:/sbin:/usr/sbin:/bin:/usr/bin" export PATH if [ ! -s ${PID_FILE} -a ! -S ${MSL_SOCK} ]; then ${MSL_STAT} >/dev/null 2>&1 & sleep 4 #echo "Will Start Mysql" | mail -s "Mysql Not Running" ${SEE_MAIL} fi for database in `${MSL_ADMN} -e "show databases;"|sed '1,2d'|grep -v "performance_schema\|mysql"` do [ -d ${BAK_DIR}/Tables/$(date +%F)/${database} ] || mkdir ${BAK_DIR}/Tables/$(date +%F)/${database} -p [ -d ${BAK_DIR}/Tables/$(date +%F)/All ] || mkdir ${BAK_DIR}/Tables/$(date +%F)/ALL -p for tables in `${MSL_ADMN} -e "show tables from $database"|sed '1d'` do ${MSL_DUMP} ${database} ${tables}|gzip >${BAK_DIR}/Tables/$(date +%F)/${database}/${tables}_${database}_$(date +%H-%M-%S).sql.gz done cd ${BAK_DIR}/Tables/$(date +%F)/${database} tar -zcf ${BAK_DIR}/Tables/$(date +%F)/ALL/$(date +%F-%H-%M-%S)-${database}.tar.gz ./ done |
个人版本:(其实就是合并了一下)
申明:本文版权属于Byrd's Blog(t4x.org),转载请注明出处。商业使用请联系我们. SourceByrd's Weblog-https://note.t4x.org/code/mysql-multi-startup-script/
申明:除非注明Byrd's Blog内容均为原创,未经许可禁止转载!详情请阅读版权申明!