当前位置:网站首页>【mysql】主从复制原理、搭建
【mysql】主从复制原理、搭建
2022-06-09 20:16:00 【雪碧不要气】
前言
本博客内容仅为记录博主思路,仅供参考,一切以自己实践结果为准。
一、工作原理
1.1 示意图

1.2 文字解释
- 主服务器开启二进制日志,从服务器开启中继日志
- 主服务器日志发生更新后,从服务器通过I/O线程探测到更新并发送请求二进制事件
- 主服务器通过dump线程将更新的二进制日志事件发送给从服务器
- 从服务器将事件写入中继日志,通过SQL线程将日志读取为sql语句,重放执行语句,保持数据的统一性
二、搭建
2.1 架构
| 模拟环境 | IP地址 | 所需工具 | root密码 |
|---|---|---|---|
| 主服务器 | 192.168.13.10 | mysql-boost-5.7.20.tar | abc123 |
| 从服务器 | 192.168.13.20 | mysql-boost-5.7.20.tar | abc123 |
2.2 主服务器
- mysql安装步骤在这篇博客已经写了:msql源码安装
#!/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 验证
- 进入主服务器创建任意库;
- 进入从服务器查看所有库,发现数据同步;
三、思维导图

四、结语
- 从服务器配置主服务器的时候,要注意文件名与偏移量一定要相同
边栏推荐
- ConvNets Principles
- Xcode 14 带来全新提升,Xcode Cloud 正式推出!
- 杰理之io_key 双按键如何使用?【篇】
- Jerry's chart confirms that the project has not opened config_ NO_ SDRAM_ Enable macro, SDRAM used in the project [chapter]
- 下载最新的全球疫情历史数据,下载到当前日前两天
- 安路科技发布SF1系列FPSoC新品:高集成低功耗,助力实现多种应用场景
- NoSQL redis configuration and optimization (I blew the night breeze of Qinhuai River for you when you were not in Nanjing)
- moderlarts第二次培训笔记
- EU requires all mobile phones to use usb-c interface! Apple will be forced to abandon lightning interface
- UNION ALL UNION FULL JOIN
猜你喜欢

Leetcode 1984. 学生分数的最小差值(可以,已解决)
![[opencvsharpdnn] implementation example of yolov3 and Caffe in opencvsharp](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[opencvsharpdnn] implementation example of yolov3 and Caffe in opencvsharp

<collection>和<association>标签

ConvNets Principles
How to query the database with eloquent in laravel (select)

Root file system

不止于观测|阿里云可观测技术峰会正式上线

面试字节测开岗失败后,被面试官在朋友圈吐槽了......(心累)

Kalman filter (KF) unscented Kalman filter (UKF)

Xcode 14 带来全新提升,Xcode Cloud 正式推出!
随机推荐
FPGA入门实验-基于状态机实现多按键控制变速流水灯和跳变灯
How to query the database with eloquent in laravel (select)
EU requires all mobile phones to use usb-c interface! Apple will be forced to abandon lightning interface
Automatically update domain name resolution to local IP, DDNS
官宣!博通将以610亿美元收购VMware,并承担80亿美元债务
HMI 联机下载失败的问题及解决方案
杰理之蓝牙配网【篇】
paramiko和线程池Demo(快速批量操作服务器)
VNCTF 2022 InterestingPHP
Neo4j桌面版数据库备份
Jerry's image transmission, recording card or camera displayed by UVC - description of high frame rate [article]
NoSQL之Redis配置与优化(你不在南京的日子我替你吹了秦淮河的晚风)
How did musk become Jindong?
Jerry's IO_ Key how to use double keys? [chapter]
Alibaba cloud 22q1 status summary, where are you going
Is there any risk in opening an account with Ping An Securities? Is it safe?
asp. Net TXT read / write
安路科技发布SF1系列FPSoC新品:高集成低功耗,助力实现多种应用场景
95後大廠程序員删庫被判刑9個月
常用正则表达式