什么是MGP:
MySQL 组复制提供了高可用、高扩展、
高可靠的 MySQL 集群服务。高一致性,基于原生复制及 paxos 协议的组复制技术,
并以插件的方式提供,提供一致数据安全保证;高容错性,只要不是大多数节点
坏掉就可以继续工作,有自动检测机制,当不同节点产生资源争用冲突时,不会
出现错误,按照先到者优先原则进行处理,并且内置了自动化脑裂防护机制;高
扩展性,节点的新增和移除都是自动的,新节点加入后,会自动从其他节点上同
步状态,直到新节点和其他节点保持一致,如果某节点被移除了,其他节点自动
更新组信息,自动维护新的组信息;高灵活性,有单主模式和多主模式,单主模
式下,会自动选主,所有更新操作都在主上进行;多主模式下,所有 server 都
可以同时处理更新操作。
实验环境使用了全新的虚拟机:
server1:172.25.254.1
server2:172.25.254.2
server3:172.25.254.3
在三个节点均安装mysql相关安装包,打开mysqld:
安装包:
mysql-community-client-5.7.17-1.el6.x86_64.rpm
mysql-community-common-5.7.17-1.el6.x86_64.rpm
mysql-community-libs-5.7.17-1.el6.x86_64.rpm
mysql-community-libs-compat-5.7.17-1.el6.x86_64.rpm
mysql-community-server-5.7.17-1.el6.x86_64.rpm
三个节点均做搭建最初实验环境:
[root@server1 ~]# /etc/init.d/mysqld start
Initializing MySQL database: [ OK ]
Installing validate password plugin: [ OK ]
Starting mysqld: [ OK ]
在server1:
[root@server1 ~]# /etc/init.d/mysqld stop
Stopping mysqld: [ OK ]
[root@server1 ~]# cd /var/lib/mysql
[root@server1 mysql]# ls
auto.cnf client-key.pem ib_logfile1 public_key.pem
ca-key.pem ib_buffer_pool mysql server-cert.pem
ca.pem ibdata1 performance_schema server-key.pem
client-cert.pem ib_logfile0 private_key.pem sys
[root@server1 mysql]# rm -fr *
[root@server1 mysql]# vim /etc/my.cnf
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
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
指示 server 必须为每个事务收集写集合,并使用 XXHASH64 哈希算法将其编码为散列。
loose-group_replication_group_name="ae6473bb-9cad-11e8-8abc-52540089f226"
告知插件,正在加入或创建的组要命名为
loose-group_replication_start_on_boot=off
指示插件在 server 启动时不自动启动组复制。
loose-group_replication_local_address= "172.25.254.1:24901"
告诉插件使用 IP 地址 127.0.0.1 或本地主机,端口 24901 用于接受来自组中其他成员的传入连接。
loose-group_replication_group_seeds="172.25.254.1:24901,172.25.254.2:24901,172.25.254.3: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="172.25.254.0/24,127.0.0.1/8"
UUID在任何一个虚拟机查看即可
[root@server1 mysql]# /etc/init.d/mysqld start
Initializing MySQL database: [ OK ]
Installing validate password plugin: [ OK ]
Starting mysqld: [ OK ]
[root@server1 mysql]# grep password /var/log/mysqld.log 查看密码
2018-08-10T14:59:24.605148Z 1 [Note] A temporary password is generated for root@localhost: W7zPaE7qr9<e
[root@server1 mysql]# mysql -p
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> alter user root@localhost identified by 'Aa@+123456'; 必须更改密码才可以进行操作数据库
Query OK, 0 rows affected (0.12 sec)
mysql>
mysql> SET SQL_LOG_BIN=0;
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%' IDENTIFIED BY 'Aa@+123456'; 授权
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges; 刷新
Query OK, 0 rows affected (0.00 sec)
mysql> reset master; 删除master信息日志
Query OK, 0 rows affected (0.73 sec)
mysql> SET SQL_LOG_BIN=1;
Query OK, 0 rows affected (0.00 sec)
mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='Aa@+123456' FOR CHANNEL 'group_replication_recovery'; 连接
Query OK, 0 rows affected, 2 warnings (1.66 sec)
mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so'; 安装插件
Query OK, 0 rows affected (0.29 sec)
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.95 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 | f84fdfe5-9cad-11e8-a395-5254009ac5cd | server1 | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
1 row in set (0.00 sec)
mysql> create database test; 建立数据库插入字段进行测试数据同步
Query OK, 1 row affected (0.22 sec)
mysql> use test
Database changed
mysql> create table t1 (c1 int primary key, c2 text not null);
Query OK, 0 rows affected (1.56 sec)
mysql> select * from t1;
Empty set (0.01 sec)
mysql> insert into t1 values('1','lurci')
-> ;
Query OK, 1 row affected (0.24 sec)
mysql> select * from t1;
+----+-------+
| c1 | c2 |
+----+-------+
| 1 | lurci |
+----+-------+
1 row in set (0.00 sec)
在server2,id号和本地IP不同即可,其他的均和server1相同:
[root@server2 ~]# vim /etc/my.cnf
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
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
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="ae6473bb-9cad-11e8-8abc-52540089f226"
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "172.25.254.2:24901"
loose-group_replication_group_seeds="172.25.254.1:24901,172.25.254.2:24901,172.25.254.3: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="172.25.254.0/24,127.0.0.1/8"
[root@server2 ~]# /etc/init.d/mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
[root@server2 ~]# mysql -pAa@+123456 已经做过了初始化数据库
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so'; 安装插件
Query OK, 0 rows affected (0.12 sec)
mysql> set global rpl_semi_sync_slave_enabled = ON;
Query OK, 0 rows affected (0.00 sec)
mysql> set sql_log_bin=0; 不进行二进制日志记录
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on *.* to rpl_user@'%' identified by 'Aa@+123456'; 授权
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> reset master; 删除master日志信息
Query OK, 0 rows affected (0.52 sec)
mysql> set sql_log_bin=1;
Query OK, 0 rows affected (0.00 sec)
mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='Aa@+123456' FOR CHANNEL 'group_replication_recovery'; 连接
Query OK, 0 rows affected, 2 warnings (1.51 sec)
mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so'; 安装插件
Query OK, 0 rows affected (0.24 sec)
mysql> START GROUP_REPLICATION;
Query OK, 0 rows affected (7.46 sec)
mysql> select * from performance_schema.replication_group_members; 查看组成员已经为2
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 56b66e2c-9cac-11e8-816f-52540089f226 | server2 | 3306 | ONLINE |
| group_replication_applier | f84fdfe5-9cad-11e8-a395-5254009ac5cd | server1 | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
2 rows in set (0.00 sec)
mysql> use test;
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> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t1 |
+----------------+
1 row in set (0.00 sec)
mysql> select * from t1; 可以查看server1建立的数据字段信息
+----+-------+
| c1 | c2 |
+----+-------+
| 1 | lurci |
+----+-------+
1 row in set (0.00 sec)
在server3进行和server2相同的步骤:
[root@server3 ~]# vim /etc/my.cnf 除了server-id和本地IP不同
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
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
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="ae6473bb-9cad-11e8-8abc-52540089f226"
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "172.25.254.3:24901"
loose-group_replication_group_seeds="172.25.254.1:24901,172.25.254.2:24901,172.25.254.3: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="172.25.254.0/24,127.0.0.1/8"
[root@server3 ~]# /etc/init.d/mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
更改密码数据库可以登陆操作:
[root@server3 ~]# grep password /var/log/mysqld.log 查看日志获取初始化密码
2018-08-10T14:47:44.789304Z 1 [Note] A temporary password is generated for root@localhost: 62tP(MvfsNMM
[root@server3 ~]# mysql -p
mysql> show databases; 不更改密码不能进行操作数据库
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> alter user root@localhost identified by 'Aa@+123456'; 修改密码
Query OK, 0 rows affected (0.11 sec)
mysql> set global rpl_semi_sync_slave_enabled = ON;
ERROR 1193 (HY000): Unknown system variable 'rpl_semi_sync_slave_enabled'
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so'; 安装插件
Query OK, 0 rows affected (0.20 sec)
mysql> set global rpl_semi_sync_slave_enabled = ON;
Query OK, 0 rows affected (0.00 sec)
mysql> set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on *.* to rpl_user@'%' identified by 'Aa@+123456'; 授权
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> reset master; 删除master日志信息
Query OK, 0 rows affected (0.66 sec)
mysql> set sql_log_bin=1;
Query OK, 0 rows affected (0.00 sec)
mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='Aa@+123456' FOR CHANNEL 'group_replication_recovery'; 连接
Query OK, 0 rows affected, 2 warnings (1.42 sec)
mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';安装插件
Query OK, 0 rows affected (0.24 sec)
mysql> START GROUP_REPLICATION;
Query OK, 0 rows affected (4.07 sec)
三个节点已经可以连接:
mysql> select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 56b66e2c-9cac-11e8-816f-52540089f226 | server2 | 3306 | ONLINE |
| group_replication_applier | 5734ab63-9cac-11e8-8345-5254004639f4 | server3 | 3306 | RECOVERING |
| group_replication_applier | f84fdfe5-9cad-11e8-a395-5254009ac5cd | server1 | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
3 rows in set (0.00 sec)
mysql> use test
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> select * from t1; 可以查看server1建立的字段信息
+----+-------+
| c1 | c2 |
+----+-------+
| 1 | lurci |
+----+-------+
1 row in set (0.00 sec)
测试用server3插入字段信息:
mysql> insert into t1 values (2,'ricci');
Query OK, 1 row affected (0.55 sec)
mysql> select * from t1;
+----+-------+
| c1 | c2 |
+----+-------+
| 1 | lurci |
| 2 | ricci |
+----+-------+
2 rows in set (0.00 sec)
用server2和server1查看数据表的字段信息已经完全同步:
用server2插入字段信息:
mysql> insert into t1 values(3,'aa');
Query OK, 1 row affected (0.53 sec)
mysql> select * from t1;
+----+-------+
| c1 | c2 |
+----+-------+
| 1 | lurci |
| 2 | ricci |
| 3 | aa |
+----+-------+
3 rows in set (0.00 sec)
用server3和server1查看数据表的字段信息已经完全同步: