当前位置:网站首页>MySQL master-slave replication and read-write separation script - pro test available
MySQL master-slave replication and read-write separation script - pro test available
2022-07-31 00:23:00 【IT rat】
集群架构:
The cluster has one master, two slaves and one agent(amoeba).The so-called master-slave replication refers tomysqlThe slave server replicates data from the master server,保持同步.The so-called read-write separation means that the master is responsible for writing,从负责读,This architecture greatly improves the performance of the database.
Master-slave node deployment script:
#!/bin/bash
#function:mysql主从复制与读写分离
#author:tommypeng 20220726
#####root判断#####
if
[ "$USER" != "root" ]
then
echo "错误:非root用户,权限不足!"
exit 0
fi
###############防火墙及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.txt
rpm -qa | grep mysql >> /root/888.txt
PLIST=$(cat /root/888.txt)
for PKGNAME in $PLIST
do
rpm -e --nodeps $PKGNAME && echo "老旧包已经清理完毕"
done
rm -rf /root/888.txt && echo "临时文件已删除"
sleep 8
##############网络测试##############
ping -c 3 www.baidu.com
if
[ $? = 0 ]
then
echo "外网通讯良好!"
else
echo "丫的你在逗我吗?网都没有安装个毛线!"
exit 1
fi
###########安装##############
yum -y install mariadb mariadb-server #安装mariadb
#echo "The server is about to restart,Please rerun the script after rebooting"
#sleep 10
#reboot
############配置文件#############
function master()
{
if [[ "$1" -eq "1" ]];then
cat >> /root/mysql.txt << EOF
server-id=1
log-bin=mysql-bin
binlog_format=MIXED
log-slave-updates=true
EOF
sed -i '/\[mysqld\]/r /root/mysql.txt' /etc/my.cnf
#############删除临时文件##############
rm -rf mysql.txt && echo "临时文件已经删除"
##########启动数据库################
systemctl start mariadb
mysql -e "grant all on *.* to [email protected]'%' identified by '123';"
mysql -e "grant replication slave on *.* to [email protected]'%' identified by '123';" ####Create an account on the main servertom并授权
mysql -e "grant all on *.* to [email protected]'%' 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 0
fi
}
function slave()
{
if [[ "$1" -eq "2" ]];then
read -p "Enter the service from the serverID(不能为1,At the same time cannot be the same as other slave servers) " ppo
cat >> /root/mysql.txt << EOF
server-id=$ppo
relay-log=mysql-relay
relay-log-index=slave-relay-bin.index
EOF
sed -i '/\[mysqld\]/r /root/mysql.txt' /etc/my.cnf
#############删除临时文件##############
rm -rf mysql.txt && echo "临时文件已经删除"
##########启动数据库################
systemctl start mariadb
systemctl restart mariadb
mysql -e "grant all on *.* to [email protected]'%' identified by '123';"
mysql -e "flush privileges;"
mysql -e "stop slave;"
read -p "Please enter a primary serverIP地址: " ppi
read -p "Please enter a primary serverPosition编号: " pos
read -p "Please enter a primary servermaster_log_file编号: " mlf
mysql -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;" |mysql
mysql -e "start slave;"
exit 1
fi
}
PS3="选择改mysql服务器的角色,1为master或者2为slave: "
select i in master slave exit
do
case $i in
master)
master 1
;;
slave)
slave 2
;;
exit)
echo "The system exit"
exit
esac
done
脚本运行中选择1Deployment is the main,选择2Deploy as from,选择3退出.When the script is running, please input relevant values according to the script prompts.
amoeba部署脚本:
Please upload the installation package to /root/下
#!/bin/bash
#function:mysql代理(Amoeba amoeba)
#author:tommypeng 20220727
#####root判断#####
if
[ "$USER" != "root" ]
then
echo "错误:非root用户,权限不足!"
exit 0
fi
#####安装文件判断######
MOUNT_File="/root/amoeba-mysql-binary-2.2.0.tar.gz"
if
[ ! -e $MOUNT_File ]
then
echo "安装文件不存在,请上传安装文件到/root/,上传完成再重新运行该脚本"
exit 1
fi
MOUNT_File1="/root/jdk-8u20-linux-x64.rpm"
if
[ ! -e $MOUNT_File1 ]
then
echo "安装文件不存在,请上传安装文件到/root/,上传完成再重新运行该脚本"
exit 1
fi
###############防火墙及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.rpm
cat >> /etc/profile << EOF
export JAVA_HOME=/usr/java/jdk1.8.0_20/
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/lib:$JAVA_HOME/jre/bin:$PATH:$HOME/bin
export AMOEBA_HOME=/usr/local/amoeba
export PATH=$PATH:$AMOEBA_HOME/bin
EOF
source /etc/profile #重新加载
source /etc/profile #重新加载
#############安装amoeba#############
mkdir /usr/local/amoeba && chmod -R 755 /usr/local/amoeba #创建目录amoebaand change permissions
tar xzf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/ #解包到/usr/local/amoeba目录中
### /usr/local/amoeba/bin/
sed -i "58s/128/256/" /usr/local/amoeba/bin/amoeba
cd /usr/local/amoeba/bin/ && ./amoeba #########Enter the execution directory to install
###################
cp /usr/local/amoeba/conf/amoeba.xml /usr/local/amoeba/conf/amoeba.xml.bak
sed -i "30s/root/amoeba/" /usr/local/amoeba/conf/amoeba.xml
sed -i "32s/"password"></property>/"password">123456</property>/" /usr/local/amoeba/conf/amoeba.xml
sed -i "115s/server1/master/" /usr/local/amoeba/conf/amoeba.xml
sed -i "119s/readPool/slave/" /usr/local/amoeba/conf/amoeba.xml
mv /usr/local/amoeba/conf/dbServers.xml /usr/local/amoeba/conf/dbServers.xml.bak
read -p "请输入mysql主服务器的IP: " masterIP
read -p "请输入mysql从服务器1的IP: " slave1IP
read -p "请输入mysql从服务器2的IP: " slave2IP
cat >> /usr/local/amoeba/conf/dbServers.xml << EOF
<?xml version="1.0" encoding="gbk"?>
<!DOCTYPE amoeba:dbServers SYSTEM "dbserver.dtd">
<amoeba:dbServers xmlns:amoeba="http://amoeba.meidusa.com/">
<!--
Each dbServer needs to be configured into a Pool,
If you need to configure multiple dbServer with load balancing that can be simplified by the following configuration:
add attribute with name virtual = "true" in dbServer, but the configuration does not allow the element with name factoryConfig
such as 'multiPool' dbServer
-->
<dbServer name="abstractServer" abstractive="true">
<factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
<property name="manager">${defaultManager}</property>
<property name="sendBufferSize">64</property>
<property name="receiveBufferSize">128</property>
<!-- mysql port -->
<property name="port">3306</property>
<!-- mysql schema -->
<!-- <property name="schema">test</property> -->
<!-- mysql user -->
<property name="user">test</property>
mysql password
<property name="password">123456</property>
</factoryConfig>
<poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
<property name="maxActive">500</property>
<property name="maxIdle">500</property>
<property name="minIdle">10</property>
<property name="minEvictableIdleTimeMillis">600000</property>
<property name="timeBetweenEvictionRunsMillis">600000</property>
<property name="testOnBorrow">true</property>
<property name="testOnReturn">true</property>
<property name="testWhileIdle">true</property>
</poolConfig>
</dbServer>
<dbServer name="master" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">$masterIP</property>
</factoryConfig>
</dbServer>
<dbServer name="slave1" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">$slave1IP</property>
</factoryConfig>
</dbServer>
</dbServer>
<dbServer name="slave1" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">$slave2IP</property>
</factoryConfig>
</dbServer>
<dbServer name="slaves" virtual="true">
<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
<!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
<property name="loadbalance">1</property>
<!-- Separated by commas,such as: server1,server2,server1 -->
<property name="poolNames">slave1, slave2</property>
</poolConfig>
</dbServer>
EOF
/usr/local/amoeba/bin/amoeba start& ###后台启动
yum -y install mariadb mariadb-server $$ echo "mariadb安装成功"
echo "请通过命令mysql -utest -p123 -h $masterIP|$slave1IP|$slave2IP 访问测试"
The relevant server needs to be entered during the script runningIP.
Please download the installation package required by the proxy script from the author's personal website:
边栏推荐
- In-depth understanding of the auto-increment operator from two error-prone written test questions
- 从笔试包装类型的11个常见判断是否相等的例子理解:包装类型、自动装箱与拆箱的原理、装箱拆箱的发生时机、包装类型的常量池技术
- Word文件损坏如何修复
- Point Cloud Scene Reconstruction with Depth Estimation
- 正则表达式密码策略与正则回溯机制绕过
- IOT cross-platform component design scheme
- Kotlin协程:协程上下文与上下文元素
- background对float的子元素无效
- 【愚公系列】2022年07月 Go教学课程 016-运算符之逻辑运算符和其他运算符
- A complete guide to avoiding pitfalls for the time-date type "yyyy-MM-dd HHmmss" in ES
猜你喜欢
【Multithreading】
what is jira
Summary of the stock problem of state machine dynamic programming
Strict Mode for Databases
@requestmapping注解的作用及用法
Error ER_NOT_SUPPORTED_AUTH_MODE Client does not support authentication protocol requested by serv
加密传输过程
C语言力扣第48题之旋转图像。辅助数组
xss靶机训练【实现弹窗即成功】
2D Transform Module && Media Queries
随机推荐
SereTOD2022 Track2代码剖析-面向半监督和强化学习的任务型对话系统挑战赛
pytorch bilinear interpolation
Asser uses ant sword to log in
software development design process
Add text watermark to PHP image
46.
Summary of the stock problem of state machine dynamic programming
47. 【Pointers and Arrays】
Shell编程之条件语句
Point Cloud Scene Reconstruction with Depth Estimation
Kotlin协程:协程上下文与上下文元素
[In-depth and easy-to-follow FPGA learning 14----------Test case design 2]
Error in go mode tidy go warning “all” matched no packages
XSS相关知识
Steven Giesel recently published a 5-part series documenting his first experience building an application with the Uno Platform.
【Yugong Series】July 2022 Go Teaching Course 017-IF of Branch Structure
Encapsulate and obtain system user information, roles and permission control
h264和h265解码上的区别
encrypted transmission process
MySQL数据库的truncate与delete区别