mysql主从复制

MySQL主从复制

1.传统主从复制的原理

  • 主库(master)将写操作记录到binlog日志中并生成一个log dump线程,将binlog日志传给binlog日志传给从库(slave)的i/o线程
  • 从库生成两个线程,一个I/O线程,一个SQL线程
    • I/O线程请求主库的binlog,并将得到的binlog日志写到relay log(中继日志)文件中
    • SQL线程,会读取relay log 文件中的日志,并解析成具体操作,来实现主从操作的一致,最终达成数据一致的目的

2.主从复制的配置

主从复制配置步骤:

  1. 确保从数据库与主数据库里的数据一样
  2. 在主数据库里创建一个同步账号授权给从数据库使用
  3. 配置主数据库(修改配置文件)
  4. 配置从数据库(修改配置文件)

环境准备

角色 IP 系统版本/mysql版本
master 192.168.169.139 centos8/mysql-5.7
slave 192.168.169.140 centos8/mysql-5.7

2.1 安装mysql

分别在主从两台服务器上安装mysql-5.7版本,过程请参考《mysql基础》《mysql进阶》

2.2 mysql主从配置

2.2.1 确保从数据库与主数据库里的数据一样

为确保从数据库与主数据库里的数据一样,先全备主数据库并还原到从数据库中

//关闭防火墙(两台都需要关闭)
systemctl stop firewalld

//先查看主库有哪些数据库
[root@master ~]# mysql -e 'show databases;'
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| zzd                |
+--------------------+

//查看从库有哪些数据库
[root@slave ~]# mysql -e 'show databases;'
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

//全备主库
//全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)

//备份主库并将备份文件传送到从库
[root@master ~]# mysqldump --all-databases > all-$(date +'%Y%m%d%H%M%S').sql
[root@master ~]# scp all-20220802003132.sql root@192.168.169.140:/root
The authenticity of host '192.168.169.140 (192.168.169.140)' can't be established.
ECDSA key fingerprint is SHA256:h0JBqfyD7nmBvCwBazGyK6b50D/gE11IG/dW6E3J1Ug.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.169.140' (ECDSA) to the list of known hosts.
root@192.168.169.140's password: 
all-20220802003132.sql                                                 100%  857KB  97.9MB/s   00:00    

//解除数据库锁定状态
mysql> exit
Bye

//在从库上恢复主库的备份并查看从库有哪些库,确保与主库一致
[root@slave ~]# ls
all-20220802003132.sql  anaconda-ks.cfg   pass
[root@slave ~]# mysql < all-20220802003132.sql
[root@slave ~]# mysql -e 'show databases;'
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| zzd                |
+--------------------+

2.2.2 在主数据库里创建一个同步账号授权给从数据库使用

mysql> grant replication slave on *.* to repl@'192.168.169.140' identified by 'repl140!';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

2.2.3 配置主数据库

[root@master ~]# vim /etc/my.cnf 
//在[mysqld]这段的后面加上如下内容
    log-bin = mysql_bin        #开启binlog日志
    server-id = 10		#数据库服务器唯一标识符,从库的server-id值必须比主库的大
    symbolic-links = 0	

//重启mysql服务
[root@master ~]# systemctl restart mysqld.service

//查看主库的状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql_bin.000021 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

2.2.4 配置从数据库

[root@slave ~]# vim /etc/my.cnf
//在[mysqld]这段的后面加上如下内容
    server-id = 20     			#设置从库的唯一标识符,从库的server-id值必须小于主库的该值
    relay-log = mysql-relay-bin   #启用中继日志relay-log

    symbolic-links = 0
//重启mysql服务
[root@slave ~]# systemctl restart mysqld.service


//配置并启动主从复制
mysql> change master to 
    -> master_host='192.168.169.139',
    -> master_user='repl',
    -> master_password='repl140!',
    -> master_log_file='mysql_bin.000021',
    -> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

//查看从服务器状态
mysql> show slave status\G;
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.169.139
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000022
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql-relay-bin.000003
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql_bin.000022
             Slave_IO_Running: Yes			#必须为yes
            Slave_SQL_Running: Yes			#必须为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: 154
              Relay_Log_Space: 740
              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: 10
                  Master_UUID: c8900af2-0d8b-11ed-b3bc-000c29f177ce
             Master_Info_File: /opt/mysql/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave 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: 
1 row in set (0.00 sec)

2.2.5 进行测试

在主数据库上进行创建数据库操作

//创建数据库
mysql> create database zic;
Query OK, 1 row affected (0.00 sec)

mysql> use zic
Database changed

//创建表
mysql> create table test1(id int not null,name varchar(50));
Query OK, 0 rows affected (0.00 sec)

在从数据库上面进行验证

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| zic                |
| zzd                |
+--------------------+
6 rows in set (0.00 sec)

mysql> use zic;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------+
| Tables_in_zic |
+---------------+
| test1         |
+---------------+
1 row in set (0.00 sec)

mysql> desc test1;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   |     | NULL    |       |
| name  | varchar(50) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

3.GTID主从

3.1 GTID工作原理

  • master更新数据时,会在事务前产生GTID,一同记录到binlog日志中。
  • slave端的i/o 线程将变更的binlog,写入到本地的relay log中。
  • sql线程从relay log中获取GTID,然后对比slave端的binlog是否有记录。
  • 如果有记录,说明该GTID的事务已经执行,slave会忽略。
  • 如果没有记录,slave就会从relay log中执行该GTID的事务,并记录到binlog。
  • 在解析过程中会判断是否有主键,如果没有就用二级索引,如果没有就用全部扫描。

3.2 GTID主从配置

环境准备

角色 IP 系统版本/mysql版本
master 192.168.169.139 centos8/mysql-5.7
slave 192.168.169.140 centos8/mysql-5.7

主库配置

//编辑my.cnf文件
[root@master ~]# vim /etc/my.cnf
//添加以下内容
    server-id = 10
    log-bin = mysql_bin
    gtid_mode = on
    enforce-gtid-consistency = true
    log-slave-updates = on

//重启mysql服务
[root@master ~]# systemctl restart mysqld.service

从库配置

//编辑my.cnf文件
[root@slave ~]# vim /etc/my.cnf
//添加以下内容
    server-id = 20
    relay-log = myrelay
    gtid_mode = on
    enforce-gtid-consistency = true
    log-slave-updates = on
    read_only = on
    master-info-repository = TABLE
    relay-log-info-repository = TABLE
    
//重启mysql服务
[root@slave ~]# systemctl restart mysqld.service 

主库授权复制用户

mysql> grant replication slave on *.* to repl@'%' identified by 'repl123!';
Query OK, 0 rows affected, 1 warning (0.00 sec)

从库设置要同步的主库信息,并开启同步。

mysql> change master to 
    -> master_host='192.168.169.139',
    -> master_port=3306,
    -> master_user='repl',
    -> master_password='repl123!',
    -> master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

//查看从库状态
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.169.139
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000011
          Read_Master_Log_Pos: 437
               Relay_Log_File: myrelay.000002
                Relay_Log_Pos: 650
        Relay_Master_Log_File: mysql_bin.000011
             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: 437
              Relay_Log_Space: 849
              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: 10
                  Master_UUID: c8900af2-0d8b-11ed-b3bc-000c29f177ce
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave 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: c8900af2-0d8b-11ed-b3bc-000c29f177ce:1
            Executed_Gtid_Set: c8900af2-0d8b-11ed-b3bc-000c29f177ce:1
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)
posted @ 2022-08-02 01:18  Zic师傅  阅读(65)  评论(0编辑  收藏  举报