当前位置: 首页 > news >正文

Linux——mysql主从复制与读写分离

一,理解什么是mysql主从复制

1,mysql支持的复制类型

  • 基于语句的复制:

在主服务器上执行的sql语句,在从服务器上执行同样的语句,mysql默认采用基于语句的复制,效率比较高。

  • 基于行的复制:

把改变的内容复制过去,而不是把命令在从服务器上执行一遍。

  • 基于二进制文件的复制:

完全基于语句复制,binlog日志文件中记录原始 SQL 语句(默认模式)。

2,mysql主从复制的工作流程

  • 在每个事务更新数据完成之前,Master 将这些改变记录进二进制日志。写入二进制日志完成后,Master 通知存储引擎提交事务。
  • Slave 将 Master 的 Binary log 复制到其中继日志(Relay log)。首先,Slave 开始一个工作线程--I/0 线程,I/0 线程在 Master 上打开一个普通的连接,然后开始 Binlog dump process。Binlog dump process 从 Master 的二进制日志中读取事件,如果已经跟上 Master,它会睡眠并等待 Master 产生新的事件。I/0 线程将这些事件写入中继日志。
  • SQL slave thread(SQl 从线程)处理该过程的最后一步。SQL 线程从中继日志读取事件,并重放其中的事件而更新 Slave数据,使其与 Master 中的数据保持一致。只要该线程与 I/0 线程保持一致,中继日志通常会位于0S的缓存中,所以中继日志的开销很小。复制过程有一个很重要的限制,即复制在S1ave 上是串行化的,也就是说 Master 上的并行更新操作不能在 Slave 上并行操作。

二,配置mysql主从复制

1,准备工作

dnf -y install ntpdate          ##安装时间同步软件包
date                            ##查看时间是否同步systemctl stop firewalld        ##关闭防火墙
setenforce 0                    ##关闭linux内核##修改mysql配置文件,添加以下内容(mysqld模块中添加)
[root@localhost local]# vim /etc/my.cnf
log-bin=/usr/local/mysql/mysql-bin            ##指定二进制文件位置
server-id=1                                   ##设置mysql服务id
binlog-format=MIXED                           ##用于控制二进制日志的记录格式为混合模式systemclt restart mysqld     ##重启mysql数据库

2,登陆mysql进行操作

create user 'myslave'@'%' identified by 'pwd123';	##创建用户grant replication slave on *.* to 'myslave'@'%';	##给复制权限
alter user 'myslave'@'%' identified with mysql_native_password by 'pwd123';   ##为myslave设置密码为pwd123.flush privileges;       ##更新用户信息mysql> show master status;            ##查看主服务区状态信息,等会要用
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      342 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

3,在从设备进行操作

[root@localhost local]# vim /etc/my.cnf
server-id=2                ##在从服务器添加id(不能与主服务器重复)
systemclt restart mysqld     ##重启mysql数据库##在从服务器进行操作
change master to master_host='192.168.10.101',master_user='myslave',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=1438;start slave;              ##启动slave
show slave status\G       ##查看主从状态Slave_IO_Running: Yes                  ##找到此部分为两个yes就成功了Slave_SQL_Running: Yes

4,验证 

##在主服务器创建名为auth的数据库
mysql> create database auth;##查看从服务器是否同步成功
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| auth               |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

三,配置mysql主主复制

1,配置从服务器

##在从服务器添加
[root@bogon ~]# vim /etc/my.cnf
log-bin=/usr/local/mysql/mysql-bin
binlog-format=MIXEDsystemclt restart mysqld     ##重启mysql数据库create user 'myslave'@'%' identified by 'pwd123'; ##创建用户
grant replication slave on *.* to 'myslave'@'%';  ##修改权限
alter user 'myslave'@'%' identified with mysql_native_password by 'pwd123';  ##该密码
flush privileges;    ##刷新show master status;	      ##查看状态信息
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |     1149 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)stop slave;	                ##一定要关闭slave

2,配置双主连接

##在101操作
change master to master_host='192.168.10.102',master_user='myslave',master_ppassword='pwd123',master_log_file='mysql-bin.000001',master_log_pos=1149;	##查看102的状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |     1149 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)##在102操作
change master to master_host='192.168.10.101',master_user='myslave',master_password='pwd123',master_log_file='mysql-bin.000001',master_log_pos=342;	##查看101的状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      342 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

相关文章:

  • 为什么 Linux 上默认没有 host.docker.internal
  • MySQL基础原理
  • 赋能数据报告:解锁智能化分析建议新姿势
  • Llama:开源的急先锋
  • ROS2学习(1)-------安装ROS2
  • 电脑开机提示按f1原因分析及解决方法(6种解决方法)
  • docker-compose——安装redis
  • C语言—再学习(指针)
  • AI 产业化浪潮:从生成智能到星载计算,中国如何重塑全球竞争格局
  • 力扣算法ing(70 / 100 )
  • Python爬虫实战:研究进制流数据,实现逆向解密
  • Protocol Buffers 全流程通俗讲解
  • DHCP协议
  • 基于 NanoDet 的工厂巡检机器人目标识别系统研究与实现​
  • 基于RFSOC ZU28DR+DSP 6U VPX处理板
  • CPS联盟+小程序聚合平台分销返利系统开发|小红书番茄网盘CPA拉新推广全解析
  • 使用matlab进行数据拟合
  • 【操作系统期末速成】①操作系统概述
  • 紫光同创FPGA实现AD9280数据采集转UDP网络传输,分享PDS工程源码和技术支持和QT上位机
  • 基于IMX429-IMX430-IMX432-IMX437等sensor的SLVS桥MIPI透传模组方案
  • 盛和资源海外找稀土矿提速:拟超7亿元收购匹克,加快推动坦桑尼亚项目
  • 2025财政观察|长三角“三公”经费普降,钱要用在刀刃上
  • 125%→10%、24%税率暂停90天,对美关税开始调整
  • 习近平同巴西总统卢拉会谈
  • 沈阳一超市疑借领养名义烹食流浪狗,当地市监局:已收到多起投诉
  • 80后莆田市文旅局长马骏登台与杨宗纬合唱,“演唱会秒变旅游推介会”