知识屋:更实用的电脑技术知识网站
所在位置:首页 > 操作系统 > linux

shell实现,利用mysqld_multi自动做多实例从库脚本实现

发布时间:2014-09-05 13:47:38作者:知识屋

shell实现,利用mysqld_multi自动做多实例从库脚本实现
 
# cat doallslave.sh
001
#!/bin/sh
002
#code by scpman
003
#mail:scpman@live.com
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
 
(免责声明:文章内容如涉及作品内容、版权和其它问题,请及时与我们联系,我们将在第一时间删除内容,文章内容仅供参考)
收藏
  • 人气文章
  • 最新文章
  • 下载排行榜
  • 热门排行榜