Shell uses Mysqld_multi to automate multiple instances from library script _linux Shell

Source: Internet
Author: User

Copy Code code as follows:

# 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 View Help information '
Echo-e ' \t\t automatically do allslave from library script use Help '
Echo-e ' \t\tslavename can only be xxx_xxx such commands as: Zq2013_test1 '
Echo-e ' \t\t add a new from the library three parameters addslave slavename IP mysql.tar-name '
Echo-e ' \t\t example: Addslave slavename 10.0.88.88 Mysql.xxx.tar '
Echo-e ' \t\t deletes one from the library three parameters Delslave Slavename '
Echo-e ' \t\t example: Delslave slavename '
}
#如果用户没有输入参数 (or enter the wrong argument), or if you enter a parameter of help, you will be displayed and quit.
If [-Z "" "-o" $ "=" help "]
Then
Helpinfo
Exit
Fi
Mulu_create ()
{
If [-D $]
Then
echo "Is have";
Else
Mkdir-p $;
echo "Create $ ok!"
Fi
}
CHECK_MYCNF ()
{
Temp1= ' tail-n 1/etc/my.cnf |grep ' server_id ';
If [-Z "$temp 1"]
Then
Sed-i "" ' $d '/etc/my.cnf
Check_mycnf

Else
echo "/ETC/MY.CNF conforms to the specification";
Fi
}
Fuc_addslave ()
{
If [-N "$"-a-n "$"-a-n "$"-a-n "$"]
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 is correct
Check_mycnf
Echo begins to create the packet storage directory! "
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 "Data placement complete!" Start build configuration File "
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 $} '
Old_db_name= "' Cat $tempconf | grep "Pid-file" | Awk-f '/' {print $} ' |awk-f ' _ ' {print $ _ ' $} ' '
Old_server_id= "' Cat $tempconf | grep "server_id" |awk ' {print $} '
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 and DBList have been modified to complete!" "
/usr/local/bin/mysqld_multi Start $new _mysqld
Sleep 1
Bin= ' Cat/usr/db_backups/${slavename}_mysql_backup/pos_mysql|awk ' {print $} ';
Pos= ' Cat/usr/db_backups/${slavename}_mysql_backup/pos_mysql|awk ' {print $} ';
#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 "Server: ${masterip} from library complete!" Please refresh and view the PHP from the library monitor
}
Fuc_delslave ()
{
If [-N ' $]
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 exists! "
Else
echo "/usr/db_backups/${nousedbname}_mysql_backup This directory does not exist, please confirm your input is correct, script exit!" "
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 "has been deleted, please refresh from library monitor"
}
If ["$" = "Addslave"]
Then
Echo ' Add New from library '
Sleep 3
Fuc_addslave $ $ $ $
elif ["$" = "Delslave"]
Then
Echo ' Delete '
Fuc_delslave $
Else
ECHO is illegal for this parameter
Helpinfo
Exit
Fi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.