linux:CentOS Linux release 7.8.2003 (Core)
mysql:Ver 8.0.18 for el7 on x86_64 (MySQL Community Server - GPL)
前提条件:中控机和安装mysql服务器之间都相互配置ssh,防火墙关闭
一、搭建过程
1、中控机上传文件
cd /shell/installmanyhost
hostnameip.txt installmanymysql.sh my.cnf mysqld mysqlinstall.sh mysql.local
2、安装
sh installmanymysql.sh
3、从节点执行
sh mysqlmasterslave.sh 192.168.17.175 192.168.17.176
二、脚本内容
1、cat hostnameip.txt
192.168.17.175 mysql01
192.168.17.176 mysql02
192.168.17.177 mysql03
2、cat mysql.local
/usr/local/mysql/bin/mysql -uroot -pXbl@mysql817
3、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 /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/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 ' '
4、cat mysqlinstall.sh
#!/bin/bash
cd /soft
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用户"
useradd 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
echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile
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密码以及授权slave########
mysql -uroot -p$mysql_pass --connect-expired-password <