MySQL - 主从同步

​​​​​​1.主从同步原理:

MySQL 主从同步是一种数据库复制技术,它通过将主服务器上的数据更改复制到一个或多个从服务器,实现数据的自动同步。

主从同步的核心原理是将主服务器上的二进制日志复制到从服务器,并在从服务器上执行这些日志中的操作。

  • MySQL内建的复制功能是构建大型、高性能应用程序的基础。

  • 通过将MySQL的某一台主机(master)的数据复制到其他主机(slaves)上,并重新执行一遍来执行。

  • 复制过程中一台服务器充当主服务器,而其他一个或多个其他服务器充当从服务器
    2、MySQL支持的复制类型

  • 基于语句(statement)的复制:在主服务器上执行SQL语句,在从服务器上执行同样的语句。MySQL默认采用基于语句的复制,效率比较高。

  • 基于行(row)的复制 把改变的内容复制过去,而不是把命令在从服务器上执行一遍。从MySQL 5.0开始支持。

  • 混合型(mixed)的复制 默认采用基于语句的复制,一旦发现基于语句的无法精确复制时,就会采用基于行的复制。

3、为什么要做主从复制

  • 灾备
  • 数据分布
  • 负载平衡
  • 读写分离
  • 提高并发能力

1.基于binlog的主从同步

1.1 主库配置

1.1.1 配置文件

root@openEuler-1 \~\]# vim /etc/my.cnf.d/mysql-server.cnf ![](https://i-blog.csdnimg.cn/direct/caf8537ac7144635ab8e7662747e3062.png) \[root@openEuler-1 \~\]# systemctl restart mysqld.service #### 1.1.2 授权用户 ```sql mysql> create user rep@'172.25.254.%' identified with mysql_native_password by '123456'; Query OK, 0 rows affected (0.00 sec) mysql> grant replication slave on *.* to rep@'172.25.254.%'; Query OK, 0 rows affected (0.00 sec) mysql> show master status; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000003 | 680 | | | | +---------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) ``` ### 1.2 从库配置 #### 1.2.1 配置文件 \[root@openEuler-1 \~\]# vim /etc/my.cnf.d/mysql-server.cnf ![](https://i-blog.csdnimg.cn/direct/8439eb21ac0548a6bbfc02d0e618eeab.png) \[root@openEuler-1 \~\]# systemctl restart mysqld \[root@openEuler-1 \~\]# vim /etc/my.cnf.d/mysql-server.cnf ![](https://i-blog.csdnimg.cn/direct/0477637825a04cf8b11e2e57d7785de5.png) \[root@openEuler-1 \~\]# systemctl restart mysqld #### 1.2.2 **设置从库的change master** ```sql mysql> change master to master_host='172.25.254.11', -> master_user='rep', -> master_password='123456', -> master_log_file='binlog.000003', -> master_log_pos=1202 -> ; Query OK, 0 rows affected, 8 warnings (0.04 sec) mysql> start slave; Query OK, 0 rows affected, 1 warning (0.12 sec) ``` #### 1.2.3 查看是否配置成功 ![](https://i-blog.csdnimg.cn/direct/1084aa3d0fcb44c793e166187153f4a8.png) ### 1.3 测试 **主库创建数据库:** ```sql mysql> create database db1; Query OK, 1 row affected (0.02 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | db1 | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> use db1; Database changed mysql> create table t1(id int); Query OK, 0 rows affected (0.14 sec) mysql> insert into t1 values(1),(2),(3),(4); Query OK, 4 rows affected (0.07 sec) Records: 4 Duplicates: 0 Warnings: 0 ``` 从库查看是否存在: ![](https://i-blog.csdnimg.cn/direct/fc10045811c3438384acd384b923151e.png) ![](https://i-blog.csdnimg.cn/direct/ecb84188fefc46a591a1355b7f51c6c3.png) ## 2.基于gtid的主从同步配置 ### 2.1 配置文件(三台同时进行) > **首先停掉从库基于`binlog`的主从同步:** > > mysql\> stop slave; > > Query OK, 0 rows affected, 1 warning (0.02 sec) **开启gtid:** gtid_mode=ON enforce-gtid-consistency=ON \[root@openEuler-1 \~\]# vim /etc/my.cnf.d/mysql-server.cnf ![](https://i-blog.csdnimg.cn/direct/772ea6557f004627a609f434ad9780cd.png) **检查是否成功:** ```sql mysql> show variables like '%gtid%'; +----------------------------------+-----------+ | Variable_name | Value | +----------------------------------+-----------+ | binlog_gtid_simple_recovery | ON | | enforce_gtid_consistency | ON | | gtid_executed | | | gtid_executed_compression_period | 0 | | gtid_mode | ON | | gtid_next | AUTOMATIC | | gtid_owned | | | gtid_purged | | | session_track_gtids | OFF | +----------------------------------+-----------+ 9 rows in set (0.00 sec) mysql> show master status; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000004 | 157 | | | | +---------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) ``` **设置从库的change master:** ```sql mysql> stop slave; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> change master to master_host='172.25.254.11', -> master_user='rep', -> master_password='123456', -> master_auto_position=1; Query OK, 0 rows affected, 7 warnings (0.02 sec) ``` ### 2.2 测试 **主库:** ```sql mysql> create database db2; Query OK, 1 row affected (0.00 sec) ``` **从库:** ![](https://i-blog.csdnimg.cn/direct/105f8e6f1c7d4914be0d793a51d46ac8.png)

相关推荐
快乐点吧6 分钟前
【MongoDB】windows安装、配置、启动
数据库·windows·mongodb
yangmf20408 分钟前
私有知识库 Coco AI 实战(二):摄入 MongoDB 数据
数据库·人工智能·mongodb·coco ai
努力进修13 分钟前
【金仓数据库征文】金仓数据库:开启未来技术脑洞,探索数据库无限可能
数据库·金仓数据库 2025 征文·数据库平替用金仓
键盘飞行员14 分钟前
使用 Node、Express 和 MongoDB 构建一个项目工程
数据库·mongodb·express
小白教程18 分钟前
MySQL主从数据库配置教程
数据库·mysql·adb·mysql8.0主从配置
foo1st33 分钟前
MySQL 8(Ubuntu 18.04.6 LTS)安装笔记
笔记·mysql·ubuntu
CodeJourney.35 分钟前
深度探索:DeepSeek赋能WPS图表绘制
数据库·人工智能·算法·信息可视化·excel
zru_96021 小时前
MongoDB 入门使用教程
数据库·mongodb
berryyan1 小时前
AKShare安装教程(一步一步适合新手)
数据库·投资
bobacgo1 小时前
[MySQL 面试题]-内部技术架构 55-56 MySQL 8.0 自带存储引擎及作用
mysql·面试