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

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

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

MySQL主从同步是基于Bin Log实现的,而Bin Log记录的是原始SQL语句。

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

  • 从库IO线程发起dump主库Bin Log文件的请求
  • 主库IO线程推送Bin Log文件到从库中
  • 从库IO线程把Bin Log内容写入本地的Relay Log文件中
  • 从库SQL线程读取Relay Log文件内容
  • 从库SQL线程重新执行一遍SQL语句

首先保持实验环境干净(关闭防火墙 selinux,克隆的机器是没有下载过MySQL的)

准备克隆了三台机子ip分别为192.168.1.130 192.168.1.133 192.168.1.134

同时下载

复制代码
 yum install mysql-server -y

选取ip为192.168.1.130作为主布机子

首先主库配置

复制代码
 vim /etc/my.cnf
sql 复制代码
  grant replication slave on *.* to  rep@'192.168.1.%';

启动

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

在主库里授权一个用户

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

查看binlog文件名(要用到)

sql 复制代码
 show master status;

接着配置另外两个从库

sql 复制代码
vim /etc/my.cnf

查看配置内容

sql 复制代码
#
# 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
sql 复制代码
#
# 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

设置

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

查看状态

sql 复制代码
show slave status \G
sql 复制代码
*************************** 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)

连接成功


进行测试

在主库创建库

sql 复制代码
 create database jj1;

创建表

sql 复制代码
create table jj(id int);

插入数据

sql 复制代码
insert into jj values(1),(2),(4);

连个从库中查看

相关推荐
茉莉玫瑰花茶23 分钟前
线程同步与互斥(下)
linux·数据库
BUG 劝退师26 分钟前
MySQL数据库编程总结
数据库·mysql
notfindjob1 小时前
QT Sqlite数据库-教程001 创建数据库和表-上
数据库
亚林瓜子1 小时前
docker compose方式安装ClickHouse数据库
数据库·clickhouse·docker·mac·m1
04Koi.1 小时前
Redis进阶--哨兵
数据库·redis·缓存
Linux运维老纪1 小时前
GitLab之搭建(Building GitLab)
linux·mysql·云计算·gitlab·运维开发
老李不敲代码2 小时前
榕壹云在线商城系统:基于THinkPHP+ Mysql+UniApp全端适配、高效部署的电商解决方案
mysql·微信小程序·小程序·uni-app·软件需求
小蒜学长2 小时前
机动车号牌管理系统设计与实现(代码+数据库+LW)
开发语言·数据库·spring boot·后端·spring·oracle
随缘而动,随遇而安2 小时前
第四十六篇 人力资源管理数据仓库架构设计与高阶实践
大数据·数据库·数据仓库·sql·数据库架构
Always_away2 小时前
数据库系统概论|第三章:关系数据库标准语言SQL—课程笔记3
数据库·笔记·sql·学习