简述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> 
相关推荐
CodeBlossom10 分钟前
Redis速通
数据库·redis·缓存
牛奶咖啡1319 分钟前
MySQL InnoDB Cluster 高可用集群部署与应用实践(下)
数据库·mysql·innodb cluster·mysql router·mysql路由的安装部署·mysql路由的测试·mgr组复制
姚远Oracle ACE2 小时前
解读Oracle AWR报告:Global Cache and Enqueue Services - Workload Characteristics
数据库·oracle
流星白龙2 小时前
【Qt】7.信号和槽_connect函数用法(2)
java·数据库·qt
Zzz 小生5 小时前
Claude Code学习笔记(四)-助你快速搭建首个Python项目
大数据·数据库·elasticsearch
nongcunqq8 小时前
abap 操作 excel
java·数据库·excel
rain bye bye9 小时前
calibre LVS 跑不起来 就将setup 的LVS Option connect下的 connect all nets by name 打开。
服务器·数据库·lvs
冻咸鱼9 小时前
MySQL的配置
mysql·配置
阿里云大数据AI技术10 小时前
云栖实录|MaxCompute全新升级:AI时代的原生数据仓库
大数据·数据库·云原生
不剪发的Tony老师10 小时前
Valentina Studio:一款跨平台的数据库管理工具
数据库·sql