shell使用mysqld_multi自動做多執行個體從庫指令碼_linux shell

來源:互聯網
上載者:User

複製代碼 代碼如下:

# cat doallslave.sh
#!/bin/sh
#code by scpman
#mail:scpman@live.com
#blog:http://www.scpman.com
#name:"doallslave.sh"
helpinfo()
{
echo -e '\t\thelp 查看協助資訊'
echo -e '\t\t自動做allslave從庫指令碼使用協助'
echo -e '\t\tslavename 只能是xxx_xxx這種命令例如:zq2013_test1'
echo -e '\t\t添加一個新從庫 三個參數addslave    slavename ip         mysql.tar-name rootpasswd'
echo -e '\t\t例子:         addslave  slavename 10.0.88.88  mysql.xxx.tar passwd  '
echo -e '\t\t刪除一個從庫  三個參數 delslave    slavename '
echo -e '\t\t例子:         delslave slavename'
}
#如果使用者沒有輸入參數(或者輸入錯誤的參數),或者輸入的參數為help,則顯示協助資訊,並退出。
if [ -z "$1"  -o "$1" = "help" ]
then
helpinfo
exit
fi
mulu_create()
{
    if [ -d $1 ]
    then
        echo "$1 is have";
    else
        mkdir -p $1;
        echo "create $1 ok!"
    fi
}
check_mycnf()
{
    temp1=`tail -n 1 /etc/my.cnf |grep "server_id"`;
    if [ -z "$temp1" ]
    then
        sed -i ""  '$d' /etc/my.cnf
        check_mycnf

    else   
        echo "/etc/my.cnf符合規範";
    fi
}
fuc_addslave()
{
if [ -n "$1" -a -n "$2" -a -n "$3" -a -n "$4" ]
then
    slavename=$1
    masterip=$2
    bao_path=$3
    passwd=$4
else
    echo "1 2 3 4 is null!shell exit now"
    helpinfo
    exit
fi
#檢查my.cnf是否正確
check_mycnf
echo "開始建立資料包存放目錄!"
sleep 1
mulu_create "/usr/doslave/${slavename}/"
mulu_create "/usr/db_backups/${slavename}_mysql_backup/"
mulu_create "/usr/mysql_backup/${slavename}_mysql_backup/"
mulu_create "/usr/mysql_backup_temp/${slavename}_mysql_backup/"
tar -zxvf $bao_path -C /usr/doslave/${slavename}/;
rm -rf /usr/db_backups/${slavename}_mysql_backup/*;
mv /usr/doslave/${slavename}/usr/dlm_db/mysql/*  /usr/db_backups/${slavename}_mysql_backup/;
chown -R mysql:mysql /usr/db_backups/${slavename}_mysql_backup/;
echo "資料放置完成!開始組建組態檔案"
mulu_create "/usr/doslave/confbak"
cp /etc/my.cnf /usr/doslave/confbak/my.cnf_`date +%Y%m%d%H%M%S`;
cp /usr/mysql_backup/dblist /usr/doslave/confbak/dblist_`date +%Y%m%d%H%M%S`;
#######################my.cnf#############
tempconf="/usr/doslave/${slavename}/mycnf.temp"
tail -n 6 /etc/my.cnf > $tempconf;
sleep 2
old_mysqld="`cat $tempconf |grep "mysqld" | sed -e "s/\[mysqld//;s/\]//;"`"
old_port="`cat $tempconf | grep 'port'|awk '{print $3}'`"
old_db_name="`cat $tempconf | grep "pid-file" | awk -F'/' '{print $4 }'|awk -F'_'  '{print $1 "_" $2}'`"
old_server_id="`cat $tempconf | grep "server_id"|awk '{print $3}'`"
new_mysqld="`expr ${old_mysqld} + 1`"
new_port="`expr ${old_port} + 1`"
new_db_name="$slavename"
new_server_id=`echo $masterip |awk -F. '{print $3$4"9"}'`
old_mysqld="`echo $old_mysqld`"
old_port="`echo $old_port`"
old_db_name="`echo $old_db_name`"
old_server_id="`echo $old_server_id`"
sed -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;
#cat $tempconf
#echo '--->'
sleep 2
echo "" >> /etc/my.cnf;
cat $tempconf >> /etc/my.cnf
echo "mysqld${new_mysqld}" >> /usr/mysql_backup/dblist;
###############################################
echo "my.cnf 和dblist已經修改完成!"
/usr/local/bin/mysqld_multi start $new_mysqld
sleep 1
BIN=`cat /usr/db_backups/${slavename}_mysql_backup/pos_mysql|awk '{print $1}'`;
POS=`cat /usr/db_backups/${slavename}_mysql_backup/pos_mysql|awk '{print $2}'`;
#echo $passwd
echo $BIN
echo $POS
echo /usr/local/bin/mysql -uroot -p$passwd --socket=/tmp/mysql_${new_db_name}.sock
sleep 2
/usr/local/bin/mysql -uroot -p$passwd --socket=/tmp/mysql_${new_db_name}.sock <<FFF
stop slave;
reset slave;
CHANGE MASTER TO MASTER_HOST="$masterip", MASTER_USER='slaveuser',MASTER_PASSWORD='slave123213', MASTER_LOG_FILE="$BIN",MASTER_LOG_POS=$POS;
start slave;
FFF
rm -rf /usr/doslave/${slavename}/;
clear
/usr/local/bin/mysql -uroot -p$passwd --socket=/tmp/mysql_${new_db_name}.sock -e "show slave status\G"
echo "伺服器: ${masterip} 的從庫完成!請重新整理並查看php從庫監控"
}
fuc_delslave()
{
if [ -n "$1" ]
then
    nousedbname=$1
else
    echo "slavename is null!shell exit now"
    helpinfo
    exit
fi
if [ -d /usr/db_backups/${nousedbname}_mysql_backup ]
then
    echo "/usr/db_backups/${nousedbname}_mysql_backup 存在!"
else
    echo "/usr/db_backups/${nousedbname}_mysql_backup這個目錄不存在,請確認您的輸入是否正確,指令碼退出!"
    exit;
fi
del_id="`grep -B1 "$nousedbname" /etc/my.cnf|grep mysqld|sed -e "s/\[//;s/\]//";`"
stop_id="`echo $del_id|sed -e "s/mysqld//;"`"
echo $del_id
echo $stop_id
/usr/local/bin/mysqld_multi stop $stop_id
rm -rf /usr/db_backups/${nousedbname}_mysql_backup;
sed -i '' "/$del_id/d" /usr/mysql_backup/dblist
sed -i '' "/$del_id/d" /etc/my.cnf
sed -i '' "/$nousedbname/d" /etc/my.cnf
echo "已經刪除,請重新整理從庫監控"
}
if [ "$1" = "addslave" ]
then
echo '添加新從庫'
sleep 3
fuc_addslave $2 $3 $4 $5
elif [ "$1" = "delslave" ]
then
echo '刪除'
fuc_delslave $2
else
echo $1 此參數是非法的
helpinfo
exit
fi

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.