MySQL的主从同步

MySQL的主从同步

1.MySQL中从同步原理

  • 简介

    MySQL主从同步是一种数据库复制技术,它可以将一台MySQL数据库(称为主服务器)的数据复制到另一台或多台MySQL数据库(称为从服务器)。主从同步可以用于以下目的:

    • 主服务器将所有数据变更记录到二进制日志(binlog)中。
    • 从服务器连接到主服务器,并从主服务器的二进制日志中获取数据变更信息。
    • 从服务器将获取到的数据变更信息应用到自己的数据库中,从而使主服务器和从服务器的数据保持一致。
  • 基本原理、

    • 主服务器将所有数据变更记录到二进制日志(binlog)中。
    • 从服务器连接到主服务器,并从主服务器的二进制日志中获取数据变更信息。
    • 从服务器将获取到的数据变更信息应用到自己的数据库中,从而使主服务器和从服务器的数据保持一致。

2.基于binlog主从同步

  • 在三个虚拟机下分别安装mysql

    # 在三个虚拟机下分别安装mysql
    root@openEulter-1 ~]# dnf install mysql-server -y
    
  • 主库配置

    [root@openEulter-1 ~]# vim /etc/my.cnf
    [mysqld]
    server_id=1
    # 启动服务
    [root@openEulter-1 ~]# systemctl enable --now mysqld
    # 创建用户并授权
    mysql> create user rep@'172.25.%.%' identified by '123456';
    mysql> grant replication slave on *.* to rep@'172.25.%.%';
    mysql> show master status;
    +---------------+----------+--------------+------------------+-------------------+
    | File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +---------------+----------+--------------+------------------+-------------------+
    | binlog.000003 |      707 |              |                  |                   |
    +---------------+----------+--------------+------------------+-------------------+
    1 row in set (0.00 sec)
    
    
  • 从库1配置

    [root@openEulter-1 ~]# vim /etc/my.cnf
    [mysqld]
    server_id=2
    # 重启服务
    [root@openEulter-1 ~]# systemctl restart mysqld
    [root@openEulter-1 ~]# mysql
    mysql> change master to
        -> master_host='172.25.254.100',
        -> master_user='rep',
        -> master_password='123456',
        -> master_log_file='binlog.000003',
        -> master_log_pos=707,
        -> get_master_public_key=1;
    # 检验
    mysql> start slave;
    mysql> show slave status \G
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for source to send event
                      Master_Host: 172.25.254.100
                      Master_User: rep
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: binlog.000003
              Read_Master_Log_Pos: 707
                   Relay_Log_File: openEulter-1-relay-bin.000002
                    Relay_Log_Pos: 323
            Relay_Master_Log_File: binlog.000003
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    ......
    
  • 从库2配置

    [root@openEulter-1 ~]# vim /etc/my.cnf
    [mysqld]
    server_id=3
    # 重启服务
    [root@openEulter-1 ~]# systemctl restart mysqld
    [root@openEulter-1 ~]# mysql
    mysql> change master to
        -> master_host='172.25.254.100',
        -> master_user='rep',
        -> master_password='123456',
        -> master_log_file='binlog.000003',
        -> master_log_pos=707,
        -> get_master_public_key=1;
    # 检验
    mysql> start slave;   
    mysql> show slave status \G;
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for source to send event
                      Master_Host: 172.25.254.100
                      Master_User: rep
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: binlog.000003
              Read_Master_Log_Pos: 707
                   Relay_Log_File: openEulter-1-relay-bin.000002
                    Relay_Log_Pos: 323
            Relay_Master_Log_File: binlog.000003
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    ......
    
  • 验证

    # 在主库创建数据库db1
    mysql> create database db1;
    # 在两个从库查看数据库
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | db1                |
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    +--------------------+
    

3.基于gtid的主从同步

  • 主库的配置

    [root@openEulter-1 ~]# vim /etc/my.cnf
    [mysqld]
    server_id=1
    gtid_mode=ON
    enforce-gtid-consistency=ON
    [root@openEulter-1 ~]# systemctl restart mysqld
    # 检测配置
    [root@openEulter-1 ~]# mysql
    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       |
    +----------------------------------+-----------+
    
    mysql> show master status;
    +---------------+----------+--------------+------------------+-------------------+
    | File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +---------------+----------+--------------+------------------+-------------------+
    | binlog.000004 |      157 |              |                  |                   |
    +---------------+----------+--------------+------------------+-------------------+
    
  • 从库1的配置

    [root@openEulter-1 ~]# vim /etc/my.cnf
    [mysqld]
    server_id=2
    gtid_mode=ON
    enforce-gtid-consistency=ON
    [root@openEulter-1 ~]# systemctl restart mysqld
    # 检测配置
    [root@openEulter-1 ~]# mysql
    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       |
    +----------------------------------+-----------+
    
    mysql> change master to
        -> master_host='172.25.254.100',
        -> master_user='rep',
        -> master_password='123456',
        -> master_auto_position=1;
    Query OK, 0 rows affected, 7 warnings (0.01 sec)
    ## mysql8.0.22以后,也可以这么写
    mysql> change replication source to
        -> source_host='172.25.254.100',
        -> source_user='rep',
        -> source_password='123456',
        -> source_auto_position=1;
    # 检测
    mysql> start slave;
    mysql> show slave status \G
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for source to send event
                      Master_Host: 172.25.254.100
                      Master_User: rep
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: binlog.000004
              Read_Master_Log_Pos: 157
                   Relay_Log_File: openEulter-1-relay-bin.000002
                    Relay_Log_Pos: 367
            Relay_Master_Log_File: binlog.000004
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    ......
    
    
  • 从库2的配置

    [root@openEulter-1 ~]# vim /etc/my.cnf
    [mysqld]
    server_id=3
    gtid_mode=ON
    enforce-gtid-consistency=ON
    [root@openEulter-1 ~]# systemctl restart mysqld
    # 检测配置
    [root@openEulter-1 ~]# mysql
    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       |
    +----------------------------------+-----------+
    
    mysql> change master to
        ->  master_host='172.25.254.100',
        -> master_user='rep',
        -> master_password='123456',
        ->  master_auto_position=1;
    Query OK, 0 rows affected, 7 warnings (0.02 sec)
    # 检测
    mysql> start slave;
    mysql> show slave status \G
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for source to send event
                      Master_Host: 172.25.254.100
                      Master_User: rep
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: binlog.000004
              Read_Master_Log_Pos: 157
                   Relay_Log_File: openEulter-1-relay-bin.000002
                    Relay_Log_Pos: 367
            Relay_Master_Log_File: binlog.000004
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    ......
    
  • 验证

    # 在主库创建数据库db2
    mysql> create database db2;
    Query OK, 1 row affected (0.00 sec)
    
    # 在从库检验
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | db1                |
    | db2                |
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    +--------------------+
    6 rows in set (0.01 sec)
    

你可能感兴趣的:(Linux学习笔记,mysql,linux,运维,数据库)