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  \[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  \[root@openEuler-1 \~\]# systemctl restart mysqld \[root@openEuler-1 \~\]# vim /etc/my.cnf.d/mysql-server.cnf  \[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 查看是否配置成功  ### 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 ``` 从库查看是否存在:   ## 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  **检查是否成功:** ```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) ``` **从库:** 