简述mysql 主从复制原理及其工作过程,配置一主两从并验证

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

MySQL 主从复制(Master-Slave Replication)是一种数据同步技术,其中一个 MySQL 实例(主库)将其数据变更(插入、更新、删除)通过二进制日志(Binary Log)记录下来,然后将这些变更传递给一个或多个 MySQL 实例(从库),从库应用这些变更,确保从库的数据与主库一致。

当主库数据发生变更时,写入本地Bin Log文件

从库IO线程发起dump主库Bin Log文件的请求

主库IO线程推送Bin Log文件到从库中

从库IO线程把Bin Log内容写入本地的Relay Log文件中

从库SQL线程读取Relay Log文件内容

从库SQL线程重新执行一遍SQL语句

准备三台虚拟机192.168.1.11(Mater),192.168.1.12(slave-1)和192.168.1.13(slave-2)

选取ip为192.168.1.11作为主布机子

首先主库配置

复制代码
 vim /etc/my.cnf

  grant replication slave on *.* to  rep@'192.168.1.%';

启动

复制代码
 systemctl enable --now mysqld.service

在主库里授权一个用户

复制代码
 create user rep@'192.168.1.%' identified with mysql_native_password by '123456';

查看binlog文件名(要用到)

复制代码
 show master status;

接着配置另外两个从库

复制代码
vim /etc/my.cnf

配置内容

复制代码
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
 
#
# This group is read by the server
#
[mysqld]
server_id=133
 
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

 
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
 
#
# This group is read by the server
#
[mysqld]
server_id=134
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

接着启动

复制代码
 systemctl enable --now mysqld.service

设置

复制代码
change master to  master_host='192.168.1.11', master_user='rep', master_log_file='binlog.000001', master_log_pos=678;

查看状态

复制代码
show slave status \G

*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.1.130
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000001
          Read_Master_Log_Pos: 678
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 323
        Relay_Master_Log_File: binlog.000001
             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: 678
              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: 130
                  Master_UUID: 3928f497-d672-11ef-8e26-000c294d2ac6
             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)

连接成功

相关推荐
赵锦川3 分钟前
Oracle数据库序列的创建
数据库
柱子jason36 分钟前
使用IOT-Tree对接工业现场PLC并把采集数据记录到关系数据库中
数据库·物联网·plc·工业物联网·工业自动化·iot-tree·生产线配套
2301_8223754439 分钟前
Python虚拟环境(venv)完全指南:隔离项目依赖
jvm·数据库·python
2301_7903009640 分钟前
Python类型提示(Type Hints)详解
jvm·数据库·python
一路向北⁢1 小时前
Spring Boot 3 整合 SSE (Server-Sent Events) 企业级最佳实践(二)
java·数据库·spring boot·sse·通信
远方16091 小时前
112-Oracle database 26ai下载和安装环境准备
大数据·数据库·sql·oracle·database
2401_838472511 小时前
Python多线程与多进程:如何选择?(GIL全局解释器锁详解)
jvm·数据库·python
光影少年1 小时前
非关系数据库和关系型数据库都有哪些?
数据库·数据库开发·非关系型数据库
2301_822363601 小时前
Python单元测试(unittest)实战指南
jvm·数据库·python
麦兜*1 小时前
深入解析分布式数据库TiDB核心架构:基于Raft一致性协议与HTAP混合负载实现金融级高可用与实时分析的工程实践
数据库·分布式·tidb