一 Mysql Group Replication简介
Mysql Group Replication(MGR)是一个全新的高可用和高扩张的MySQL集群服务。
高一致性,基于原生复制及paxos协议的组复制技术,以插件方式提供一致数据安全保证;
高容错性,大多数服务正常就可继续工作,自动不同节点检测资源征用冲突,按顺序优先处理,内置自动防脑裂机制;
高扩展性,自动添加移除节点,并更新组信息;
高灵活性,单主模式和多主模式。单主模式自动选主,所有更新操作在主进行;多主模式,所有server同时更新。
二 Mysql Group Replication与传统复制的区别和大幅改进
1.传统复制
主-从复制:有一个主和不等数量的从。主节点执行的事务会异步发送给从节点,在从节点重新执行。(异步和半同步)
(半同步相对异步Master会确认Slave是否接到数据,更加安全)
(原理见主从复制笔记)
2.并行复制
并行复制:复制->广播->正式复制
优势:
弹性复制(高扩展性):server动态添加移除
高可用分片(高扩展性):分片实现写扩展,每个分片是一个复制组。
替代主从复制(高扩展性):整组写入,避免单点争用。
自动化系统:自动化部署Mysql复制到已有复制协议的自动化系统。
故障检测与容错:自动检测,若服务faild,组内成员大多数达成认为该服务已不正常,则自动隔离。
组内成员会构成一个视图,组内成员主动加入或离开(主动或被动),都会更新组配置,更新视图。成员自愿离开,先更新组配置,然后采用大多数成员(不包含主动脱离的成员)意见是否确认该成员离开更新视图。如果是故障要排除,则需大多数服务确认(包括故障成员意见),然后才会更新组配置和视图。
最大允许即时故障数:f=(n-1)/2,多数正常则正常
三 主从复制限制
1.存储引擎必须为innodb
2.每个表必须提供主键
3.只支持ipv4,网络需求较高
4.一个group最多只能有9台服务器
5.不支持Replication event checksums,
6.不支持Savepoints
7.multi-primary mode部署方式不支持SERIALIZABLE事务隔离级别
8.multi-primary mode部署方式不能完全支持级联外键约束
9.multi-primary mode部署方式不支持在不同节点上对同一个数据库对象并发执行DDL(在不同节点上对同一行并发进行RW事务,后发起的事务会失败)
四 单主模式布置组复制
四 单主模式布置组复制
(组中server可在独立物理机运行,也可在同一台机器,同一机器采用多实例,也就是逻辑认为是独立机器)
1.安装mysql5.7包
1.卸载系统已有数据库
#检测系统是否已安装mariadb,如有则卸载
rpm -qa | grep mariadb
rpm -e mariadb-libs --nodeps
2.编译安装mysql5.7
#添加mysql用户组并添加mysql用户(不允许登录)
groupadd mysql
useradd -r -g mysql -s /bin/false -M mysql
#下载mysql源码包
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.17.tar.gz
#安装编译工具和依赖包
yum install -y cmake make gcc gcc-c++
yum install -y ncurses-devel openssl-devel bison-devel libaio libaio-devel
# boost库安装
# 该步骤可以省略,在cmake阶段添加参数-DDOWNLOAD_BOOST= -DWITH_BOOST=/usr/local/boost即可
wget http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
tar -zxvf boost_1_59_0.tar.gz -C /usr/local
mv /usr/local/boost_1_59_0 /usr/local/boost
cd /usr/local/boost
./bootstrap.sh
./b2 stage threading=multi link=shared
./b2 install threading=multi link=shared
#安装编译mysql
cd
tar -zxvf mysql-5.7..tar.gz
cd mysql-5.7.
#使用cmake工具设置参数,新版的php都从configure改用cmake
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADTR=/usr/local/mysql/data \
-DSYSCONFDIR=/etc \
-DMYSQL_USER=mysql \
-DWITH_MYISAM_STORAGE_ENGINE= \
-DWITH_INNOBASE_STORAGE_ENGINE= \
-DWITH_ARCHIVE_STORAGE_ENGINE= \
-DWITH_MEMORY_STORAGE_ENGINE= \
-DWITH_READLINE= \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DMYSQL_TCP_PORT= \
-DENABLED_LOCAL_INFILE= \
-DENABLE_DOWNLOADS= \
-DWITH_PARTRTION_STORAGE_ENGINE= \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_DEBUG= \
-DMYSQL_MAINTAINER_MODE= \
-DMITH_SSL:STRING=bundled \
-DWITH_ZLIB:STRING=bundled \
-DDOWNLOAD_BOOST= \
-DWITH_BOOST=/usr/local/boost
make && make install
#默认1个线程编译,可使用指定线程数加快编译
#make -j $(grep processor /proc/cpuinfo | wc -l) && make install
#将mysql目录权限给mysql用户
chown -Rf mysql:mysql /usr/local/mysql
#设置mysql的默认配置文件
cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
#把mysql命令加入查找路径
echo "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile
source /etc/profile
防火墙和selinux设置
firewall-cmd --permanent --add-port=/tcp
firewall-cmd --permanent --add-port=/tcp
firewall-cmd --permanent --add-port=/tcp
firewall-cmd --permanent --add-port=/tcp
firewall-cmd --permanent --add-port=/tcp
firewall-cmd --permanent --add-port=/tcp
firewall-cmd --reload
setenforce
sed -i 's#enforcing#permissive#g' /etc/selinux/config
3.部署组复制实例
mkdir /data
#初始化数据库,创建数据库
mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/s1
mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/s2
mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/s3
4.配置组复制实例S1
cat > /data/s1/my.cnf <<EOF
[mysqld]
# server configuration
user=mysql
datadir=/data/s1
basedir=/usr/local/mysql/
port=
socket=/usr/local/mysql/s1.sock
#复制框架
server_id=
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
#组复制设置
#server必须为每个事务收集写集合,并使用XXHASH64哈希算法将其编码为散列
transaction_write_set_extraction=XXHASH64
#告知插件加入或创建组命名,UUID
loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
#server启动时不自启组复制
loose-group_replication_start_on_boot=off
#告诉插件使用IP地址,端口3407用于接收组中其他成员转入连接
loose-group_replication_local_address="127.0.0.1:3407"
#启动组server,种子server,加入组应该连接这些的ip和端口;其他server要加入组得由组成员同意
loose-group_replication_group_seeds="127.0.0.1:3407,127.0.0.1:3408,127.0.0.1:3409"
loose-group_replication_bootstrap_group=off
loose-group_replication_single_primary_mode=FALSE
loose-group_replication_enforce_update_everywhere_checks= TRUE
EOF
5.用户凭据
#启动mysql实例1服务
nohup mysqld --defaults-file=/data/s1/my.cnf >/data/s1/nohup.out >/data/s1/nohup.out &
#登录mysql s1
mysql -uroot -h127.0.0. -P3307 --skip-password
#修改root密码
alter user 'root'@'localhost' identified by '';
#下面操纵不写入二进制日志,避免修改传递给其他实例,先关闭
set sql_log_bin=;
#创建拥有replication slave权限mysql用户
create user rpl_user@'%';
grant replication slave on *.* to rpl_user@'%' identified by 'rpl_pass';
flush privileges;
#开启二进制写入
set sql_log_bin=;
#分布式恢复加入组的server执行第一步
#change master to语言将server配置为,在下次需要从其他成员恢复状态时,使用group_replication_recovery复制通道的给定凭证。
change master to master_user='rpl_user',master_password='rpl_pass' for channel 'group_replication_recovery';
#建议每个mysql通过配置唯一主机名,通过DNS或本地设置。(相同可能导致无法恢复)
6.启动组复制
#安装组复制插件
install plugin group_replication soname 'group_replication.so';
#检测插件是否安装成功
show plugins;
+----------------------------+----------+--------------------+----------------------+---------+ | Name | Status | Type | Library | License | +----------------------------+----------+--------------------+----------------------+---------+ ...... | group_replication | ACTIVE | GROUP REPLICATION | group_replication.so | GPL | +----------------------------+----------+--------------------+----------------------+---------+
#server s1引导组,启动组复制程序(复制组只启动一次就行)
set global group_replication_bootstrap_group=ON;
start 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 | 70bf7af3--11e7-82a8-000c29433013 | test1 | | ONLINE | +---------------------------+--------------------------------------+-------------+-------------+--------------+
7.测试
mysql> create database test;
--31T23::.535115Z [Note] Plugin group_replication reported: 'Primary had applied all relay logs, disabled conflict detection'
Query OK, row affected (0.03 sec)
mysql> use test;
Database changed
mysql> create table t1(c1 int primary key,c2 text not null);
Query OK, rows affected (0.03 sec)
mysql> insert into t1 values ( , 'Luis');
Query OK, row affected (0.01 sec)
mysql> select * from t1;
+----+------+
| c1 | c2 |
+----+------+
| | Luis |
+----+------+
row in set (0.00 sec)
mysql> show binlog events;
+---------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +---------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------+ | binlog. | | Format_desc | | | Server ver: 5.7.-log, Binlog ver: | | binlog. | | Previous_gtids | | | | | binlog. | | Gtid | | | SET @@SESSION.GTID_NEXT= '21355e09-16ea-11e7-bb6b-000c29433013:1' | | binlog. | | Query | | | ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' | | binlog. | | Gtid | | | SET @@SESSION.GTID_NEXT= 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:1' | | binlog. | | Query | | | BEGIN | | binlog. | | View_change | | | view_id=: | | binlog. | | Query | | | COMMIT | | binlog. | | Gtid | | | SET @@SESSION.GTID_NEXT= 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:2' | | binlog. | | Query | | | create database test | | binlog. | | Gtid | | | SET @@SESSION.GTID_NEXT= 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:3' | | binlog. | | Query | | | use `test`; create table t1(c1 int primary key,c2 text not null) | | binlog. | | Gtid | | | SET @@SESSION.GTID_NEXT= 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:4' | | binlog. | | Query | | | BEGIN | | binlog. | | Table_map | | | table_id: (test.t1) | | binlog. | | Write_rows | | | table_id: flags: STMT_END_F | | binlog. | | Xid | | | COMMIT /* xid=40 */ | +---------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------+ rows in set (0.00 sec)
8.第二个实例添加(S2)
cat > /data/s2/my.cnf <<EOF
[mysqld]
# server configuration
user=mysql
datadir=/data/s2
basedir=/usr/local/mysql/
port=
socket=/usr/local/mysql/s2.sock
#复制框架
server_id=
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
#组复制设置
#server必须为每个事务收集写集合,并使用XXHASH64哈希算法将其编码为散列
transaction_write_set_extraction=XXHASH64
#告知插件加入或创建组命名,UUID
loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
#server启动时不自启组复制
loose-group_replication_start_on_boot=off
#告诉插件使用IP地址,端口3408用于接收组中其他成员转入连接
loose-group_replication_local_address="127.0.0.1:3408"
#启动组server,种子server,加入组应该连接这些的ip和端口;其他server要加入组得由组成员同意
loose-group_replication_group_seeds="127.0.0.1:3407,127.0.0.1:3408,127.0.0.1:3409"
loose-group_replication_bootstrap_group=off
loose-group_replication_single_primary_mode=FALSE
loose-group_replication_enforce_update_everywhere_checks= TRUE
EOF
9.用户凭证(S2)
#启动mysql实例2服务
nohup mysqld --defaults-file=/data/s2/my.cnf >/data/s2/nohup.out >/data/s2/nohup.out &
#登录mysql s1
mysql -uroot -h127.0.0. -P3308 --skip-password
#修改root密码
alter user 'root'@'localhost' identified by '';
#下面操纵不写入二进制日志,避免修改传递给其他实例,先关闭
set sql_log_bin=;
#创建拥有replication slave权限mysql用户
create user rpl_user@'%';
grant replication slave on *.* to rpl_user@'%' identified by 'rpl_pass';
flush privileges;
#开启二进制写入
set sql_log_bin=;
#分布式恢复加入组的server执行第一步
#change master to语言将server配置为,在下次需要从其他成员恢复状态时,使用group_replication_recovery复制通道的给定凭证。
change master to master_user='rpl_user',master_password='rpl_pass' for channel 'group_replication_recovery';
#建议每个mysql通过配置唯一主机名,通过DNS或本地设置。(相同可能导致无法恢复)
10.添加组
#安装组复制插件
install plugin group_replication soname 'group_replication.so';
#检测插件是否安装成功
show plugins;
+----------------------------+----------+--------------------+----------------------+---------+ | Name | Status | Type | Library | License | +----------------------------+----------+--------------------+----------------------+---------+ ...... | group_replication | ACTIVE | GROUP REPLICATION | group_replication.so | GPL | +----------------------------+----------+--------------------+----------------------+---------+
#添加到复制组(不用再设置启动,该组已在是s1时启动)
set global group_replication_allow_local_disjoint_gtids_join=ON;
start group_replication;
#检测组是否创建并已加入新成员
select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+ | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | +---------------------------+--------------------------------------+-------------+-------------+--------------+ | group_replication_applier | 21355e09-16ea-11e7-bb6b-000c29433013 | test1 | | ONLINE | | group_replication_applier | 25b39bc8-16ea-11e7-bc1e-000c29433013 | test1 | | ONLINE | +---------------------------+--------------------------------------+-------------+-------------+--------------+
.测试同步是否正常
mysql> show databases like 'test';
+-----------------+
| Database (test) |
+-----------------+
| test |
+-----------------+
问题:
#在建立第二个mysql实例s2时,会有以下现象,s2一直处于RECOVERING状态。
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 21355e09-16ea-11e7-bb6b-000c29433013 | test1 | | ONLINE |
| group_replication_applier | 25b39bc8-16ea-11e7-bc1e-000c29433013 | test1 | | RECOVERING |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
错误日志如下:
[ERROR] Slave I/O for channel 'group_replication_recovery': error connecting to master 'rpl_user@test1:3307' - retry-time: retries: , Error_code:
[ERROR] Plugin group_replication reported: 'There was an error when connecting to the donor server. Check group replication recovery's connection credentials.'
原因:mysql组复制用的是域名连接,我的主机设置名字为test1,但没有在hosts文件中声明其IP为127.0.0.1。(DNS就更没有了)导致s2无法正常访问s1。因而报错。
解决方法:修改/etc/hosts文件,追加127.0.0.1 对应主机名为test1。然后重新启动组复制就可以。
《Mysql Group Repaliation》京东翻译
参考博客 http://www.voidcn.com/blog/d6619309/article/p-6346153.html
转自
http://blog.csdn.net/hzsunshine/article/details/69132225