MySQL Group Replication的安裝部署

1、簡介

此次給你們介紹下MySQL官方最新版本5.7.17中GA的新功能 Group Replication 。html

 

Group Replication是一種可用於實現容錯系統的技術。複製組是一組經過消息傳遞相互交互的服務器。通訊層提供一組保證,例如原子消息和總訂單消息傳遞。這些是很是強大的屬性,能夠轉化爲很是有用的抽象,人們能夠訴諸構建更高級的數據庫複製解決方案。MySQL組複製構建在這些屬性和抽象之上,並實現多主複製協議的更新。實質上,複製組由多個服務器造成,而且組中的每一個服務器能夠獨立地執行事務。可是全部讀寫(RW)事務只有在組被批准後纔會提交。只讀(RO)事務不須要在組內協調,所以當即提交。換句話說,對於任何RW事務,組須要決定是否提交,所以提交操做不是來自始發服務器的單向決定。準確地說,當事務準備好在始發服務器上提交時,服務器原子地廣播寫入值(已改變的行)和對應的寫入集(已更新的行的惟一標識符)。而後爲該交易創建全局總訂單。最終,這意味着全部服務器以相同的順序接收同一組事務。所以,全部服務器以相同的順序應用相同的一組更改,所以它們在組內保持一致。
可是,在不一樣服務器上併發執行的事務之間可能存在衝突。經過在稱爲認證的過程當中檢查兩個不一樣的併發事務的寫集合來檢測這樣的衝突。若是在不一樣的服務器上執行的兩個併發事務更新同一行,則會出現衝突。解析過程指出,首先訂購的事務在全部服務器上提交,而順序第二次停止的事務將在源服務器上回滾,並由組中的其餘服務器刪除。這其實是一個分佈式的第一個提交贏的規則。

 

MySQL組複製協議

 

 

最後,組複製是一種無共享複製方案,其中每一個服務器都有本身的整個數據副本。
上圖描述了MySQL組複製協議,並經過將其與MySQL複製(或甚至MySQL半同步複製)進行比較,您能夠看到一些差別。注意,爲了清楚起見,這個圖片中缺乏一些基本的共識和Paxos相關消息。mysql

 

介紹就到這,本文中我將一步一步的安裝部署group_replication的三個節點,並讓你看到它的功能和特性,若是看徹底文,你十分的感興趣的話,能夠去mysqlGroup Replication主頁去查看更詳細的信息。linux

 

2、正式部署

1. 環境介紹

basedir = /usr/local/mysql
(PS: 這點仍是要吐槽一下的,mysql官方basedir若是不在這個目錄的話,mysql.server也很差使,這個實驗也不成功,咱們仍是勉強先放在這)sql

端口號 數據及日誌目錄  Group_Replication通信端口
3306 /data/mysql/mysql_3306/{data,logs,tmp} 33061
3307 /data/mysql/mysql_3307/{data,logs,tmp} 33062
3308 /data/mysql/mysql_3308/{data,logs,tmp} 33063

 

 

 

 

 

2. 初始化

①、下載and解壓縮,並把mysql放到指定地方(標準目錄: /usr/local/mysql)數據庫

cd /opt/
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.26-linux-glibc2.12-x86_64 /usr/local/mysql

 

②、 建立數據庫須要的數據、日誌和臨時目錄並賦權:bootstrap

mkdir -p /data/mysql/{mysql_3306,mysql_3307,mysql_3308}/{data,logs,tmp}
chown -R mysql.mysql /data/mysql/

 

3. 初始化

安裝機器: 192.168.0.162 (單機多實例安裝)
配置文件說明:ubuntu

端口號 配置文件
3306 /data/mysql/mysql_3306/my3306.cnf
3307 /data/mysql/mysql_3306/my3307.cnf
3308 /data/mysql/mysql_3306/my3308.cnf

 

 

 

 

 
 
3306端口配置文件詳情:
 
my3306.cnf
 
[client]
port            = 3306
socket          = /tmp/mysql3306.sock
[mysql]
prompt                         = mysql [\d]>
default_character_set          = utf8
no-auto-rehash
[mysqld]
#misc
user = mysql
basedir = /usr/local/mysql
datadir = /data/mysql/mysql_3306/data
port = 3306
socket = /tmp/mysql3306.sock
event_scheduler = 0
tmpdir=/data/mysql/mysql_3306/tmp
#timeout
interactive_timeout = 43200
wait_timeout = 43200
#character set
character-set-server = utf8
open_files_limit = 65535
max_connections = 100
max_connect_errors = 100000
#
explicit_defaults_for_timestamp
#logs
log-output=file
slow_query_log = 1
slow_query_log_file = slow.log
log-error = error.log
log_error_verbosity=3
pid-file = mysql.pid
long_query_time = 1
#log-slow-admin-statements = 1
#log-queries-not-using-indexes = 1
log-slow-slave-statements = 1
#binlog
binlog_format = row
server-id = 1343306
log-bin = /data/mysql/mysql_3306/logs/mysql-bin
binlog_cache_size = 1M
max_binlog_size = 200M
max_binlog_cache_size = 2G
sync_binlog = 0
expire_logs_days = 10
 
#group replication
server_id=1013306
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
binlog_format=ROW
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="13855fca-d2ab-11e6-8f37-005056b8286c"
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "192.168.0.162:33061"
loose-group_replication_group_seeds= "192.168.0.162:33061,192.168.0.162:33071,192.168.0.162:33081"
loose-group_replication_bootstrap_group= off
loose-group_replication_single_primary_mode=off
loose-group_replication_enforce_update_everywhere_checks=on
#relay log
skip_slave_start = 1
max_relay_log_size = 500M
relay_log_purge = 1
relay_log_recovery = 1
#slave-skip-errors=1032,1053,1062
#buffers & cache
table_open_cache = 2048
table_definition_cache = 2048
table_open_cache = 2048
max_heap_table_size = 96M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 256
query_cache_size = 0
query_cache_type = 0
query_cache_limit = 256K
query_cache_min_res_unit = 512
thread_stack = 192K
tmp_table_size = 96M
key_buffer_size = 8M
read_buffer_size = 2M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 32M
#myisam
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
#innodb
innodb_buffer_pool_size = 500M
innodb_buffer_pool_instances = 1
innodb_data_file_path = ibdata1:100M:autoextend
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 64M
innodb_log_file_size = 256M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_file_per_table = 1
innodb_rollback_on_timeout
innodb_status_file = 1
innodb_io_capacity = 2000
transaction_isolation = READ-COMMITTED
innodb_flush_method = O_DIRECT
View Code

 

3307和3308端口的配置文件我不貼的,替換下端口便可。
其中比較重要的地方,也是安裝Group_replication必需要有的配置項,須要注意一下:
 
#group replication
server_id=1013306
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
binlog_format=ROW
 
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="13855fca-d2ab-11e6-8f37-005056b8286c"
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "192.168.0.162:33061"
loose-group_replication_group_seeds= "192.168.0.162:33061,192.168.0.162:33071,192.168.0.162:33081"
loose-group_replication_bootstrap_group= off
 
loose-group_replication_single_primary_mode=off
loose-group_replication_enforce_update_everywhere_checks=on
View Code

 

在這裏有一些技術細節要說明一下:vim

上面的三個配置文件省略了全部沒必要要的配置項、可是看起來仍是有點多、這些都是mgr環境要求的。服務器

server_id 每一個實例都要不要樣併發

loose-group_replication_group_name:爲mgr高可用組起一個名字,必須是有效的UUID。在二進制日誌中爲組複製事件設置GTID時,將在內部使用此UUID。使用SELECT UUID()生成一個UUID。

loose-group_replication_local_address:mgr各實例以前都是要進行通訊的、這個配置項設置的就是本實例所監聽的ip:端口

loose-group_replication_group_seeds:各mgr實例所監聽的ip:端口信息

 

其餘配置參數,請參考官方文檔:

https://dev.mysql.com/doc/refman/5.7/en/group-replication-configuring-instances.html

 

4. 啓動Group_replication第一個節點

初始化3306實例:

/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql_3306/my3306.cnf --initialize-insecure

 

啓動3306實例:

/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql_3306/my3306.cnf &

 

進入mysql進行change操做:

/usr/local/mysql/bin/mysql -u root -S /tmp/mysql3306.sock
mysql> SET SQL_LOG_BIN=0;
mysql> CREATE USER rpl_user@'%';
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%' IDENTIFIED BY 'rpl_pass';
mysql> SET SQL_LOG_BIN=1;
mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='rpl_pass' FOR CHANNEL 'group_replication_recovery';

 

加載 group_replication的plugin:
mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
mysql> SHOW PLUGINS;
+----------------------------+----------+--------------------+----------------------+---------+
| Name                       | Status   | Type               | Library              | License |
+----------------------------+----------+--------------------+----------------------+---------+
| binlog                     | ACTIVE   | STORAGE ENGINE     | NULL                 | GPL     |
| mysql_native_password      | ACTIVE   | AUTHENTICATION     | NULL                 | GPL     |
|... ... ...                                                                                  |
| group_replication          | ACTIVE   | GROUP REPLICATION  | group_replication.so | GPL     |
+----------------------------+----------+--------------------+----------------------+---------+

 

啓動第一個節點的Group_replication:
mysql> SET GLOBAL group_replication_bootstrap_group=ON;     #只在第一個節點使用
mysql> START GROUP_REPLICATION;

 

確認節點加入狀況:

mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-----------------------+-------------+--------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST           | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-----------------------+-------------+--------------+
| group_replication_applier | 68ce93ca-d292-11e6-bdf9-005056b8286c | localhost.localdomain |        3306 | ONLINE       |
+---------------------------+--------------------------------------+-----------------------+-------------+--------------+
1 rows in set (0.00 sec)

 

建立測試數據:
mysql> create database boom;
mysql> use boom;
mysql> create table boomballa(id int not null,name varchar(32),primary key(id));
mysql> insert into boomballa(id,name) values(1,'boomballa.top');
mysql> insert into boomballa(id,name) values(2,'myblog');

 

查看端口是否啓動

root@ubuntu:/data/mysql/mysql_3306# netstat -anpt | grep 33061
tcp        0      0 0.0.0.0:33061           0.0.0.0:*               LISTEN      1897/mysqld

 

5. 啓動Group_replication第二個節點

複製配置文件:

# 複製配置文件
cd /data/mysql/mysql_3307/
cp ../mysql_3306/my3306.cnf my3307.cnf
 
# 編輯配置文件
vim my3307.cnf
 
#將替換3306爲3307

 

 
特別注意如下2行:
loose-group_replication_local_address= "192.168.0.162:33071"
loose-group_replication_group_seeds= "192.168.0.162:33061,192.168.0.162:33071,192.168.0.162:33081"

 

注意:loose-group_replication_local_address配置參數,必需要在loose-group_replication_group_seeds裏面!

不然會出現3092錯誤

 

初始化並啓動實例:

/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql_3307/my3307.cnf --initialize-insecure
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql_3307/my3307.cnf &

 

安裝插件並啓動Group_replication:
mysql> SET SQL_LOG_BIN=0;
mysql> CREATE USER rpl_user@'%';
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%' IDENTIFIED BY 'rpl_pass';
mysql> SET SQL_LOG_BIN=1;
mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='rpl_pass' FOR CHANNEL 'group_replication_recovery';
mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
mysql> START GROUP_REPLICATION;
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-----------------------+-------------+--------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST           | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-----------------------+-------------+--------------+
| group_replication_applier | 5aaa8529-d296-11e6-a7be-005056b8286c | localhost.localdomain |        3307 | ONLINE       |
| group_replication_applier | 68ce93ca-d292-11e6-bdf9-005056b8286c | localhost.localdomain |        3306 | ONLINE       |
+---------------------------+--------------------------------------+-----------------------+-------------+--------------+
2 rows in set (0.00 sec)

 

查看端口是否啓動
root@ubuntu:/data/mysql/mysql_3306# netstat -anpt | grep 33061
tcp        0      0 0.0.0.0:33071           0.0.0.0:*               LISTEN      1950/mysqld

 

6. 第三節點安裝配置

這裏省略了,參考第二個節點。注意:替換端口爲3308

安裝好了之後的狀態應該是(三節點上查詢結果都是如此):

mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-----------------------+-------------+--------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST           | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-----------------------+-------------+--------------+
| group_replication_applier | 5aaa8529-d296-11e6-a7be-005056b8286c | localhost.localdomain |        3307 | ONLINE       |
| group_replication_applier | 68ce93ca-d292-11e6-bdf9-005056b8286c | localhost.localdomain |        3306 | ONLINE       |
| group_replication_applier | af93afd1-d297-11e6-b8e9-005056b8286c | localhost.localdomain |        3308 | ONLINE       |
+---------------------------+--------------------------------------+-----------------------+-------------+--------------+
3 rows in set (0.00 sec)
 
mysql> use boom
Database changed
mysql> select * from boomballa;
+----+---------------+
| id | name          |
+----+---------------+
|  1 | boomballa.top |
|  2 | myblog        |
+----+---------------+
2 rows in set (0.00 sec)

 

確認一下:

[root@localhost ~]# echo "select * from boom.boomballa;"|/usr/local/mysql/bin/mysql -S /tmp/mysql3306.sock
id      name
1       boomballa.top
2       myblog
[root@localhost ~]# echo "select * from boom.boomballa;"|/usr/local/mysql/bin/mysql -S /tmp/mysql3307.sock
id      name
1       boomballa.top
2       myblog
[root@localhost ~]# echo "select * from boom.boomballa;"|/usr/local/mysql/bin/mysql -S /tmp/mysql3308.sock
id      name
1       boomballa.top
2       myblog

 

3、測試

查看mysql進程

root@ubuntu:/data/mysql/mysql_3306# ps -aux | grep mysql
mysql     1897  0.1  6.3 1718160 255244 pts/0  Sl   16:06   0:08 /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql_3306/my3306.cnf
mysql     1950  0.1  6.2 1705688 252724 pts/0  Sl   16:07   0:08 /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql_3307/my3307.cnf
mysql     2147  0.2  6.2 1709152 251420 pts/1  Sl   16:25   0:09 /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql_3308/my3308.cnf
root      2228  0.0  0.0  14224   928 pts/1    S+   17:35   0:00 grep --color=auto mysql

 

設置第一個mysql實例密碼,設置密碼爲root

/usr/local/mysql/bin/mysqladmin -u root -S /tmp/mysql3306.sock -p password root

 

受權root用戶遠程鏈接

/usr/local/mysql/bin/mysql -u root -S /tmp/mysql3306.sock -proot
mysql> grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;
mysql> flush privileges;

 

使用Navicat 客戶端查看數據

 

 

測試其餘端口,好比:3307和3308,也應該是能夠查看錶數據!

 

在3306修改表數據,在3307查看數據,會當即同步的

 

 

 關閉其中一個節點

關閉3308的進程

kill 2147

 

 

再次查看3306和3307表數據,是ok的。

在3306修改表數據,3307也是會同步的

 

啓動3308實例

/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql_3308/my3308.cnf &

 

查看3308數據,發現數據沒有更新過來

 

 

進入到3308,開啓GROUP_REPLICATION

/usr/local/mysql/bin/mysql -u root -S /tmp/mysql3308.sock -proot
mysql> START GROUP_REPLICATION;

 

再次刷新數據,發現數據同步過來了

 

 

大功告成!

 

本文參考連接:

https://www.jianshu.com/p/dc64fee1c792

相關文章
相關標籤/搜索