简述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> 
相关推荐
员大头硬花生11 分钟前
六、InnoDB引擎-架构-结构
数据库·mysql·oracle
程序新视界1 小时前
在MySQL中,是否可以使用UUID作为主键?
数据库·后端·mysql
晓py1 小时前
InnoDB 事务日志机制全流程详解|从 SQL 到崩溃恢复的完整旅程
数据库·sql·oracle
爱考证的小刘1 小时前
MySQL OCP认证和Oracle OCP认证分别是什么?
mysql·mysql数据库·数据库mysql·oracle数据库·oracle19c·oracle认证·mysql自学
挨踢攻城1 小时前
Oracle OCP认证:深度解析与实战指南
mysql·oracle·dba·开闭原则·ocp·公众号:厦门微思网络·数据库专家
白帽子黑客杰哥2 小时前
湖湘杯网络安全技能大赛参与形式
数据库·web安全·渗透测试·安全演练·湖湘杯·实战演练
寒秋丶4 小时前
Milvus:Json字段详解(十)
数据库·人工智能·python·ai·milvus·向量数据库·rag
ManageEngineITSM7 小时前
技术的秩序:IT资产与配置管理的现代重构
大数据·运维·数据库·重构·工单系统
档案宝档案管理10 小时前
档案宝:企业合同档案管理的“安全保险箱”与“效率加速器”
大数据·数据库·人工智能·安全·档案·档案管理
wangjialelele10 小时前
mysql库操作二
数据库·mysql