MySQL master-slave replication (4)-the construction of cascading replication architecture

MySQL master-slave replication (4)-the construction of cascading replication architecture

MySQL's replication capabilities are the foundation for building large, high-performance applications. Distribute MySQL data to multiple systems. This distribution mechanism is realized by copying the data of a certain MySQL master (master) to other masters (slaves) and executing it again. During the replication process, one server acts as the master server, and one or more other servers act as slave servers.

One, MySQL commonly used master-slave replication architecture

The replication architecture needs to follow the following basic principles:
(1) Each slave can only have one master;
(2) Each slave has a unique server ID (server-id);
(3) Each master can have one or Many slaves;
(4) By setting log_slave_updates, one slave can become the master of other slaves.

The commonly used master-slave replication architecture of MySQL is as follows:

1. One master and one slave or one master and multiple slaves

A master-slave replication architecture is a replication system composed of a master and a slave. The one-master-multi-slave replication architecture is composed of a master and multiple slaves. Slaves do not communicate with each other, but can only communicate with the master. In actual application scenarios, more than 90% of MySQL replication is an architecture pattern in which one Master replicates to one or more Slaves, which is mainly used for applications with high read pressure. The one-master multiple-slave replication architecture is shown in the following figure:
Insert picture description here

This structure is suitable for fewer write operations and many read operations. The read operation can be distributed to other slaves, thereby reducing the pressure on the master. However, when the slave increases to a certain number, the load of the slave on the master and the network bandwidth will become the bottleneck.
The advantages of this structure are: simple, flexible, and able to meet the needs of most applications.

2. Master master replication (master-master)

The two servers of the master-master replication architecture are both the master and the slave of the other server. In this way, the changes made by either party will be replicated to the other party's database. The master-master replication architecture is shown in the following figure:
Insert picture description here
The biggest problem with the master-master replication architecture is update conflicts. Assuming that a table has only one row and one column of data, its value is 1. If two servers execute the following statements
at the same time: execute on the first server:
mysql> UPDATE tbl SET col=col + 1;
execute on the second server :
Mysql> UPDATE tbl SET col=col * 2;
So what is the result? One server is 4 and the other server is 3. However, this does not produce an error.

3. Cascade replication architecture (Master-Slaves-Slaves)

In some application scenarios, the reading and writing pressure may be quite different, and the reading pressure is particularly high. A Master may need 10 or more slaves to support the reading pressure. At this time, the Master will be more strenuous, because there are more Slave IO threads connected only, so when the pressure of writing is slightly greater, the Master will consume more resources due to replication, which can easily cause replication delays. Time.

In this case, we can use MySQL to record the Binary Log information of the changes generated by the replication on the Slave side, that is, turn on the log-slave-update option. Then, secondary replication is used to reduce the pressure on the Master side due to replication. The cascading replication architecture is shown in the following figure:
Insert picture description here

4. Master-Master with Slaves with slave server

The master-master replication architecture with slave servers is shown in the figure below:
Insert picture description here

Second, the construction of cascading replication architecture

1. System environment

MySQL version: MySQL5.7

Linux version: CentOS7.0

Master address: 192.168.1.11

The address of the slave database (slave, which serves as the master of 13): 192.168.1.12

Slave address: 192.168.1.13

2. The master-slave construction between 192.168.1.11 and 192.168.1.12

See the previous article: MySQL master-slave replication (2)-the construction of a master-slave architecture (https://blog.csdn.net/weixin_44377973/article/details/107136733).

3. Master-slave construction between 192.168.1.12 and 192.168.1.13

1. Configure the 192.168.1.12 server

(1) Add the following content in the configuration file

[mysqld]
log-bin=mysql-bin
logbin_format=mixed
####  把relay log中的记录写入binlog日志
log_slave_updates=1

(2) Create a copy user

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

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

mysql> select host,user,authentication_string from mysql.user;
+-------------+---------------+-------------------------------------------+
| host        | user          | authentication_string                     |
+-------------+---------------+-------------------------------------------+
| localhost   | root          | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| localhost   | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost   | mysql.sys     | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| %           | wgx           | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| 192.168.1.% | repl          | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-------------+---------------+-------------------------------------------+
5 rows in set (0.02 sec)

(3) Export the data in the database and record the log file and location

Step 1. Before exporting the database, lock the database to prevent any write operations to the database.

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.01 sec)

mysql> use hist;
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> drop table t1;
ERROR 1223 (HY000): Can't execute the query because you have a conflicting read lock

step2, export all database data in the main library

[root@Mysql11 ~]# mysqldump -uroot -p123456 --all-databases --master-data=2 --events > /tmp/all_bak.sql

step3, copy the backup master database data to the slave database

[root@Mysql11 ~]# scp /tmp/all_bak.sql 192.168.1.13:/tmp/
[email protected]'s password: 
all_bak.sql                                          100%  834KB 833.7KB/s   00:00    
[root@Mysql11 ~]# 

step4, unlock, view binary log information

mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

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

2. Configure the 192.168.1.13 server

(1) Add the following content in the configuration file

[mysqld]
server-id=3
relay-log = mysql-relay-log

(2) After the configuration is complete, restart the mysql service and check the relay log file information:

[root@localhost ~]# ll /var/lib/mysql/mysql-relay*
-rw-r-----. 1 mysql mysql 207 7月   5 21:31 /var/lib/mysql/mysql-relay-log.000001
-rw-r-----. 1 mysql mysql 320 7月   5 21:31 /var/lib/mysql/mysql-relay-log.000002
-rw-r-----. 1 mysql mysql  76 7月   5 21:31 /var/lib/mysql/mysql-relay-log.index

(3) Import the data on the main server

[root@localhost ~]# mysql -uroot -p123456 < /tmp/all_bak.sql 

(4) Configure master-slave synchronization parameters

Execute the following commands on the slave server:

change master to 
master_host='192.168.1.12',
master_port=3306,
master_user='repl',
master_password='123456',
master_log_file='mysql-bin.000005',
master_log_pos=866214;

(5) Open the service from the library (start IO and SQL threads)

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

(6) View the status of master-slave replication

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.12
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 866214
               Relay_Log_File: mysql-relay-log.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000005
             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: 866214
              Relay_Log_Space: 527
              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: 2
                  Master_UUID: 800e4f85-2e00-11ea-a7ce-000c29cd5d3e
             Master_Info_File: /var/lib/mysql/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)

Fourth, test the working status of master-slave replication

1. Create a database on the 192.168.1.11 server, add a table, and enter data

mysql> create database mydb;
Query OK, 1 row affected (0.04 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hist               |
| mydb               |
| mysql              |
| performance_schema |
| sys                |
| wanggx             |
+--------------------+
7 rows in set (0.00 sec)

mysql> use mydb;
Database changed
mysql> create table goods(id int primary key auto_increment,name char(20));
Query OK, 0 rows affected (0.28 sec)

mysql> insert into goods(name) values('mobile');
Query OK, 1 row affected (0.09 sec)

mysql> insert into goods(name) values('computer');
Query OK, 1 row affected (0.03 sec)

mysql> 
mysql> select * from goods;
+----+----------+
| id | name     |
+----+----------+
|  1 | mobile   |
|  2 | computer |
+----+----------+
2 rows in set (0.00 sec)

2. View the data of the 192.168.1.12 slave library

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hist               |
| mydb               |
| mysql              |
| performance_schema |
| sys                |
| wanggx             |
+--------------------+
7 rows in set (0.00 sec)

mysql> 
mysql> use mydb;
Database changed
mysql> select * from goods;
+----+----------+
| id | name     |
+----+----------+
|  1 | mobile   |
|  2 | computer |
+----+----------+
2 rows in set (0.00 sec)

3. View the data of the 192.168.1.13 slave library

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hist               |
| mydb               |
| mysql              |
| performance_schema |
| sys                |
| wanggx             |
+--------------------+
7 rows in set (0.00 sec)

mysql> use mydb;
Database changed
mysql> select * from goods;
+----+----------+
| id | name     |
+----+----------+
|  1 | mobile   |
|  2 | computer |
+----+----------+
2 rows in set (0.00 sec)

4. Delete mydb on 192.168.1.11

mysql> drop database mydb;
Query OK, 1 row affected (0.58 sec)

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

5. View the data on 192.168.1.12

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

6. View the data on 192.168.1.13

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

Guess you like

Origin blog.csdn.net/weixin_44377973/article/details/107145637