MySQL主從複製(4)——級聯複製架構的搭建

MySQL主從複製(4)——級聯複製架構的搭建

MySQL 的複製功能是構建大型,高性能應用程序的基礎。將 MySQL 的數據分佈到多個系統,這種分佈機制是通過將MySQL 的某一臺主機(master)的數據複製到其它主機(slaves)上,並重新執行一遍來實現的。複製過程中一個服務器充當主服務器,而一個或多個其它服務器充當從服務器。

一、MySQL常用的主從複製架構

複製的體系結構需要遵循以下基本原則:
(1)每個 slave 只能有一個 master;
(2)每個 slave 有一個唯一的服務器ID(server-id);
(3)每個 master 可以有一個或很多 slave;
(4)通過設置 log_slave_updates,一個 slave 可以成爲其它 slave 的 master。

MySQL常用的主從複製架構如下:

1、一主一從或一主多從

一主一從複製架構是由一個 master 和一個 slave 組成複製系統。一主多從複製架構是由一個 master 和多個 slave 組成,Slave之間並不相互通信,只能與master進行通信。在實際應用場景中,MySQL 複製90%以上都是一個 Master 複製到一個或者多個 Slave 的架構模式,主要用於讀壓力比較大的應用。一主多從複製架構如下圖所示:
在這裏插入圖片描述

這種結構適合於寫操作較少,而讀操作很多。可以將讀操作分佈到其它的 slave,從而減小 master 的壓力。但是,當slave 增加到一定數量時,slave 對 master 的負載以及網絡帶寬會成爲瓶頸。
這種結構的優點是:簡單、靈活,能夠滿足大多數應用的需求。

2、主主複製(master-master)

主主複製架構的兩臺服務器,既是 master,同時又是另一臺服務器的 slave。 這樣,任何一方所做的變更,都會通過複製應用到另外一方的數據庫中。主主複製架構如下圖所示:
在這裏插入圖片描述
主主複製架構最大的問題就是更新衝突。假設一個表只有一行一列的數據,其值爲1,如果兩個服務器分別同時執行如下語句:
在第一個服務器上執行:
mysql> UPDATE tbl SET col=col + 1;
在第二個服務器上執行:
mysql> UPDATE tbl SET col=col * 2;
那麼結果是多少呢?一臺服務器是4,另一個服務器是3,但是,這並不會產生錯誤。

3、級聯複製架構(Master –Slaves - Slaves)

在某些應用場景中,可能讀寫壓力差別比較大,讀壓力特別的大,一個 Master 可能需要上10臺甚至更多的 Slave 才能支撐讀的壓力。這時候,Master 就會比較喫力了,因爲僅僅連上來的 Slave IO 線程就比較多,這樣寫的壓力稍微大一點的時候,Master 端因爲複製就會消耗較多的資源,很容易造成複製的延時。

遇到這種情況,我們能夠利用 MySQL 可以在 Slave 端記錄複製所產生變更的 Binary Log 信息的功能,也就是打開 log-slave-update 選項。然後,通過二級複製來減少 Master 端因爲複製所帶來的壓力。級聯複製架構如下圖所示:
在這裏插入圖片描述

4、帶從服務器的主主複製架構(Master-Master with Slaves)

帶從服務器的主主複製架構如下圖所示:
在這裏插入圖片描述

二、級聯複製架構的搭建

1、系統環境

MySQL版本:MySQL5.7

Linux版本:CentOS7.0

主數據庫(master)地址:192.168.1.11

從數據庫(slave,該數據庫又作爲13的 master)地址:192.168.1.12

從數據庫(slave)地址:192.168.1.13

2、192.168.1.11 和 192.168.1.12 之間的主從搭建

參見上一篇文章:MySQL主從複製(2)——一主多從架構的搭建(https://blog.csdn.net/weixin_44377973/article/details/107136733)。

三、192.168.1.12 和 192.168.1.13 之間的主從搭建

1、配置 192.168.1.12 服務器

(1)在配置文件中添加如下內容

[mysqld]
log-bin=mysql-bin
logbin_format=mixed
####  把relay log中的記錄寫入binlog日誌
log_slave_updates=1

(2)創建複製用戶

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)導出數據庫中的數據並記錄日誌文件及位置

step1、導出數據庫之前,給數據庫加鎖,阻止對數據庫進行任何的寫操作。

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、導出主庫中的所有數據庫數據

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

step3、把備份的主庫數據複製到從庫

[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、解鎖,查看二進制日誌信息

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、配置 192.168.1.13 服務器

(1)在配置文件中添加如下內容

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

(2)配置完成後重啓mysql服務,查看relay log文件信息:

[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)導入主服務器上的數據

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

(4)配置主從同步參數

在從服務器上執行如下命令:

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)開啓從庫的服務(啓動IO和SQL線程)

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

(6)查看主從複製的狀態

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)

四、測試主從複製的工作狀態

1、在192.168.1.11服務器上創建一個數據庫,並添加一張表,輸入數據

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、查看192.168.1.12從庫的數據

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、查看192.168.1.13從庫的數據

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、在192.168.1.11上刪除mydb

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、查看192.168.1.12上的數據

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

6、查看192.168.1.13上的數據

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hist               |
| mysql              |
| performance_schema |
| sys                |
| wanggx             |
+--------------------+
6 rows in set (0.00 sec)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章