shell實現,利用mysqld_multi自動做多執行個體從庫指令碼實現 # cat doallslave.sh001#!/bin/sh002#code by scpman003#mail:scpman@live.com004#blog:http://www.scpman.com005#name:"doallslave.sh"006helpinfo()007{008echo -e '\t\thelp 查看協助資訊'009echo -e '\t\t自動做allslave從庫指令碼使用協助'010echo -e '\t\tslavename 只能是xxx_xxx這種命令例如:zq2013_test1'011echo -e '\t\t添加一個新從庫 三個參數addslave slavename ip mysql.tar-name rootpasswd'012echo -e '\t\t例子: addslave slavename 10.0.88.88 mysql.xxx.tar passwd '013echo -e '\t\t刪除一個從庫 三個參數 delslave slavename '014echo -e '\t\t例子: delslave slavename'015}016#如果使用者沒有輸入參數(或者輸入錯誤的參數),或者輸入的參數為help,則顯示協助資訊,並退出。017if [ -z "$1" -o "$1" = "help" ]018then019helpinfo020exit021fi022mulu_create()023{024 if [ -d $1 ]025 then026 echo "$1 is have";027 else028 mkdir -p $1;029 echo "create $1 ok!"030 fi031}032check_mycnf()033{034 temp1=`tail -n 1 /etc/my.cnf |grep "server_id"`;035 if [ -z "$temp1" ]036 then037 sed -i "" '$d' /etc/my.cnf038 check_mycnf039 040 else 041 echo "/etc/my.cnf符合規範";042 fi043}044fuc_addslave()045{046if [ -n "$1" -a -n "$2" -a -n "$3" -a -n "$4" ]047then048 slavename=$1049 masterip=$2050 bao_path=$3051 passwd=$4052else053 echo "1 2 3 4 is null!shell exit now"054 helpinfo055 exit056fi057#檢查my.cnf是否正確058check_mycnf059echo "開始建立資料包存放目錄!"060sleep 1061mulu_create "/usr/doslave/${slavename}/"062mulu_create "/usr/db_backups/${slavename}_mysql_backup/"063mulu_create "/usr/mysql_backup/${slavename}_mysql_backup/"064mulu_create "/usr/mysql_backup_temp/${slavename}_mysql_backup/"065tar -zxvf $bao_path -C /usr/doslave/${slavename}/;066rm -rf /usr/db_backups/${slavename}_mysql_backup/*;067mv /usr/doslave/${slavename}/usr/dlm_db/mysql/* /usr/db_backups/${slavename}_mysql_backup/;068chown -R mysql:mysql /usr/db_backups/${slavename}_mysql_backup/;069echo "資料放置完成!開始組建組態檔案"070mulu_create "/usr/doslave/confbak"071cp /etc/my.cnf /usr/doslave/confbak/my.cnf_`date +%Y%m%d%H%M%S`;072cp /usr/mysql_backup/dblist /usr/doslave/confbak/dblist_`date +%Y%m%d%H%M%S`;073#######################my.cnf#############074tempconf="/usr/doslave/${slavename}/mycnf.temp"075tail -n 6 /etc/my.cnf > $tempconf;076sleep 2077old_mysqld="`cat $tempconf |grep "mysqld" | sed -e "s/\[mysqld//;s/\]//;"`"078old_port="`cat $tempconf | grep 'port'|awk '{print $3}'`"079old_db_name="`cat $tempconf | grep "pid-file" | awk -F'/' '{print $4 }'|awk -F'_' '{print $1 "_" $2}'`"080old_server_id="`cat $tempconf | grep "server_id"|awk '{print $3}'`"081new_mysqld="`expr ${old_mysqld} + 1`"082new_port="`expr ${old_port} + 1`"083new_db_name="$slavename"084new_server_id=`echo $masterip |awk -F. '{print $3$4"9"}'`085old_mysqld="`echo $old_mysqld`"086old_port="`echo $old_port`"087old_db_name="`echo $old_db_name`"088old_server_id="`echo $old_server_id`"089sed -i "" "s/$old_mysqld/$new_mysqld/g;s/$old_port/$new_port/g;s/$old_db_name/$new_db_name/g;s/$old_server_id/$new_server_id/g;"$tempconf;090#cat $tempconf091#echo '--->'092sleep 2093echo "" >> /etc/my.cnf;094cat $tempconf >> /etc/my.cnf095echo "mysqld${new_mysqld}" >> /usr/mysql_backup/dblist;096###############################################097echo "my.cnf 和dblist已經修改完成!"098/usr/local/bin/mysqld_multi start $new_mysqld099sleep 1100BIN=`cat /usr/db_backups/${slavename}_mysql_backup/pos_mysql|awk '{print $1}'`;101POS=`cat /usr/db_backups/${slavename}_mysql_backup/pos_mysql|awk '{print $2}'`;102#echo $passwd103echo $BIN104echo $POS105echo /usr/local/bin/mysql -uroot -p$passwd --socket=/tmp/mysql_${new_db_name}.sock106sleep 2107/usr/local/bin/mysql -uroot -p$passwd --socket=/tmp/mysql_${new_db_name}.sock <<FFF108stop slave;109reset slave;110CHANGE MASTER TO MASTER_HOST="$masterip", MASTER_USER='slaveuser',MASTER_PASSWORD='slave123213', MASTER_LOG_FILE="$BIN",MASTER_LOG_POS=$POS;111start slave;112FFF113rm -rf /usr/doslave/${slavename}/;114clear115/usr/local/bin/mysql -uroot -p$passwd --socket=/tmp/mysql_${new_db_name}.sock -e "show slave status\G"116echo "伺服器: ${masterip} 的從庫完成!請重新整理並查看php從庫監控"117}118fuc_delslave()119{120if [ -n "$1" ]121then122 nousedbname=$1123else124 echo "slavename is null!shell exit now"125 helpinfo126 exit127fi128if [ -d /usr/db_backups/${nousedbname}_mysql_backup ]129then130 echo "/usr/db_backups/${nousedbname}_mysql_backup 存在!"131else132 echo "/usr/db_backups/${nousedbname}_mysql_backup這個目錄不存在,請確認您的輸入是否正確,指令碼退出!"133 exit;134fi135del_id="`grep -B1 "$nousedbname" /etc/my.cnf|grep mysqld|sed -e "s/\[//;s/\]//";`"136stop_id="`echo $del_id|sed -e "s/mysqld//;"`"137echo $del_id138echo $stop_id139/usr/local/bin/mysqld_multi stop $stop_id140rm -rf /usr/db_backups/${nousedbname}_mysql_backup;141sed -i '' "/$del_id/d" /usr/mysql_backup/dblist142sed -i '' "/$del_id/d" /etc/my.cnf143sed -i '' "/$nousedbname/d" /etc/my.cnf144echo "已經刪除,請重新整理從庫監控"145}146if [ "$1" = "addslave" ]147then148echo '添加新從庫'149sleep 3150fuc_addslave $2 $3 $4 $5151elif [ "$1" = "delslave" ]152then153echo '刪除'154fuc_delslave $2155else156echo $1 此參數是非法的157helpinfo158exit159fi