#!/bin/sh
#multi-instance
#usage sh tl2.sh $1 $2
hostip=`/sbin/ifconfig eth0|grep -i "inet addr"|awk '{print $2}'|awk -F"." '{print $3$4}'`
echo $hostip
cnf=/etc/my.cnf
#配置my.cnf
if [ -f "$cnf" -a ! -f "${cnf}.bak" ];then
cp $cnf $cnf.bak
fi
multi=`cat /etc/my.cnf|grep -i -w "mysqld_multi"`
if [ "${multi}" = "" ];then
sed -i '4a\[mysqld_multi]\nmysqld = /usr/local/mysql/bin/mysqld_safe\n' $cnf
fi
masterip=$1
lastip=`echo "$1"|awk -F'.' '{print $3$4}'`
sed -i '6a\[mysqld'${lastip}']\nsocket = /tmp/mysql.sock'${lastip}'\nport = '$2'\npid\-file = '${hostip}'.pid'${lastip}'\ndatadir = /usr/local/mysql/var'${lastip}'\nrelay\-log = '${hostip}'\-relay\-bin\-'${lastip}'\n' $cnf
#生成数据文件
if [ -d /app/mysql/var ];then
/app/mysql/bin/mysql_install_db --datadir=/app/mysql/var${lastip} --user=mysql
chown -R mysql.mysql /app/mysql/var*
fi
#解压主库备份+mysql.tar
cd /app/mysql/var${lastip}
gs=`ls -l /app/dbbackup|grep -i "${lastip}"|awk -F'.' '{print $2}'`
case "$gs" in
zip)
unzip /app/dbbackup/${lastip}*
;;
rar)
unrar x /app/dbbackup/${lastip}*
;;
tar)
tar xvf /app/dbbackup/${lastip}*
;;
tgz)
tar zxvf /app/dbbackup/${lastip}*
;;
esac
sleep 5
tar xvf /root/mysql.tar
sleep 1
chown -R mysql:mysql .
#配置主从
mysql_bin=/app/mysql/bin
mysql_sock=/tmp/mysql.sock
#${mysql_bin}/mysqld_multi stop ${lastip}
#${mysql_bin}/mysqladmin -S ${mysql_sock}${lastip} shudown -pmys
ps -ef|grep 'port=$2'|grep -v grep|awk '{print $2,$3}'|xargs -n1 kill -9
${mysql_bin}/mysqld_multi start ${lastip}
sleep 1
${mysql_bin}/mysql -uroot -S ${mysql_sock}${lastip} -pmysql_ -e "slave stop;CHANGE MASTER TO MASTER_HOST='$1',MASTER_USER='repl',MASTER_PASSWORD='repl_SGH1H',MASTER_LOG_FILE='log-bin.000001',MASTER_LOG_POS=0;slave start;"
sleep 2
${mysql_bin}/mysql -uroot -S ${mysql_sock}${lastip} -pmysql_ -e "show slave status\G;"