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

MySQL主从复制原理及工作过程

一、主从复制原理

1、MySQL将数据变化记录到二进制日志中;
2、Slave将MySQL的二进制日志拷贝到Slave的中继日志中;
3、Slave将中继日志中的事件在做一次,将数据变化,反应到自身(Slave)的数据库

详细步骤:
1、从库通过手工执行change master to 语句连接主库,提供了连接的用户一切条件(user 、password、port、ip),并且让从库知道,二进制日志的起点位置(file名 position 号); start slave
2、从库的IO线程和主库的dump线程建立连接。
3、从库根据change master to 语句提供的file名和position号,IO线程向主库发起binlog的请求。
4、主库dump线程根据从库的请求,将本地binlog以events的方式发给从库IO线程。
5、从库IO线程接收binlog events,并存放到本地relay-log中,传送过来的信息,会记录到master.info中
6、从库SQL线程应用relay-log,并且把应用过的记录到relay-log.info中,默认情况下,已经应用过的relay 会自动被清理purge

二、配置过程

一主两从:

# 主库配置

[root@openEuler ~]# vim /etc/my.cnf.d/mysql-server.cnf   #  基于`binlog`的主从同步

server_id=103        # 配置id号


[root@openEuler ~]# systemctl  restart mysqld.service    # 重启服务


[root@openEuler ~]# mysql           # 进入mysql
  

#  创建用户并授权 

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)


#  查看主服务器(在主从复制架构中)的二进制日志(binary log)的相关状态信息

mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000002 |      992 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)



# 测试验证 ,创建数据库

mysql> create database school;
Query OK, 1 row affected (0.01 sec)

mysql>
# 从库配置

[root@openEuler ~]# vim /etc/my.cnf.d/mysql-server.cnf

server_id=104

[root@openEuler ~]# systemctl  restart  mysqld.service
[root@openEuler ~]# mysql


mysql> change master to
    -> master_host='172.25.254.103',                       # 指定主服务器的 IP 地址
    ->  master_user='rep',                                 # 指定用于复制的用户
    ->  master_password='123456',                          # 指定上述复制用户的密码
    ->  master_log_file='binlog.000002',                   # 指定从服务器开二进制日志文件
    ->  master_log_pos=992;                                
Query OK, 0 rows affected, 8 warnings (0.01 sec)

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 172.25.254.103
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000002
          Read_Master_Log_Pos: 992
               Relay_Log_File: openEuler-relay-bin.000002
                Relay_Log_Pos: 323
        Relay_Master_Log_File: binlog.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 992
              Relay_Log_Space: 537
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 103
                  Master_UUID: f5a6733a-e505-11ef-88cf-000c29635ce1
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
       Master_public_key_path:
        Get_master_public_key: 0
            Network_Namespace:
1 row in set, 1 warning (0.00 sec)


# 测试 


mysql> show  databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
5 rows in set (0.00 sec)


# 从库2


[root@openEuler ~]# vim /etc/my.cnf.d/mysql-server.cnf

server_id=105

[root@openEuler ~]# systemctl  restart mysqld.service
[root@openEuler ~]# mysql


mysql> change master to
    ->  master_host='172.25.254.103',
    ->  master_user='rep',
    ->  master_password='123456',
    ->  master_log_file='binlog.000002',
    ->  master_log_pos=992;
Query OK, 0 rows affected, 8 warnings (0.01 sec)

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.03 sec)

mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 172.25.254.103
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000002
          Read_Master_Log_Pos: 992
               Relay_Log_File: openEuler-relay-bin.000002
                Relay_Log_Pos: 323
        Relay_Master_Log_File: binlog.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 992
              Relay_Log_Space: 537
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 103
                  Master_UUID: f5a6733a-e505-11ef-88cf-000c29635ce1
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
       Master_public_key_path:
        Get_master_public_key: 0
            Network_Namespace:
1 row in set, 1 warning (0.00 sec)



# 测试验证  

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
5 rows in set (0.01 sec)


http://www.kler.cn/a/536221.html

相关文章:

  • 《深度揭秘LDA:开启人工智能降维与分类优化的大门》
  • 32.日常算法
  • 【Spring Boot】 SpringBoot自动装配-Condition
  • 第16章 Single Thread Execution设计模式(Java高并发编程详解:多线程与系统设计)
  • ssh中公钥和私钥怎么生成
  • 2.6-组合博弈入门
  • [数据结构] Set的使用与注意事项
  • OpenBMC:可编译的硬件平台定义在哪
  • 安恒网安面试题来啦!
  • 模型压缩技术(一):模型蒸馏,给大模型“瘦身”
  • 软件设计模式
  • 自动化测试(selenium篇)
  • python怎么求 一个数是否包含3
  • WPF模板
  • Python应用指南:一个库解决常见的国内坐标系转换需求
  • 工厂模式+枚举类的json序列化+redisson的使用
  • 【深度学习】DataLoader自定义数据集制作
  • 海康威视豆干型网络相机QT的Demo
  • 【学习总结|DAY036】Vue工程化+ElementPlus
  • 华为小艺助手接入DeepSeek,升级鸿蒙HarmonyOS NEXT即可体验
  • Linux中DataX使用第三期
  • Java 8的Stream API
  • 栈和队列的实现(C语言)
  • 解决aspose将Excel转成PDF中文变成方框的乱码问题
  • esp32 udp 客户端 广播
  • 【Elasticsearch】nested聚合