Shell implementation, using mysqld_multi Automatic Multi-instance slave database script implementation

Source: Internet
Author: User

Shell implementation, using mysqld_multi to automatically perform multiple instance slave database scripts # cat doallslave. sh001 #! /Bin/sh002 # code by scpman003 # mail: scpman@live.com004 # blog: http://www.scpman. com005 # name: "doallslave. sh "006 helpinfo () 007 {008 echo-e '\ t \ thelp view help information '009echo-e' \ t auto-Execute allslave slave database script use help '010echo-e' \ t \ tslavename the command can only be xxx_xxx, for example: zq2013_test1 '011echo-e' \ t add a new slave database with three parameters addslave slavename ip mysql.tar-name rootpasswd '012echo-e' \ t: addslave slavename 10.0.88.88 mysql.xxx.tar passwd '013echo-e '\ T \ t delslave slavename '014echo-e' \ t example: delslave slavename '015} 016 # If you do not enter a parameter (or enter an incorrect parameter) or the input parameter is "help", the system displays the help information and exits. 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 compliant with specifications "; 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 # Check whether my. cnf is correct 058check_mycnf059echo" to create a data packet storage directory! "060 sleep 1061mulu_create"/usr/doslave/$ {slavename}/"Examples"/usr/db_backups/$ {slavename} _ mysql_backup/"063mulu_create"/usr/mysql_backup/$ {slavename} _ mysql_backup/"064mulu_create"/usr/logs/$ {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/*/us R/db_backups/$ {slavename} _ mysql_backup/; 068 chown-R mysql: mysql/usr/db_backups/$ {slavename} _ mysql_backup/; 069 echo "data placement completed! Start generating the configuration file "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 ############ 074 tempconf = "/usr/doslave/$ {slavename}/mycnf. temp "075 tail-n 6/etc/my. cnf> $ tempconf; 076 sleep 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 2093 echo "">/etc/my. cnf; 094cat $ tempconf>/etc/my. cnf095echo" Mysqld $ {new_mysqld} ">/usr/mysql_backup/dblist; 096 ####################################### ######## 097 echo "my. cnf and dblist have been modified! "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 $ bin1_echo $ 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; 109 reset slave; 110 change master to MASTER_HOST = "$ masterip", MASTER_USER = 'slaveuser', MASTER_PASSWORD = 'slave123213 ', MASTER_LOG_FILE = "$ BIN ", MASTER_LOG_POS = $ POS; 111 start slave; effecfff113rm-rf/usr/doslave/$ {slavename }/; 114clear115/usr/local/bin/mysql-uroot-p $ passwd -- socket =/tmp/mysql _ $ {new_db_name }. sock-e "show slave status \ G" 116 echo "server: Slave database of $ {masterip} is complete! Please refresh and view php slave database monitoring "117} 118fuc_delslave () 119 {120if [-n" $1 "] 121then122 nousedbname = $1123else124 echo" slavename is null! Shell exit now "125 helpinfo126 exit1_fi128if [-d/usr/db_backups/$ {nousedbname} _ mysql_backup] 129then130 echo"/usr/db_backups/$ {nousedbname} _ mysql_backup! "Cancelse132 echo"/usr/db_backups/$ {nousedbname} _ mysql_backup this directory does not exist. Please confirm that your input is correct and the script exits! "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 //; "'" 137 echo $ 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 "deleted, please refresh slave database monitoring "145} 146if [" $1 "=" addslave "] 147then148echo 'add new slave database '149sleep 3150fuc_addslave $2 $3 $4 $5151 elif [" $1 "=" delslave "] 152then153echo 'delete '154fuc _ delslave $ 2155else156echo $1 this parameter is invalid 157helpinfo158exit159fi

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.