mysql的全同步復制


介紹:

Mysql的復制方法有三種:

基於行,基於sql語句,基於混合部署,還有一種就是GTIDS

Mysql的主從復制方式有三種:

同步、異步、半同步

全同步復制:在主節點上寫入的數據,在從服務器上都同步完了以后才會給客戶端返回成功消息,相對來說比較安全,比較靠譜。但是返回信息的時間比較慢

 

異步復制:在主節點接收到客戶端發送的數據就給客戶端返回執行成功的消息。然后再開始再從上面同步,不太靠譜,因為如果我給你返回消息以后,但是我的主節點壞了,並沒有在從節點上同步完成,數據就會丟失,就算給客戶端返回成功消息,但是我執行是不成功的(mysql默認使用異步復制)

 

半同步復制:在接收到客戶端發送的數據,主節點會將數據同步到至少一台從節點以后再給客戶端發送執行成功的消息,這個以前是沒有的,是谷歌貢獻的一個插件才可以做,它相當於是同步和異步的一個中和的復制方式。

 

MySQL全同步復制:

 

實驗環境:沒有主從之分,全都是一樣的。

主機名

功能

Server1:192.168.200.101

組成員

Server2:192.168.200.102

組成員

Server3:192.168.200.103

組成員

 

實驗步驟:

  1. 安裝mysql:(三台全裝,單台做演示)(因為mariadb版本太低,用mysql做)

 

  1. 為了我們數據保持一致性,我們需要將/etc/my.cnf中datadir所指定的mysql數據全部清空,保持三台數據的一致性(三台全做,單台演示)(如果是新安裝的就不需要清除數據)

[root@server1 ~]# cd /usr/local/mysql/data

[root@server1 mysql]# rm -rf *

[root@server1 mysql]#/etc/init.d/mysqld start

  1. 配置/etc/my.cnf文件

配置server1:

.首先關閉數據庫,刪除/usr/local/mysql/data所有文件,編輯/etc/my.cnf,開啟數據庫,進行安全檢測更改密碼

 

[mysqld]

datadir=/usr/local/mysql/data

socket=/tmp/mysql.sock

server_id=1

gtid_mode=ON

enforce_gtid_consistency=ON

master_info_repository=TABLE

relay_log_info_repository=TABLE

binlog_checksum=NONE

log_slave_updates=ON

log_bin=binlog

binlog_format=ROW

 

transaction_write_set_extraction=XXHASH64

loose-group_replication_group_name="df8e2d2f-d5aa-4200-9a55-4314739974d1"

loose-group_replication_start_on_boot=off

loose-group_replication_local_address="192.168.200.101:24901"

loose-group_replication_group_seeds="192.168.200.101:24901,192.168.200.102:24901,192.168.200.103:24901"

loose-group_replication_bootstrap_group=off

loose-group_replication_single_primary_mode=off

loose-group_replication_enforce_update_everywhere_checks=on

loose-group_replication_ip_whitelist="192.168.200.0/24,127.0.0.1/8"

參數說明:

 

server_id=1        #這些設置將server配置為使用唯一標識號,以啟動全局事物標示符號

gtid_mode=ON        #打開gtid保證半同步

enforce_gtid_consistency=ON    #強制持續打開

master_info_repository=TABLE    #master的信息存放在系統表中,而不是文件中

relay_log_info_repository=TABLE #io從主獲取的信息

binlog_checksum=NONE         #它設置 server 打開二進制日志記錄,使用基於行的格式並禁

用二進制日志事件校驗和

log_slave_updates=ON        

log_bin=binlog

binlog_format=ROW        #二進制文件格式

 

#組復制設置

#loose-如果在 server 啟動時尚未加載組復制插件,上面 group_replication 變量使用的 loose- 前綴將指示 server 繼續啟動

transaction_write_set_extraction=XXHASH64 #server必須為每個事務收集寫集合,並使用 XXHASH64 哈希算法將其編碼為散列。

loose-group_replication_group_name="3ec5f888-1a7d-4754-802c-d9a122ee1ad1"#告知插件,正在加入或創建的組要命名為3ec5f888-1a7d-4754-802c-d9a122ee1ad1“(uuid號)可以uuidgen查看”

loose-group_replication_start_on_boot=off #server 啟動時不自動啟動組復制。

loose-group_replication_local_address= "192.168.200.101:24901" #插件使用的ip地址,端口用於接受來自組中其他成員的傳入連接

loose-group_replication_group_seeds="192.168.200.101:24901,192.168.200.102:24902,192.168.200.103:24903"#種子成員

loose-group_replication_bootstrap_group= off  #插件是否自動引導,注意此選項在任何時候只能在一個 server 實例上使用,通常是首次引導組時(或在整個組被崩潰然后恢復的情況下)。 如果您多次引導組,例如,當多個 server 實例設置了此選項,則它們可能會人為地造成腦裂的情況,其中存在兩個具有相同名稱的不同組。 在第一個server 實例加入組后禁用此選項。組中的所有 server 成員的配置都非常相似。

loose-group_replication_ip_whitelist="192.168.200.0/24" #組的白名單用戶,如果不設置則不能,加入組

loose-group_replication_enforce_update_everywhere_checks=on

loose-group_replication_single_primary_mode=off

    組復制使用異步復制協議來實現分布式恢復

 

2.server1上初始化密碼

 

grep password /var/log/mysqld.log

 

mysql_secure_installation

 

3.創建具有preplication-slave權限的mysql用戶,此操作不應該記錄到二進制日志中,以避免將更改傳遞到其他server實例上,步驟如下:創建用戶myslave,密碼為123123,配置服務器是需要使用正確的用戶名和密碼。

 

[root@server1 mysql]# mysql -u root -p123123

Enter password:

--------------

mysql> SET SQL_LOG_BIN=0;   #關閉防止之后傳到其他server

Query OK, 0 rows affected (0.00 sec)

 

mysql> GRANT REPLICATION SLAVE ON *.* TO myslave@'%' IDENTIFIED BY '123123';

Query OK, 0 rows affected, 1 warning (0.00 sec)    #創建用戶授權

 

mysql> FLUSH PRIVILEGES;  #刷新表

Query OK, 0 rows affected (0.00 sec)

 

mysql> reset master;  #刪除了二進制日志,這一步很重要,必須要做。

Query OK, 0 rows affected (0.16 sec)

4.開啟日志

 

mysql> reset master;

Query OK, 0 rows affected (0.57 sec)

 

mysql> SET SQL_LOG_BIN=1;

Query OK, 0 rows affected (0.00 sec)

 

mysql> CHANGE MASTER TO MASTER_USER='myslave', MASTER_PASSWORD='123123' FOR CHANNEL 'group_replication_recovery';

Query OK, 0 rows affected, 2 warnings (1.07 sec)

 

mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';

Query OK, 0 rows affected (0.37 sec)

 

mysql> SHOW PLUGINS;

+----------------------------+----------+--------------------+----------------------+---------+

| Name                       | Status   | Type               | Library              | License |

+----------------------------+----------+--------------------+----------------------+---------+

| binlog                     | ACTIVE   | STORAGE ENGINE     | NULL                 | GPL     |

| mysql_native_password      | ACTIVE   | AUTHENTICATION     | NULL                

| validate_password          | ACTIVE   | VALIDATE PASSWORD  | validate_password.so | GPL     |

| group_replication          | ACTIVE   | GROUP REPLICATION  | group_replication.so | GPL     |

+----------------------------+----------+--------------------+----------------------+---------+

46 rows in set (0.00 sec)

5.開啟全同步復制,創建一個表用於同步檢測

 

mysql>  SET GLOBAL group_replication_bootstrap_group=ON;

Query OK, 0 rows affected (0.00 sec)

 

mysql> START GROUP_REPLICATION;

Query OK, 0 rows affected (2.80 sec)

 

mysql> SET GLOBAL group_replication_bootstrap_group=OFF;

Query OK, 0 rows affected (0.00 sec)

 

mysql> SELECT * FROM performance_schema.replication_group_members;

+---------------------------+--------------------------------------+-------------+-------------+--------------+

| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |

+---------------------------+--------------------------------------+-------------+-------------+--------------+

| group_replication_applier | c94d76cc-d80a-11e8-8c01-5254002ac7eb | server1     |        3306 | ONLINE       |

+---------------------------+--------------------------------------+-------------+-------------+--------------+

1 row in set (0.00 sec)

 

mysql> CREATE DATABASE test;

Query OK, 1 row affected (0.05 sec)

 

mysql> use test

Database changed

mysql> CREATE TABLE t1 (c1 INT PRIMARY KEY, c2 TEXT NOT NULL);

Query OK, 0 rows affected (0.81 sec)

 

mysql> INSERT INTO t1 VALUES (1, 'Luis');

Query OK, 1 row affected (0.14 sec)

 

mysql> select * from t1;

+----+------+

| c1 | c2   |

+----+------+

|  1 | Luis |

+----+------+

1 row in set (0.00 sec)

6.server2上做類似步驟

 

/etc/my.cnf

 

server_id=2

 

loose-group_replication_local_address= "172.25.254.2:24901"

 

/etc/my.cnf

 

server_id=3

 

loose-group_replication_local_address= "172.25.254.3:24901"

 

[root@server2 mysql]# mysql -u root -p123123

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 9

Server version: 5.7.17-log MySQL Community Server (GPL)

 

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

mysql> SET SQL_LOG_BIN=0;

Query OK, 0 rows affected (0.00 sec)

 

mysql> GRANT REPLICATION SLAVE ON *.* TO myslave@'%' IDENTIFIED BY '123123';

Query OK, 0 rows affected, 1 warning (0.00 sec)

 

mysql> FLUSH PRIVILEGES;

Query OK, 0 rows affected (0.00 sec)

 

mysql> reset master;

Query OK, 0 rows affected (0.45 sec)

 

mysql> SET SQL_LOG_BIN=1;

Query OK, 0 rows affected (0.00 sec)

 

mysql> CHANGE MASTER TO MASTER_USER='myslave', MASTER_PASSWORD='123123' FOR CHANNEL 'group_replication_recovery';

Query OK, 0 rows affected, 2 warnings (1.12 sec)

 

mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';

Query OK, 0 rows affected (0.23 sec)

 

mysql> SHOW PLUGINS;

+----------------------------+----------+--------------------+----------------------+---------+

| Name                       | Status   | Type               | Library              | License |

+----------------------------+----------+--------------------+----------------------+---------+

| binlog                     | ACTIVE   | STORAGE ENGINE     | NULL                 | GPL     |

| mysql_native_password      | ACTIVE   | AUTHENTICATION     | NULL                 | GPL     |

| validate_password          | ACTIVE   | VALIDATE PASSWORD  | validate_password.so | GPL     |

| group_replication          | ACTIVE   | GROUP REPLICATION  | group_replication.so | GPL     |

+----------------------------+----------+--------------------+----------------------+---------+

46 rows in set (0.00 sec)

 

mysql> START GROUP_REPLICATION;

 

Query OK, 0 rows affected (6.98 sec)

 

mysql> SELECT * FROM performance_schema.replication_group_members;

+---------------------------+--------------------------------------+-------------+-------------+--------------+

| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |

+---------------------------+--------------------------------------+-------------+-------------+--------------+

| group_replication_applier | c352b7e2-d80e-11e8-a945-525400de467d | server2     |        3306 | RECOVERING   |

| group_replication_applier | c94d76cc-d80a-11e8-8c01-5254002ac7eb | server1     |        3306 | ONLINE       |

+---------------------------+--------------------------------------+-------------+-------------+--------------+

2 rows in set (0.00 sec)

 

mysql> +---------------------------+--------------------------------------+-

 

server3:

 

[root@server3 mysql]# mysql -uroot -p123123

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 9

Server version: 5.7.17-log MySQL Community Server (GPL)

 

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

mysql> SET SQL_LOG_BIN=0;

Query OK, 0 rows affected (0.00 sec)

 

mysql> GRANT REPLICATION SLAVE ON *.* TO myslave@'%' IDENTIFIED BY '123123';

Query OK, 0 rows affected, 1 warning (0.00 sec)

 

mysql> FLUSH PRIVILEGES;

Query OK, 0 rows affected (0.00 sec)

 

mysql> reset master;

Query OK, 0 rows affected (0.46 sec)

 

mysql> SET SQL_LOG_BIN=1;

Query OK, 0 rows affected (0.00 sec)

 

mysql> CHANGE MASTER TO MASTER_USER='myslave', MASTER_PASSWORD='123123' FOR CHANNEL 'group_replication_recovery';

Query OK, 0 rows affected, 2 warnings (0.97 sec)

 

mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';

Query OK, 0 rows affected (0.20 sec)

 

mysql> Query OK, 0 rows affected (0.59 sec)

    -> ^C

mysql>  SHOW PLUGINS;

+----------------------------+----------+--------------------+----------------------+---------+

| Name                       | Status   | Type               | Library              | License |

+----------------------------+----------+--------------------+----------------------+---------+

| binlog                     | ACTIVE   | STORAGE ENGINE     | NULL                 | GPL     |

| mysql_native_password      | ACTIVE   | AUTHENTICATION     | NULL                 | GPL     |

| sha256_password            | ACTIVE   | AUTHENTICATION     | NULL                 | GPL     |

| validate_password          | ACTIVE   | VALIDATE PASSWORD  | validate_password.so | GPL     |

| group_replication          | ACTIVE   | GROUP REPLICATION  | group_replication.so | GPL     |

+----------------------------+----------+--------------------+----------------------+---------+

46 rows in set (0.00 sec)

 

mysql>  START GROUP_REPLICATION;

Query OK, 0 rows affected (3.94 sec)

 

mysql> SELECT * FROM performance_schema.replication_group_members;

+---------------------------+--------------------------------------+-------------+-------------+--------------+

| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |

+---------------------------+--------------------------------------+-------------+-------------+--------------+

| group_replication_applier | 4c0ae162-d80f-11e8-8946-525400e512c6 | server3     |        3306 | ONLINE       |

| group_replication_applier | c352b7e2-d80e-11e8-a945-525400de467d | server2     |        3306 | ONLINE       |

| group_replication_applier | c94d76cc-d80a-11e8-8c01-5254002ac7eb | server1     |        3306 | ONLINE       |

+---------------------------+--------------------------------------+-------------+-------------+--------------+

3 rows in set (0.00 sec)

 

mysql>

7.測試


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM