shell實現,利用mysqld_multi自動做多實例從庫腳本實現
# cat doallslave.sh
001
#!/bin/sh
002
#code by scpman
003
#mail:
[email protected]
004
#blog:http://www.scpman.com
005
#name:"doallslave.sh"
006
helpinfo()
007
{
008
echo -e '\t\thelp 查看幫助信息'
009
echo -e '\t\t自動做allslave從庫腳本使用幫助'
010
echo -e '\t\tslavename 只能是xxx_xxx這種命令例如:zq2013_test1'
011
echo -e '\t\t添加一個新從庫 三個參數addslave slavename ip mysql.tar-name rootpasswd'
012
echo -e '\t\t例子: addslave slavename 10.0.88.88 mysql.xxx.tar passwd '
013
echo -e '\t\t刪除一個從庫 三個參數 delslave slavename '
014
echo -e '\t\t例子: delslave slavename'
015
}
016
#如果用戶沒有輸入參數(或者輸入錯誤的參數),或者輸入的參數為help,則顯示幫助信息,並退出。
017
if [ -z "$1" -o "$1" = "help" ]
018
then
019
helpinfo
020
exit
021
fi
022
mulu_create()
023
{
024
if [ -d $1 ]
025
then
026
echo "$1 is have";
027
else
028
mkdir -p $1;
029
echo "create $1 ok!"
030
fi
031
}
032
check_mycnf()
033
{
034
temp1=`tail -n 1 /etc/my.cnf |grep "server_id"`;
035
if [ -z "$temp1" ]
036
then
037
sed -i "" '$d' /etc/my.cnf
038
check_mycnf
039
040
else
041
echo "/etc/my.cnf符合規范";
042
fi
043
}
044
fuc_addslave()
045
{
046
if [ -n "$1" -a -n "$2" -a -n "$3" -a -n "$4" ]
047
then
048
slavename=$1
049
masterip=$2
050
bao_path=$3
051
passwd=$4
052
else
053
echo "1 2 3 4 is null!shell exit now"
054
helpinfo
055
exit
056
fi
057
#檢查my.cnf是否正確
058
check_mycnf
059
echo "開始創建數據包存放目錄!"
060
sleep 1
061
mulu_create "/usr/doslave/${slavename}/"
062
mulu_create "/usr/db_backups/${slavename}_mysql_backup/"
063
mulu_create "/usr/mysql_backup/${slavename}_mysql_backup/"
064
mulu_create "/usr/mysql_backup_temp/${slavename}_mysql_backup/"
065
tar -zxvf $bao_path -C /usr/doslave/${slavename}/;
066
rm -rf /usr/db_backups/${slavename}_mysql_backup/*;
067
mv /usr/doslave/${slavename}/usr/dlm_db/mysql/* /usr/db_backups/${slavename}_mysql_backup/;
068
chown -R mysql:mysql /usr/db_backups/${slavename}_mysql_backup/;
069
echo "數據放置完成!開始生成配置文件"
070
mulu_create "/usr/doslave/confbak"
071
cp /etc/my.cnf /usr/doslave/confbak/my.cnf_`date +%Y%m%d%H%M%S`;
072
cp /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 2
077
old_mysqld="`cat $tempconf |grep "mysqld" | sed -e "s/\[mysqld//;s/\]//;"`"
078
old_port="`cat $tempconf | grep 'port'|awk '{print $3}'`"
079
old_db_name="`cat $tempconf | grep "pid-file" | awk -F'/' '{print $4 }'|awk -F'_' '{print $1 "_" $2}'`"
080
old_server_id="`cat $tempconf | grep "server_id"|awk '{print $3}'`"
081
new_mysqld="`expr ${old_mysqld} + 1`"
082
new_port="`expr ${old_port} + 1`"
083
new_db_name="$slavename"
084
new_server_id=`echo $masterip |awk -F. '{print $3$4"9"}'`
085
old_mysqld="`echo $old_mysqld`"
086
old_port="`echo $old_port`"
087
old_db_name="`echo $old_db_name`"
088
old_server_id="`echo $old_server_id`"
089
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;
090
#cat $tempconf
091
#echo '--->'
092
sleep 2
093
echo "" >> /etc/my.cnf;
094
cat $tempconf >> /etc/my.cnf
095
echo "mysqld${new_mysqld}" >> /usr/mysql_backup/dblist;
096
###############################################
097
echo "my.cnf 和dblist已經修改完成!"
098
/usr/local/bin/mysqld_multi start $new_mysqld
099
sleep 1
100
BIN=`cat /usr/db_backups/${slavename}_mysql_backup/pos_mysql|awk '{print $1}'`;
101
POS=`cat /usr/db_backups/${slavename}_mysql_backup/pos_mysql|awk '{print $2}'`;
102
#echo $passwd
103
echo $BIN
104
echo $POS
105
echo /usr/local/bin/mysql -uroot -p$passwd --socket=/tmp/mysql_${new_db_name}.sock
106
sleep 2
107
/usr/local/bin/mysql -uroot -p$passwd --socket=/tmp/mysql_${new_db_name}.sock <<FFF
108
stop 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;
112
FFF
113
rm -rf /usr/doslave/${slavename}/;
114
clear
115
/usr/local/bin/mysql -uroot -p$passwd --socket=/tmp/mysql_${new_db_name}.sock -e "show slave status\G"
116
echo "服務器: ${masterip} 的從庫完成!請刷新並查看php從庫監控"
117
}
118
fuc_delslave()
119
{
120
if [ -n "$1" ]
121
then
122
nousedbname=$1
123
else
124
echo "slavename is null!shell exit now"
125
helpinfo
126
exit
127
fi
128
if [ -d /usr/db_backups/${nousedbname}_mysql_backup ]
129
then
130
echo "/usr/db_backups/${nousedbname}_mysql_backup 存在!"
131
else
132
echo "/usr/db_backups/${nousedbname}_mysql_backup這個目錄不存在,請確認您的輸入是否正確,腳本退出!"
133
exit;
134
fi
135
del_id="`grep -B1 "$nousedbname" /etc/my.cnf|grep mysqld|sed -e "s/\[//;s/\]//";`"
136
stop_id="`echo $del_id|sed -e "s/mysqld//;"`"
137
echo $del_id
138
echo $stop_id
139
/usr/local/bin/mysqld_multi stop $stop_id
140
rm -rf /usr/db_backups/${nousedbname}_mysql_backup;
141
sed -i '' "/$del_id/d" /usr/mysql_backup/dblist
142
sed -i '' "/$del_id/d" /etc/my.cnf
143
sed -i '' "/$nousedbname/d" /etc/my.cnf
144
echo "已經刪除,請刷新從庫監控"
145
}
146
if [ "$1" = "addslave" ]
147
then
148
echo '添加新從庫'
149
sleep 3
150
fuc_addslave $2 $3 $4 $5
151
elif [ "$1" = "delslave" ]
152
then
153
echo '刪除'
154
fuc_delslave $2
155
else
156
echo $1 此參數是非法的
157
helpinfo
158
exit
159
fi