1. 程式人生 > >群組複製-單主模式實戰篇

群組複製-單主模式實戰篇

自定義jdbc客戶端相容群組模式
直接進入正題
下載mysql5.7+,進入安裝目錄

[[email protected] mysql]# cd /usr/local/mysql

[[email protected] mysql]# ll
總用量 48
drwxr-xr-x.  2 mysql mysql  4096 11月  2 09:59 bin
-rw-r--r--.  1 mysql mysql 17987 11月  2 09:59 COPYING
drwxr-xr-x.  5 mysql mysql    36 11月  2 14:12 data
drwxr-xr-x.  2 mysql mysql    55 11月  2 09:59 docs
drwxr-xr-x.  3 mysql mysql  4096 11月  2 09:59 include
drwxr-xr-x.  5 mysql mysql   230 11月  2 09:59 lib
drwxr-xr-x.  4 mysql mysql    30 11月  2 09:59 man
-rw-r--r--.  1 mysql mysql  2478 11月  2 09:59 README
srwxrwxrwx.  1 mysql mysql     0 11月  2 15:11 s1.sock
-rw-------.  1 mysql mysql     6 11月  2 15:11 s1.sock.lock
srwxrwxrwx.  1 mysql mysql     0 11月  2 15:16 s2.sock
-rw-------.  1 mysql mysql     6 11月  2 15:16 s2.sock.lock
srwxrwxrwx.  1 mysql mysql     0 11月  2 15:25 s3.sock
-rw-------.  1 mysql mysql     6 11月  2 15:25 s3.sock.lock
drwxr-xr-x. 28 mysql mysql  4096 11月  2 09:59 share
drwxr-xr-x.  2 mysql mysql    90 11月  2 09:59 support-files

[
[email protected]
mysql] mkdir data/s1 s2 s3

第一個例項(主)

初始化data資料,密碼為空

./mysqld --initialize-insecure --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/s1

建立conf配置
vi data/s1/s1.cnf

[mysqld]
bind-address=0.0.0.0
datadir=/usr/local/mysql/data/s1
port=24801
basedir=/usr/local/mysql
socket=/usr/local/mysql/s1.sock

#服務id,全域性唯一
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

#本機ip地址
report_host=192.168.32.128
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
loose-group_replication_start_on_boot=off
#群組複製屬性,特別注意:group_replication_local_address的埠並不是mysql繫結的外部埠,是group獨有的埠,用於組內節點的內部通訊。
loose-group_replication_local_address= "127.0.0.1:24901"
loose-group_replication_group_seeds= "127.0.0.1:24901,127.0.0.1:24902,127.0.0.1:24903"
#群組複製主節點引導預設關閉,在啟動後通過全域性變數動態設定
loose-group_replication_bootstrap_group=off

切換組賬戶,啟動例項

su mysql
./mysqld --defaults-file=../data/s1/s1.cnf &

ok後進入shell

./mysql -P 24801 -uroot -p -S /usr/local/mysql/s1.sock

shell下逐步執行

SET SQL_LOG_BIN=0;
CREATE USER [email protected]'%' IDENTIFIED BY 'rpl_user';
GRANT REPLICATION SLAVE ON *.* TO [email protected]'%';
FLUSH PRIVILEGES;
SET SQL_LOG_BIN=1;
CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='rpl_user' FOR CHANNEL 'group_replication_recovery';
安裝外掛
INSTALL PLUGIN group_replication SONAME 'group_replication.so';
檢視外掛
SHOW PLUGINS;

#設定主節點引導開關(單主模式只允許有一臺設定該屬性)
SET GLOBAL group_replication_bootstrap_group=ON;
START/STOP GROUP_REPLICATION;
SET GLOBAL group_replication_bootstrap_group=OFF;
檢視執行狀態
SELECT * FROM performance_schema.replication_group_members;

+---------------------------+--------------------------------------+-------------+-------------+---------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE  |
+---------------------------+--------------------------------------+-------------+-------------+---------------+
| group_replication_applier | ce9be252-2b71-11e6-b8f4-00212844f856 | myhost      |       24801 | ONLINE        |
+---------------------------+--------------------------------------+-------------+-------------+---------------+

新建測試資料
mysql> CREATE DATABASE test;
mysql> USE test;
mysql> CREATE TABLE t1 (c1 INT PRIMARY KEY, c2 TEXT NOT NULL);
mysql> INSERT INTO t1 VALUES (1, 'Luis');

mysql> SELECT * FROM t1;
+----+------+
| c1 | c2   |
+----+------+
|  1 | Luis |
+----+------+

mysql> SHOW BINLOG EVENTS;
+---------------+-----+----------------+-----------+-------------+--------------------------------------------------------------------+
| Log_name      | Pos | Event_type     | Server_id | End_log_pos | Info                                                               |
+---------------+-----+----------------+-----------+-------------+--------------------------------------------------------------------+
| binlog.000001 |   4 | Format_desc    |         1 |         123 | Server ver: 5.7.19-gr080-log, Binlog ver: 4                        |
| binlog.000001 | 123 | Previous_gtids |         1 |         150 |                                                                    |
| binlog.000001 | 150 | Gtid           |         1 |         211 | SET @@SESSION.GTID_NEXT= 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:1'  |
| binlog.000001 | 211 | Query          |         1 |         270 | BEGIN                                                              |
| binlog.000001 | 270 | View_change    |         1 |         369 | view_id=14724817264259180:1                                        |
| binlog.000001 | 369 | Query          |         1 |         434 | COMMIT                                                             |
| binlog.000001 | 434 | Gtid           |         1 |         495 | SET @@SESSION.GTID_NEXT= 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:2'  |
| binlog.000001 | 495 | Query          |         1 |         585 | CREATE DATABASE test                                               |
| binlog.000001 | 585 | Gtid           |         1 |         646 | SET @@SESSION.GTID_NEXT= 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:3'  |
| binlog.000001 | 646 | Query          |         1 |         770 | use `test`; CREATE TABLE t1 (c1 INT PRIMARY KEY, c2 TEXT NOT NULL) |
| binlog.000001 | 770 | Gtid           |         1 |         831 | SET @@SESSION.GTID_NEXT= 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:4'  |
| binlog.000001 | 831 | Query          |         1 |         899 | BEGIN                                                              |
| binlog.000001 | 899 | Table_map      |         1 |         942 | table_id: 108 (test.t1)                                            |
| binlog.000001 | 942 | Write_rows     |         1 |         984 | table_id: 108 flags: STMT_END_F                                    |
| binlog.000001 | 984 | Xid            |         1 |        1011 | COMMIT /* xid=38 */                                                |
+---------------+-----+----------------+-----------+-------------+--------------------------------------------------------------------+

第一個例項(主)部署完成,等待後續節點加入

第二個cnf(過程同第一個)

[mysqld]
datadir=/usr/local/mysql/data/s2
port=24802
basedir=/usr/local/mysql
socket=/usr/local/mysql/s2.sock

server_id=2
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

report_host=192.168.32.128
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "127.0.0.1:24902"
loose-group_replication_group_seeds= "127.0.0.1:24901,127.0.0.1:24902,127.0.0.1:24903"
loose-group_replication_bootstrap_group= off

啟動完成後進入shell,依次新增

SET SQL_LOG_BIN=0;
CREATE USER [email protected]'%' IDENTIFIED BY 'password';
GRANT REPLICATION SLAVE ON *.* TO [email protected]'%';
SET SQL_LOG_BIN=1;
CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='rpl_user' FOR CHANNEL 'group_replication_recovery';
安裝外掛	
INSTALL PLUGIN group_replication SONAME 'group_replication.so';
啟動外掛
START GROUP_REPLICATION;

mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+---------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE  |
+---------------------------+--------------------------------------+-------------+-------------+---------------+
| group_replication_applier | 395409e1-6dfa-11e6-970b-00212844f856 | myhost      |       24801 | ONLINE        |
| group_replication_applier | ac39f1e6-6dfa-11e6-a69d-00212844f856 | myhost      |       24802 | ONLINE        |
+---------------------------+--------------------------------------+-------------+-------------+---------------+

第三個例項(過程同第一個)

[mysqld]
datadir=/usr/local/mysql/data/s3
port=24803
basedir=/usr/local/mysql
socket=/usr/local/mysql/s3.sock

server_id=3
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

report_host=192.168.32.128

#
### Group Replication configuration
#
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "127.0.0.1:24903"
loose-group_replication_group_seeds= "127.0.0.1:24901,127.0.0.1:24902,127.0.0.1:24903"
loose-group_replication_bootstrap_group= off

啟動完成後進入shell,依次新增

SET SQL_LOG_BIN=0;
CREATE USER [email protected]'%' IDENTIFIED BY 'password';
GRANT REPLICATION SLAVE ON *.* TO [email protected]'%';
SET SQL_LOG_BIN=1;
CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='rpl_user' FOR CHANNEL 'group_replication_recovery';
安裝外掛	
INSTALL PLUGIN group_replication SONAME 'group_replication.so';
啟動外掛
START GROUP_REPLICATION;

mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+---------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE  |
+---------------------------+--------------------------------------+-------------+-------------+---------------+
| group_replication_applier | 395409e1-6dfa-11e6-970b-00212844f856 | myhost      |       24801 | ONLINE        |
| group_replication_applier | 7eb217ff-6df3-11e6-966c-00212844f856 | myhost      |       24803 | ONLINE        |
| group_replication_applier | ac39f1e6-6dfa-11e6-a69d-00212844f856 | myhost      |       24802 | ONLINE        |
+---------------------------+--------------------------------------+-------------+-------------+---------------+

至此單主模式(三個節點)部署完成

各例項分配root帳號

CREATE USER [email protected]'%' IDENTIFIED BY 'root';
GRANT all ON *.* TO [email protected]'%';

各例項都可以執行查詢當前主資訊:

SELECT * 
FROM performance_schema.replication_group_members WHERE MEMBER_ID = 
(SELECT VARIABLE_VALUE FROM performance_schema.global_status WHERE VARIABLE_NAME= 'group_replication_primary_member');