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

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

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

一、基于binlog的主从同步

##主服务器配置

bash 复制代码
#写配置文件
[root@localhost ~]# tail -1 /etc/my.cnf.d/mysql-server.cnf 
server_id=134
#启动(重启)MySQL服务
[root@localhost ~]# systemctl enable --now  mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.

#创建用户并对用户进行授权
mysql> create user rep@'192.168.198.%' identified with mysql_native_password by 'MySQL@123456';
Query OK, 0 rows affected (0.01 sec)

mysql> grant replication slave on *.* to rep@'192.168.198.%';
Query OK, 0 rows affected (0.00 sec)


#查看日志文件名
mysql> show master status;
+---------------+----------+--------------+------------------+-----------
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_G
+---------------+----------+--------------+------------------+-----------
| binlog.000006 |      682 |              |                  |           
+---------------+----------+--------------+------------------+-----------
1 row in set (0.00 sec)

##从服务器配置

bash 复制代码
#写配置文件
[root@localhost ~]# vim /etc/my.cnf.d/mysql-server.cnf 
[root@localhost ~]# tail -1 /etc/my.cnf.d/mysql-server.cnf 
server_id=142
[root@localhost ~]# systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.

#####另外的从库配置方式一样,只是server_id号改一下


#与主机建立连接
mysql> change master to
    -> master_host='192.168.198.134',
    -> master_user='rep',
    -> master_password='MySQL@123456',
    -> master_log_file='binlog.000006',
    -> master_log_pos=682;
Query OK, 0 rows affected, 8 warnings (0.02 sec)

#检查同步功能是否成功开启(如果不是两个YES可用检查一下主从的uuid)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.10 sec)
mysql> SHOW SLAVE STATUS \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.198.134
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000006
          Read_Master_Log_Pos: 682
               Relay_Log_File: localhost-relay-bin.000004
                Relay_Log_Pos: 323
        Relay_Master_Log_File: binlog.000006
             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: 682
              Relay_Log_Space: 703
              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: 134
                  Master_UUID: e4ce8701-d0af-11ef-af1e-000c293c9c44
             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)

###查看主从数据库的uuid是否一致

bash 复制代码
SHOW GLOBAL VARIABLES LIKE 'server_uuid';
##########如果一致
# 停止从库 MySQL 服务
sudo systemctl stop mysql
# 删除从库 auto.cnf 文件
sudo rm /var/lib/mysql/auto.cnf
# 启动从库 MySQL 服务
sudo systemctl start mysql
-- 登录从库 MySQL
-- 停止从库复制进程
STOP SLAVE;
-- 启动从库复制进程
START SLAVE;
-- 检查从库复制状态
SHOW SLAVE STATUS \G;

###验证

bash 复制代码
#在主库创建一个数据库,看从库是否同步生成
mysql> create database feng;
Query OK, 1 row affected (0.01 sec)


mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| KW                 |
| feng               |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.01 sec)

二、基于gtid的主从同步

##开启gtid

bash 复制代码
##主从一样
[root@localhost ~]# vim /etc/my.cnf.d/mysql-server.cnf 
[root@localhost ~]# systemctl restart mysqld;
[root@localhost ~]# tail -3 /etc/my.cnf.d/mysql-server.cnf 
server_id=142
gtid_mode=ON
enforce-gtid-consistency=ON


##验证是否开启成功
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)

##主服务器创建用户和binlog的一样

##从服务器配置

bash 复制代码
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)

#与主机建立连接
mysql> change master to
    -> master_host='192.168.198.134', 
    -> master_user='rep',
    -> master_password='MySQL@123456',
    -> master_auto_position=1;
Query OK, 0 rows affected, 7 warnings (0.01 sec)

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

#检查同步功能是否成功开启(如果不是两个YES可用检查一下主从的uuid)
mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.198.134
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000007
          Read_Master_Log_Pos: 157
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 367
        Relay_Master_Log_File: binlog.000007
             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: 157
              Relay_Log_Space: 581
              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: 134
                  Master_UUID: e4ce8701-d0af-11ef-af1e-000c293c9c44
             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: 1
         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)

##验证

bash 复制代码
#主
mysql> create database fengfeng;
Query OK, 1 row affected (0.01 sec)

mysql> 



#从
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| KW                 |
| feng               |
| fengfeng           |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
7 rows in set (0.00 sec)

mysql> 
相关推荐
dxt_snow42 分钟前
Centos7系统安装redis
数据库·redis·缓存
0wioiw02 小时前
Python基础(SQLAlchemy)
java·开发语言·数据库
CodeJourney.3 小时前
DeepSeek 关联 Word 使用教程:解锁办公新效率
数据库·人工智能·算法
ChinaRainbowSea3 小时前
十四. Redis 新功能
java·数据库·redis·缓存·bootstrap
NineData3 小时前
NineData云原生智能数据管理平台新功能发布|2025年1月版
数据库·人工智能·云原生·oracle·ninedata
Java小王子呀4 小时前
SQL写法:行行比较
数据库·sql
m0_748245744 小时前
C#数据库操作系列---SqlSugar完结篇
网络·数据库·c#
羊小猪~~5 小时前
MYSQL学习笔记(七):新年第一篇之子查询
数据库·笔记·后端·sql·学习·mysql·考研
无聊的烤苕皮6 小时前
MySQL第五次作业(触发器、存储过程)
android·mysql·adb
小虚竹6 小时前
从MySQL优化到脑力健康:技术人与效率的双重提升
数据库·mysql