当前位置:网站首页>【mysql】主从复制原理、搭建

【mysql】主从复制原理、搭建

2022-06-09 20:16:00 雪碧不要气


前言

本博客内容仅为记录博主思路,仅供参考,一切以自己实践结果为准。


一、工作原理

1.1 示意图

![在这里插入图片描述](https://img-blog.csdnimg.cn/52d959b6dadb47f8acc642509440bc29.png


1.2 文字解释

  • 主服务器开启二进制日志,从服务器开启中继日志
  • 主服务器日志发生更新后,从服务器通过I/O线程探测到更新并发送请求二进制事件
  • 主服务器通过dump线程将更新的二进制日志事件发送给从服务器
  • 从服务器将事件写入中继日志,通过SQL线程将日志读取为sql语句,重放执行语句,保持数据的统一性

二、搭建

2.1 架构

模拟环境IP地址所需工具root密码
主服务器192.168.13.10mysql-boost-5.7.20.tarabc123
从服务器192.168.13.20mysql-boost-5.7.20.tarabc123

2.2 主服务器

#!/bin/bash
function_deploy_master (){
    

systemctl disable firewalld
setenforce 0
sed -i "7c SELINUX=disabled" /etc/sysconfig/selinux

hostnamectl set-hostname Mysql1


sed -i "23c server-id = 1" /etc/my.cnf
sed -i "24i log_bin = master-bin" /etc/my.cnf
sed -i "25i log-slave-updates = true" /etc/my.cnf

systemctl restart mysqld

ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/

mysql -uroot -pabc123 -e"grant replication slave on *.* to 'myslave'@'192.168.13.%' identified by '123';"
mysql -uroot -pabc123 -e"grant all privileges on *.* to 'mha'@'192.168.13.%' identified by 'manager';"
mysql -uroot -pabc123 -e"grant all privileges on *.* to 'mha'@'Mysql1' identified by 'manager';"
mysql -uroot -pabc123 -e"grant all privileges on *.* to 'mha'@'Mysql2' identified by 'manager';"
mysql -uroot -pabc123 -e"grant all privileges on *.* to 'mha'@'Mysql3' identified by 'manager';"
mysql -uroot -pabc123 -e"flush privileges;"

mysql -u root -pabc123 -e "show master status;"|grep master-bin|awk '{print $1}' >/mnt/master_log_file
mysql -u root -pabc123 -e "show master status;"|grep master-bin|awk '{print $2}' >/mnt/master_position

/usr/bin/expect <<EOF
spawn scp /mnt/master_position /mnt/master_log_file 192.168.13.20:/mnt/
expect "yes/no " {
    send "yes\r"}
expect "password " {
    send "abc123\r"}
expect eof
EOF

#mysql -u root -pabc123 -e "create database test_db;" #开启后可检查主从复制是否开启
}

  • 主服务器的日志名为mysql-bin.000001,偏移量为915
    在这里插入图片描述

2.3 从服务器

function_deploy_slave1 (){
    

systemctl disable firewalld
setenforce 0
sed -i "7c SELINUX=disabled" /etc/sysconfig/selinux

hostnamectl set-hostname Mysql2

sed -i "23c server-id = 2" /etc/my.cnf
sed -i "24i log_bin = master-bin" /etc/my.cnf
sed -i "25i relay-log = relay-log-bin" /etc/my.cnf
sed -i "26i relay-log-index = slave-relay-bin.index" /etc/my.cnf

systemctl restart mysqld

ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/

mysql -uroot -pabc123 -e"grant replication slave on *.* to 'myslave'@'192.168.13.%' identified by '123';"
mysql -uroot -pabc123 -e"grant all privileges on *.* to 'mha'@'192.168.13.%' identified by 'manager';"
mysql -uroot -pabc123 -e"grant all privileges on *.* to 'mha'@'Mysql1' identified by 'manager';"
mysql -uroot -pabc123 -e"grant all privileges on *.* to 'mha'@'Mysql2' identified by 'manager';"
mysql -uroot -pabc123 -e"grant all privileges on *.* to 'mha'@'Mysql3' identified by 'manager';"
mysql -uroot -pabc123 -e"flush privileges;"

master_log_file=`cat /mnt/master_log_file`
master_position=`cat /mnt/master_position`

mysql -uroot -pabc123 -e"change master to master_host='192.168.13.10';"
mysql -uroot -pabc123 -e"change master to master_user='myslave';"
mysql -uroot -pabc123 -e"change master to master_password='123';"
mysql -uroot -pabc123 -e"change master to master_log_file='"$master_log_file"';"
mysql -uroot -pabc123 -e"change master to master_log_pos='"$master_position"';"

mysql -uroot -pabc123 -e"start slave;"
mysql -uroot -pabc123 -e"show slave status\G" |grep Slave_IO_Running
mysql -uroot -pabc123 -e"show slave status\G" |grep Slave_SQL_Running

mysql -uroot -pabc123 -e"set global read_only=1;"

#mysql -uroot -pabc123 -e"show databases;" #开启后可检查主从复制是否开启
}

2.4 验证

  • 进入主服务器创建任意库;
  • 进入从服务器查看所有库,发现数据同步;

三、思维导图

在这里插入图片描述

四、结语

  • 从服务器配置主服务器的时候,要注意文件名与偏移量一定要相同
原网站

版权声明
本文为[雪碧不要气]所创,转载请带上原文链接,感谢
https://blog.csdn.net/H875035681/article/details/125165406