MySQL的主从复制

主从复制原理

MySQL的主从复制_第1张图片

 

主从复制三个线程
主节点:
dump Thread:为每个Slave的I/O Thread启动一个dump线程,用于向其发送binary log events
从节点:
I/O Thread:向Master请求二进制日志事件,并保存于中继日志中
SQL Thread:从中继日志中读取日志事件,在本地完成重放

过程:

 1、master开启binlog

2、将master现有的库导出,并导入到slave

3、slave开启同步,指定上次导入数据的位置记录(通过change master命令指令)

4、slave的IO线程向master请求从指定的binlog日志文件的指定位置之后的binlog日志内容

5、master收到请求,负责复制的IO线程去读取指定的binlog日志内容,然后将内容,以及本次返回日志内容的下一个更新位置一起返回给slave的IO线程

6、slave收到日志,依次写入中继日志(relay log)中,并将新的binlog文件名和位置记录到master-info文件中。以便下一次同步。

7、slave的SQL线程会实时检测中继日志的内容,然后及时的把日志中的SQL语句按顺序执行。

实现主从复制配置

主配置

1、配置主要打开bin-log

2、主的server id为20

3、授权一个账号给从、可以从主复制数据

4、备份数据库,传给从,因为同步初始阶段,主从的数据要一致

[root@cent8_yzl_20 ~]# yum install -y mysql-server

#配置主要打开bin-log
[root@cent8_yzl_20 ~]# vi /etc/my.cnf

[mysqld]
#log-bin  #不指定路径,默认记录在/var/lib/mysql里

server-id=20   #主的server id为20

[root@cent8_yzl_20 ~]# systemctl restart mysqld

mysql> select @@log_bin;
+-----------+
| @@log_bin |
+-----------+
|         1 |
+-----------+
1 row in set (0.00 sec)

mysql> select @@sql_log_bin;
+---------------+
| @@sql_log_bin |
+---------------+
|             1 |
+---------------+
1 row in set (0.00 sec)

mysql> 

 #创建binlog目录并授权

[root@cent8_yzl_20 ~]# mkdir /var/lib/mysql/binlog
[root@cent8_yzl_20 ~]# chown -R  mysql.mysql /var/lib/mysql/binlog 
[root@cent8_yzl_20 ~]# vi /etc/my.cnf
#log-bin  不指定路径,默认记录在/var/lib/mysql里

[mysqld]
server-id=20
log-bin=/var/lib/mysql/binlog/mysql-bin

[root@cent8_yzl_20 ~]# systemctl restart mysqld
#创建一个授权用户给从、可以从主复制数据
mysql> create user yzil@'10.0.0.%' identified by 'redhat';

mysql> grant replication slave on *.* to yzil@'10.0.0.%';

#查看主角色状态

#查看从二进制的日志的文件和位置开始进行复制

mysql> select @@server_id;
+-------------+
| @@server_id |
+-------------+
|          20 |
+-------------+
1 row in set (0.00 sec)

mysql> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000011
         Position: 156
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000011 |      156 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> 

#备份数据

#注意,--master-date=2,binlog记录被注释了

[root@cent8_yzl_20 ~]# mkdir /data
[root@cent8_yzl_20 ~]# mysqldump -A -F --single-transaction --master-data=2 > /data/`date +%F`-mysql-all.sql
[root@cent8_yzl_20 ~]# ls /data/2021-05-17-mysql-all.sql 
/data/2021-05-17-mysql-all.sql
[root@cent8_yzl_20 ~]# 

#拷贝到从节点

[root@cent8_yzl_20 ~]# rsync -av /data/2021-05-17-mysql-all.sql 10.0.0.30:/data
[root@cent8_yzl_30 data]# ls
2021-05-17-mysql-all.sql

从配置

1、设置server id为30

2、导入数据

3、指定主是谁,并指连到主的账号信息

#设置server-id=30并开启read-only

#slave建议开启binlog

[root@cent8_yzl_30 ~]# mkdir /var/lib/mysql/binlog
[root@cent8_yzl_30 ~]# chown -R mysql.mysql /var/lib/mysql/binlog

[root@cent8_yzl_30 ~]# vi /etc/my.cnf

[mysqld]
server-id=30
read-only  #设置数据库只读
log-bin=/var/lib/mysql/binlog/mysql-bin

[root@cent8_yzl_30 ~]# systemctl restart mysqld

#临时关闭log_bin

mysql> set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)

mysql> select @@sql_log_bin;
+---------------+
| @@sql_log_bin |
+---------------+
|             0 |
+---------------+
1 row in set (0.00 sec)

#导入数据

[root@cent8_yzl_30 ~]# mysql < /data/2021-05-17-mysql-all.sql 

#使用有复制权限的用户账号连接至主服务器,并启动复制线程

mysql> help change master to;  #查看帮助

mysql> CHANGE MASTER TO
    ->   MASTER_HOST='10.0.0.20',     
    ->   MASTER_USER='yzil',               
    ->   MASTER_PASSWORD='redhat',
    ->   MASTER_PORT=3306,
    ->   MASTER_LOG_FILE='mysql-bin.000011',   #指定主库备份时binlog文件
    ->   MASTER_LOG_POS=156;                   #指主库备份时的posttion
                                               #从指定位置开始同步
                                               #注意,如果主备份数据时,--master-data=1,上面两项就并不用了,因为备份文件里已经有记录了,就不需要指定了。
Query OK, 0 rows affected, 2 warnings (0.32 sec)

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

mysql> start slave;      #开启从角色。默认,所有节点,都开启了master角色
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: 10.0.0.20
                  Master_User: yzil
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000011
          Read_Master_Log_Pos: 156
               Relay_Log_File: cent8_yzl_30-relay-bin.000002
                Relay_Log_Pos: 324
        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: 156
              Relay_Log_Space: 540
              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: 20
                  Master_UUID: e8869add-b716-11eb-b4b7-000c29a9116e
             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: 
            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 (0.00 sec)

mysql> show processlist;    #查看线程
+----+-----------------+-----------------+------+---------+------+--------------------------------------------------------+------------------+
| Id | User            | Host            | db   | Command | Time | State                                                  | Info             |
+----+-----------------+-----------------+------+---------+------+--------------------------------------------------------+------------------+
|  5 | event_scheduler | localhost       | NULL | Daemon  | 1805 | Waiting on empty queue                                 | NULL             |
|  9 | root            | localhost       | NULL | Query   |    0 | starting                                               | show processlist |
| 10 | system user     | connecting host | NULL | Connect |  816 | Waiting for master to send event                       | NULL             |
| 11 | system user     |                 | NULL | Query   |  598 | Slave has read all relay log; waiting for more updates | NULL             |
+----+-----------------+-----------------+------+---------+------+--------------------------------------------------------+------------------+
4 rows in set (0.00 sec)

mysql> 

#开启log_bin

mysql> set sql_log_bin=1;
Query OK, 0 rows affected (0.00 sec)

mysql> select @@sql_log_bin;
+---------------+
| @@sql_log_bin |
+---------------+
|             1 |
+---------------+
1 row in set (0.00 sec)

mysql> 

 

你可能感兴趣的:(MySQL)