当前位置: 操作系统/服务器>linux
shell使用mysqld_multi自动做多实例从库脚本
来源: 互联网 发布时间:2014-10-16
本文导语: 代码如下:# cat doallslave.sh#!/bin/sh#code by scpman#mail:scpman@live.com#blog:http://www.scpman.com#name:"doallslave.sh"helpinfo(){echo -e 'tthelp 查看帮助信息'echo -e 'tt自动做allslave从库脚本使用帮助'echo -e 'ttslavename 只能是xxx_xxx这种命令例如:zq2013_test1'echo...
代码如下:
# cat doallslave.sh
#!/bin/sh
#code by scpman
#mail:scpman@live.com
#blog:http://www.scpman.com
#name:"doallslave.sh"
helpinfo()
{
echo -e 'tthelp 查看帮助信息'
echo -e 'tt自动做allslave从库脚本使用帮助'
echo -e 'ttslavename 只能是xxx_xxx这种命令例如:zq2013_test1'
echo -e 'tt添加一个新从库 三个参数addslave slavename ip mysql.tar-name rootpasswd'
echo -e 'tt例子: addslave slavename 10.0.88.88 mysql.xxx.tar passwd '
echo -e 'tt删除一个从库 三个参数 delslave slavename '
echo -e 'tt例子: 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