mha shell脚本搭建
/**********************************搭建*******************************************/
linux版本:CentOS Linux release 7.8.2003 (Core)
mysql版本:Ver 8.0.18 for el7 on x86_64 (MySQL Community Server - GPL)
192.168.17.170 linuxsinglemysql 中控机
192.168.17.177 mysql03 从2
192.168.17.175 mysql01 主
192.168.17.176 mysql02 从1
192.168.17.169 mysqlmanager mha管理机
一、搭建过程
1、配置ssh互信
2、中控机/shell/installmanyhost下文件
hostnameimport.sh hostnameimport.txt hostnameip.txt installmanymysql.sh mha_conf.cnf my.cnf mysqld mysqlinstall.sh mysql.local mysqlmasterslave.sh
3、中控机执行
sh installmanymysql.sh
4、mysql02,mysql03执行
sh mysqlmasterslave.sh 192.168.17.175 192.168.17.176
sh mysqlmasterslave.sh 192.168.17.175 192.168.17.177
/********************************脚本内容*********************************************/
1、cat hostnameimport.sh
#!/bash/shell
#cat /shell/installmanyhost/hostnameimport.txt|awk ‘{print $1" "$2}‘|while read hostip hostname
#cat /shell/installmanyhost/hostnameimport.txt|grep -v ^$ |awk ‘{print $1, $2}‘|while read hostip hostname
cat /shell/installmanyhost/hostnameimport.txt |awk ‘{print $1, $2}‘|while read hostip hostname
do
if [ "$hostip" == "" ]; then
continue
else
hostcount=`cat /etc/hosts|grep $hostip|wc -l`
if [ $hostcount -gt 0 ] ;then
echo exitshost
else
echo $hostip $hostname >> /etc/hosts
fi
fi
done
2、cat installmanymysql.sh
#!/bin/bash
echo ‘ ‘
echo ‘ ‘
echo ************开始执行**************
set -e
cat /shell/installmanyhost/hostnameip.txt|awk ‘NR>0 {print $2}‘|while read hostname
do
echo *****$hostname*******
yes|scp /soft/mysql-8.0.18-el7-x86_64.tar.gz $hostname:/soft 1>/dev/null 2>/dev/null
if [ $? != 0 ]; then
echo $hostname拷贝mysql失败!
exit
else
echo $hostname拷贝mysql成功!
fi
yes|scp /soft/mha4mysql-node-0.58.tar.gz $hostname:/soft 1>/dev/null 2>/dev/null
if [ $? != 0 ]; then
echo $hostname拷贝mha4mysql-node失败!
exit
else
echo $hostname拷贝mha4mysql-node成功!
fi
yes|scp /soft/mha4mysql-manager-0.58.tar.gz $hostname:/soft 1>/dev/null 2>/dev/null
if [ $? != 0 ]; then
echo $hostname拷贝mha4mysql-manager失败!
exit
else
echo $hostname拷贝mha4mysql-manager成功!
fi
yes|scp /shell/installmanyhost/my.cnf $hostname:/soft 1>/dev/null 2>/dev/null
if [ $? != 0 ]; then
echo $hostname拷贝my.cnf失败!
exit
else
echo $hostname拷贝my.cnf成功!
fi
yes|scp /shell/installmanyhost/mha_conf.cnf $hostname:/soft 1>/dev/null 2>/dev/null
if [ $? != 0 ]; then
echo $hostname拷贝mha_conf.cnf失败!
exit
else
echo $hostname拷贝mha_conf.cnf成功!
fi
yes|scp /shell/installmanyhost/hostnameimport.txt $hostname:/soft 1>/dev/null 2>/dev/null
if [ $? != 0 ]; then
echo $hostname拷贝hostnameimport失败!
exit
else
echo $hostname拷贝hostnameimport成功!
fi
yes|scp /shell/installmanyhost/mysqld $hostname:/soft 1>/dev/null 2>/dev/null
if [ $? != 0 ]; then
echo $hostname拷贝mysqld失败!
exit
else
echo $hostname拷贝mysqld成功!
fi
yes|scp /shell/installmanyhost/mysql.local $hostname:/usr/bin 1>/dev/null 2>/dev/null
if [ $? != 0 ]; then
echo $hostname拷贝mysql.local失败!
exit
else
echo $hostname拷贝mysql.local成功!
fi
ssh -tt $hostname < /shell/installmanyhost/mysqlinstall.sh 1>/dev/null 2>/dev/null
if [ $? != 0 ]; then
echo $hostname安装mysql失败!
exit
else
echo $hostname安装mysql成功!
fi
done
echo ************执行结束***************
echo "bye"
echo ‘ ‘
echo ‘ ‘
3、cat mysqlinstall.sh
#!/bin/bash
cd /soft
#cat /shell/installmanyhost/hostnameimport.txt|awk ‘{print $1" "$2}‘|while read hostip hostname
#cat /shell/installmanyhost/hostnameimport.txt|grep -v ^$ |awk ‘{print $1, $2}‘|while read hostip hostname
cat /soft/hostnameimport.txt |awk ‘{print $1, $2}‘|while read hostip hostname
do
if [ "$hostip" == "" ]; then
continue
else
hostcount=`cat /etc/hosts|grep $hostip|wc -l`
if [ $hostcount -eq 1 ] ;then
echo exitshost
else
echo $hostip $hostname >> /etc/hosts
fi
fi
done
hostnamecheck=`hostname`
if [ "$hostnamecheck" != ‘mysqlmanager‘ ]; then
tar -zxvf mysql-8.0.18-el7-x86_64.tar.gz
rpm -e --nodeps mariadb-libs-5.5.65-1.el7.x86_64
service mysqld stop
sleep 10
rm -rf /usr/local/mysql 1>/dev/null 2>/dev/null
rm -rf /data 1>/dev/null 2>/dev/null
mkdir /usr/local/mysql -p
mv mysql-8.0.18-el7-x86_64/* /usr/local/mysql
mkdir -p /data/mysql/mysql
mkdir -p /data/mysql/logs
mysql_user=`cat /etc/passwd |grep mysql|wc -l`
if [ 1 -eq $mysql_user ];then
echo "Mysql用户存在"
else
echo -e "Mysql用户不存在,开始添加mysql用户"
groupadd mysql && useradd -g mysql mysql
echo "mysql" | passwd --stdin mysql
echo -e "添加mysql用户成功!"
fi
chown -R mysql:mysql /data/mysql
chown -R mysql:mysql /usr/local/mysql/
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/mysql/
yes|cp /soft/mysqld /etc/init.d/mysqld
yes|cp /soft/my.cnf /etc/my.cnf
chmod 775 /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
touch /data/mysql/logs/mysql-error.log
chown mysql:mysql /data/mysql/logs/mysql-error.log
#newserverid=ip a | grep ens33| awk ‘/inet/{ print $2}‘|awk -F ‘/‘ ‘{print $1}‘|awk -F ‘.‘ ‘{print $NF}‘
newserverid=$(ip addr | awk ‘/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}‘ |awk -F ‘.‘ ‘{print $4}‘)
sed -i ‘s/server_id=313306/server_id=‘"$newserverid"‘3306/‘ /etc/my.cnf
service mysqld start
mysql_pass=`cat /data/mysql/logs/mysql-error.log|grep password | awk ‘/root@localhost:/{print $13}‘`
mysql -uroot -p$mysql_pass --connect-expired-password <> /etc/profile
source /etc/profile
else
echo mysqlenvivarexits
fi
else
echo 不需要安装mysql!
fi
cd /soft
yum install -y perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-CPAN perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes
tar -zxf mha4mysql-node-0.58.tar.gz
cd mha4mysql-node-0.58/
perl Makefile.PL
make && make install
if [ "$hostnamecheck" == ‘mysqlmanager‘ ]; then
cd /soft
tar -zxf mha4mysql-manager-0.58.tar.gz
cd mha4mysql-manager-0.58/
yes|perl Makefile.PL
yes|make && make install
mkdir -pv /mha/scripts
chown -R mysql:mysql /mha
chown -R mysql:mysql /mha/scripts/
cd /soft
yes|cp mha_conf.cnf /mha
mkdir -pv /usr/local/mha
chown -R mysql:mysql /usr/local/mha
fi
exit
4、cat mysqlmasterslave.sh
#!/bin/bash
set -e
master=$1
slave=$2
if [ "$1" == "" ];then
echo -e "请在脚本后输入主服务器ip地址"
exit 1
fi
if [ "$2" == "" ];then
echo -e "请在脚本后输入从服务器ip地址"
exit 1
fi
bin_log=`/usr/local/mysql/bin/mysql -uroot -pXbl@mysql817 -h $master -e "show master status;" | awk ‘/mysql-bin/{print $1}‘`
posID=`/usr/local/mysql/bin/mysql -uroot -pXbl@mysql817 -h $master -e "show master status;" | awk ‘/mysql-bin/{print $2}‘`
if [ "$1" == "" ];then
echo -e "请在脚本后输入主服务器ip地址"
exit 1
fi
if [ "$2" == "" ];then
echo -e "请在脚本后输入从服务器ip地址"
exit 1
fi
######同步bin_log######
/usr/local/mysql/bin/mysql -uroot -pXbl@mysql817 --connect-expired-password <