mysql主从复制及读写分离脚本-亲测可用

集群架构:
集群一主两从一代理() 。所谓的主从复制是指mysql从服务器从主服务器复制数据,保持同步 。所谓读写分离即主负责写,从负责读,此架构大大提升了数据库的性能 。
主从节点部署脚本:

mysql主从复制及读写分离脚本-亲测可用

文章插图
#!/bin/bash#function:mysql主从复制与读写分离#author:tommypeng20220726#####root判断#####if["$USER"!= "root"]thenecho "错误:非root用户,权限不足!"exit0fi###############防火墙及SElinux############systemctl stop firewalld && systemctl disable firewalld&& echo "防火墙已经关闭"sed -i 's/SELINUX=.*/SELINUX=disabled/g'/etc/selinux/config&& echo "关闭selinux"###########清理旧版本###########rpm -qa | grep mariadb >/root/888.txtrpm -qa | grep mysql>>/root/888.txtPLIST=$(cat/root/888.txt)forPKGNAMEin$PLISTdorpm -e --nodeps$PKGNAME&&echo"老旧包已经清理完毕" donerm -rf/root/888.txt&&echo "临时文件已删除"sleep 8##############网络测试##############ping-c3www.baidu.comif[ $? = 0 ]thenecho "外网通讯良好!"elseecho"丫的你在逗我吗?网都没有安装个毛线!"exit1fi###########安装##############yum -y install mariadb mariadb-server #安装mariadb#echo "服务器即将重启,重启后请重新运行脚本"#sleep 10#reboot############配置文件#############functionmaster(){if [[ "$1" -eq "1" ]];thencat >> /root/mysql.txt << EOFserver-id=1log-bin=mysql-binbinlog_format=MIXEDlog-slave-updates=trueEOFsed-i '/\[mysqld\]/r/root/mysql.txt'/etc/my.cnf#############删除临时文件##############rm -rfmysql.txt&&echo "临时文件已经删除"##########启动数据库################systemctl start mariadbmysql-e "grant all on *.* to tom@'%' identified by '123';"mysql-e "grant replication slave on *.* to tom@'%' identified by '123';"####在主服务器建立账号tom并授权mysql-e"grant all on *.* to test@'%' identified by '123';"mysql-e "flush privileges;" ##mysql -u root-e "create database wg character set utf8 collate utf8_bin;"##创建测试库 echo"请记住master_log_file及Position的值,部署slave时需要用到"&&mysql -u root-e "show master status;" exit 0fi}functionslave(){if [[ "$1" -eq "2" ]];thenread -p"输入从服务器的服务ID(不能为1,同时不能与其他从服务器相同) "ppocat >> /root/mysql.txt << EOFserver-id=$pporelay-log=mysql-relayrelay-log-index=slave-relay-bin.indexEOFsed-i '/\[mysqld\]/r/root/mysql.txt'/etc/my.cnf#############删除临时文件##############rm -rfmysql.txt&&echo "临时文件已经删除"##########启动数据库################systemctl start mariadb systemctl restart mariadb mysql-e"grant all on *.* to test@'%' identified by '123';"mysql-e "flush privileges;" mysql -e "stop slave;" read -p"请输入主服务器IP地址:"ppiread -p"请输入主服务器Position编号:"posread -p"请输入主服务器master_log_file编号:"mlfmysql-e"CHANGE master to master_host='"$ppi"',master_user='tom',master_password='123',master_log_file='"$mlf"',master_log_pos=$pos;"##echo "change master to master_host='"$ppi"',master_user='tom',master_password='123',master_log_file='"$mlf"',master_log_pos=$pos;"|mysqlmysql-e "start slave;" exit 1fi}PS3="选择改mysql服务器的角色,1为master或者2为slave:"select i inmasterslaveexitdocase $i inmaster)master 1;;slave)slave 2;;exit)echo "The system exit"exitesacdone
脚本运行中选择1部署为主,选择2部署为从,选择3退出 。脚本运行过程中请根据脚本提示输入相关值 。
部署脚本:
运行脚本前请上传安装包到/root/下
#!/bin/bash#function:mysql代理(阿米巴变形虫)#author:tommypeng20220727#####root判断#####if["$USER"!= "root"]thenecho "错误:非root用户,权限不足!"exit0fi#####安装文件判断######MOUNT_File="/root/amoeba-mysql-binary-2.2.0.tar.gz"if[ ! -e$MOUNT_File ]thenecho "安装文件不存在,请上传安装文件到/root/,上传完成再重新运行该脚本"exit 1fiMOUNT_File1="/root/jdk-8u20-linux-x64.rpm"if[ ! -e$MOUNT_File1 ]thenecho "安装文件不存在,请上传安装文件到/root/,上传完成再重新运行该脚本"exit 1fi###############防火墙及SElinux############systemctl stop firewalld && systemctl disable firewalld&& echo "防火墙已经关闭"sed -i 's/SELINUX=.*/SELINUX=disabled/g'/etc/selinux/config&& echo "关闭selinux"sleep 10##############jdk#############cd /root&&rpm -ivh jdk-8u20-linux-x64.rpmcat >> /etc/profile