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

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)

http://www.xdnf.cn/news/6311.html

相关文章:

  • 人工智能+ERP:政策新规下企业智能化转型路径
  • 【vue】axios网络请求介绍
  • 【2025版】Spring Boot面试题
  • C语言_自定义类型:结构体
  • (4)python开发经验
  • (十七)Java日期时间API全面解析:从传统Date到现代时间处理
  • Ros2 - Moveit2 - DeepGrasp(深度抓握)
  • golang -- 如何让main goroutine等一等
  • 数智驱动——AI:企业数字化转型的“超级引擎”
  • FreeRTOS学习笔记
  • 【Java学习笔记】finalize方法
  • 前后端分离博客 Weblog 项目实战
  • 【AI大模型】赋能【传统业务】
  • Java基础语法之数组
  • Windows下Docker安装portainer
  • 64. 最小路径和
  • Shell 脚本中的通道号(文件描述符)
  • maven项目, idea右上角一直显示gradle的同步标识, 如何去掉
  • 计算机网络:什么是计算机网络?它的定义和组成是什么?
  • 开源Heygem本地跑AI数字人视频教程
  • python使用matplotlib画图
  • IDEA编辑器设置的导出导入
  • why FPGA喜欢FMC子卡?
  • Vue3学习(组合式API——计算属性computed详解)
  • 使用Word2Vec算法实现古诗自动生成实战
  • Linux514 rsync 解决方案环境配置
  • 2025年渗透测试面试题总结-360[实习]安全工程师(题目+回答)
  • 三维CAD皇冠CAD(CrownCAD)建模教程:工程图模块二
  • 52页PPT | 企业数字化转型L1-L5数据架构设计方法论及案例数字化转型解决方案数字化规划方案
  • 回溯实战篇2