Mysql Group Replication 简介及单主模式组复制配置

数据库 waitig 527℃ 百度已收录 0评论


一 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.卸载系统已有数据库

  1. #检测系统是否已安装mariadb,如有则卸载
  2. rpm -qa | grep mariadb
  3. rpm -e mariadb-libs --nodeps

   2.编译安装mysql5.7

  1. #添加mysql用户组并添加mysql用户(不允许登录)
  2. groupadd mysql
  3. useradd -r -g mysql -s /bin/false -M mysql
  4. #下载mysql源码包
  5. wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.17.tar.gz
  6. #安装编译工具和依赖包
  7. yum install -y cmake make gcc gcc-c++
  8. yum install -y ncurses-devel openssl-devel bison-devel libaio libaio-devel
  9. # boost库安装
  10. # 该步骤可以省略,在cmake阶段添加参数-DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost即可
  11. wget http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
  12. tar -zxvf boost_1_59_0.tar.gz -C /usr/local
  13. mv /usr/local/boost_1_59_0 /usr/local/boost
  14. cd /usr/local/boost
  15. ./bootstrap.sh
  16. ./b2 stage threading=multi link=shared
  17. ./b2 install threading=multi link=shared
  18. #安装编译mysql
  19. cd
  20. tar -zxvf mysql-5.7.17.tar.gz
  21. cd mysql-5.7.17
  22. #使用cmake工具设置参数,新版的php都从configure改用cmake
  23. cmake \
  24. -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
  25. -DMYSQL_DATADTR=/usr/local/mysql/data \
  26. -DSYSCONFDIR=/etc \
  27. -DMYSQL_USER=mysql \
  28. -DWITH_MYISAM_STORAGE_ENGINE=1 \
  29. -DWITH_INNOBASE_STORAGE_ENGINE=1 \
  30. -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
  31. -DWITH_MEMORY_STORAGE_ENGINE=1 \
  32. -DWITH_READLINE=1 \
  33. -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
  34. -DMYSQL_TCP_PORT=3306 \
  35. -DENABLED_LOCAL_INFILE=1 \
  36. -DENABLE_DOWNLOADS=1 \
  37. -DWITH_PARTRTION_STORAGE_ENGINE=1 \
  38. -DEXTRA_CHARSETS=all \
  39. -DDEFAULT_CHARSET=utf8 \
  40. -DDEFAULT_COLLATION=utf8_general_ci \
  41. -DWITH_DEBUG=0 \
  42. -DMYSQL_MAINTAINER_MODE=0 \
  43. -DMITH_SSL:STRING=bundled \
  44. -DWITH_ZLIB:STRING=bundled \
  45. -DDOWNLOAD_BOOST=1 \
  46. -DWITH_BOOST=/usr/local/boost
  47. make && make install
  48. #默认1个线程编译,可使用指定线程数加快编译
  49. #make -j $(grep processor /proc/cpuinfo | wc -l) && make install
  50. #将mysql目录权限给mysql用户
  51. chown -Rf mysql:mysql /usr/local/mysql
  52. #设置mysql的默认配置文件
  53. cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
  54. #把mysql命令加入查找路径
  55. echo "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile
  56. source /etc/profile

   防火墙和selinux设置

  1. firewall-cmd --permanent --add-port=3307/tcp
  2. firewall-cmd --permanent --add-port=3308/tcp
  3. firewall-cmd --permanent --add-port=3309/tcp
  4. firewall-cmd --permanent --add-port=3407/tcp
  5. firewall-cmd --permanent --add-port=3408/tcp
  6. firewall-cmd --permanent --add-port=3409/tcp
  7. firewall-cmd --reload
  8. setenforce 0
  9. sed -i 's#enforcing#permissive#g' /etc/selinux/config

   3.部署组复制实例

  1. mkdir /data
  2. #初始化数据库,创建数据库
  3. mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/s1
  4. mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/s2
  5. mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/s3

   4.配置组复制实例S1

  1. cat > /data/s1/my.cnf <<EOF
  2. [mysqld]
  3. # server configuration
  4. user=mysql
  5. datadir=/data/s1
  6. basedir=/usr/local/mysql/
  7. port=3307
  8. socket=/usr/local/mysql/s1.sock
  9. #复制框架
  10. server_id=1
  11. gtid_mode=ON
  12. enforce_gtid_consistency=ON
  13. master_info_repository=TABLE
  14. relay_log_info_repository=TABLE
  15. binlog_checksum=NONE
  16. log_slave_updates=ON
  17. log_bin=binlog
  18. binlog_format=ROW
  19. #组复制设置
  20. #server必须为每个事务收集写集合,并使用XXHASH64哈希算法将其编码为散列
  21. transaction_write_set_extraction=XXHASH64
  22. #告知插件加入或创建组命名,UUID
  23. loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
  24. #server启动时不自启组复制
  25. loose-group_replication_start_on_boot=off
  26. #告诉插件使用IP地址,端口3407用于接收组中其他成员转入连接
  27. loose-group_replication_local_address="127.0.0.1:3407"
  28. #启动组server,种子server,加入组应该连接这些的ip和端口;其他server要加入组得由组成员同意
  29. loose-group_replication_group_seeds="127.0.0.1:3407,127.0.0.1:3408,127.0.0.1:3409"
  30. loose-group_replication_bootstrap_group=off
  31. loose-group_replication_single_primary_mode=FALSE
  32. loose-group_replication_enforce_update_everywhere_checks= TRUE
  33. EOF

   5.用户凭据

  1. #启动mysql实例1服务
  2. nohup mysqld --defaults-file=/data/s1/my.cnf >/data/s1/nohup.out 2>/data/s1/nohup.out &
  3. #登录mysql s1
  4. mysql -uroot -h127.0.0.1 -P3307 --skip-password
  5. #修改root密码
  6. alter user 'root'@'localhost' identified by '123456';
  7. #下面操纵不写入二进制日志,避免修改传递给其他实例,先关闭
  8. set sql_log_bin=0;
  9. #创建拥有replication slave权限mysql用户
  10. create user rpl_user@'%';
  11. grant replication slave on *.* to rpl_user@'%' identified by 'rpl_pass';
  12. flush privileges;
  13. #开启二进制写入
  14. set sql_log_bin=1;
  15. #分布式恢复加入组的server执行第一步
  16. #change master to语言将server配置为,在下次需要从其他成员恢复状态时,使用group_replication_recovery复制通道的给定凭证。
  17. change master to master_user='rpl_user',master_password='rpl_pass' for channel 'group_replication_recovery';
  18. #建议每个mysql通过配置唯一主机名,通过DNS或本地设置。(相同可能导致无法恢复)

   6.启动组复制

  1. #安装组复制插件
  2. install plugin group_replication soname 'group_replication.so';
  3. #检测插件是否安装成功
  4. show plugins;
  5. +----------------------------+----------+--------------------+----------------------+---------+ | Name | Status | Type | Library | License | +----------------------------+----------+--------------------+----------------------+---------+ ...... | group_replication | ACTIVE | GROUP REPLICATION | group_replication.so | GPL | +----------------------------+----------+--------------------+----------------------+---------+
  6. #server s1引导组,启动组复制程序(复制组只启动一次就行)
  7. set global group_replication_bootstrap_group=ON;
  8. start group_replication;
  9. set global group_replication_bootstrap_group=OFF;
  10. #检测组是否创建并已加入新成员
  11. select * from performance_schema.replication_group_members;
  12. +---------------------------+--------------------------------------+-------------+-------------+--------------+ | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | +---------------------------+--------------------------------------+-------------+-------------+--------------+ | group_replication_applier | 70bf7af3-1657-11e7-82a8-000c29433013 | test1 | 3307 | ONLINE | +---------------------------+--------------------------------------+-------------+-------------+--------------+

   7.测试

  1. mysql> create database test;
  2. 2017-03-31T23:23:45.535115Z 8 [Note] Plugin group_replication reported: 'Primary had applied all relay logs, disabled conflict detection'
  3. Query OK, 1 row affected (0.03 sec)
  4. mysql> use test;
  5. Database changed
  6. mysql> create table t1(c1 int primary key,c2 text not null);
  7. Query OK, 0 rows affected (0.03 sec)
  8. mysql> insert into t1 values (1 , 'Luis');
  9. Query OK, 1 row affected (0.01 sec)
  10. mysql> select * from t1;
  11. +----+------+
  12. | c1 | c2 |
  13. +----+------+
  14. | 1 | Luis |
  15. +----+------+
  16. 1 row in set (0.00 sec)
  17. mysql> show binlog events;
  18. +---------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------+
  19. | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +---------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------+ | binlog.000001 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.17-log, Binlog ver: 4 | | binlog.000001 | 123 | Previous_gtids | 1 | 150 | | | binlog.000001 | 150 | Gtid | 1 | 211 | SET @@SESSION.GTID_NEXT= '21355e09-16ea-11e7-bb6b-000c29433013:1' | | binlog.000001 | 211 | Query | 1 | 386 | ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' | | binlog.000001 | 386 | Gtid | 1 | 447 | SET @@SESSION.GTID_NEXT= 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:1' | | binlog.000001 | 447 | Query | 1 | 506 | BEGIN | | binlog.000001 | 506 | View_change | 1 | 645 | view_id=14910585094598745:1 | | binlog.000001 | 645 | Query | 1 | 710 | COMMIT | | binlog.000001 | 710 | Gtid | 1 | 771 | SET @@SESSION.GTID_NEXT= 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:2' | | binlog.000001 | 771 | Query | 1 | 861 | create database test | | binlog.000001 | 861 | Gtid | 1 | 922 | SET @@SESSION.GTID_NEXT= 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:3' | | binlog.000001 | 922 | Query | 1 | 1044 | use `test`; create table t1(c1 int primary key,c2 text not null) | | binlog.000001 | 1044 | Gtid | 1 | 1105 | SET @@SESSION.GTID_NEXT= 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:4' | | binlog.000001 | 1105 | Query | 1 | 1173 | BEGIN | | binlog.000001 | 1173 | Table_map | 1 | 1216 | table_id: 220 (test.t1) | | binlog.000001 | 1216 | Write_rows | 1 | 1258 | table_id: 220 flags: STMT_END_F | | binlog.000001 | 1258 | Xid | 1 | 1285 | COMMIT /* xid=40 */ | +---------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------+ 17 rows in set (0.00 sec)

   8.第二个实例添加(S2)     

  1. cat > /data/s2/my.cnf <<EOF
  2. [mysqld]
  3. # server configuration
  4. user=mysql
  5. datadir=/data/s2
  6. basedir=/usr/local/mysql/
  7. port=3308
  8. socket=/usr/local/mysql/s2.sock
  9. #复制框架
  10. server_id=2
  11. gtid_mode=ON
  12. enforce_gtid_consistency=ON
  13. master_info_repository=TABLE
  14. relay_log_info_repository=TABLE
  15. binlog_checksum=NONE
  16. log_slave_updates=ON
  17. log_bin=binlog
  18. binlog_format=ROW
  19. #组复制设置
  20. #server必须为每个事务收集写集合,并使用XXHASH64哈希算法将其编码为散列
  21. transaction_write_set_extraction=XXHASH64
  22. #告知插件加入或创建组命名,UUID
  23. loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
  24. #server启动时不自启组复制
  25. loose-group_replication_start_on_boot=off
  26. #告诉插件使用IP地址,端口3408用于接收组中其他成员转入连接
  27. loose-group_replication_local_address="127.0.0.1:3408"
  28. #启动组server,种子server,加入组应该连接这些的ip和端口;其他server要加入组得由组成员同意
  29. loose-group_replication_group_seeds="127.0.0.1:3407,127.0.0.1:3408,127.0.0.1:3409"
  30. loose-group_replication_bootstrap_group=off
  31. loose-group_replication_single_primary_mode=FALSE
  32. loose-group_replication_enforce_update_everywhere_checks= TRUE
  33. EOF

   9.用户凭证(S2)

  1. #启动mysql实例2服务
  2. nohup mysqld --defaults-file=/data/s2/my.cnf >/data/s2/nohup.out 2>/data/s2/nohup.out &
  3. #登录mysql s1
  4. mysql -uroot -h127.0.0.1 -P3308 --skip-password
  5. #修改root密码
  6. alter user 'root'@'localhost' identified by '123456';
  7. #下面操纵不写入二进制日志,避免修改传递给其他实例,先关闭
  8. set sql_log_bin=0;
  9. #创建拥有replication slave权限mysql用户
  10. create user rpl_user@'%';
  11. grant replication slave on *.* to rpl_user@'%' identified by 'rpl_pass';
  12. flush privileges;
  13. #开启二进制写入
  14. set sql_log_bin=1;
  15. #分布式恢复加入组的server执行第一步
  16. #change master to语言将server配置为,在下次需要从其他成员恢复状态时,使用group_replication_recovery复制通道的给定凭证。
  17. change master to master_user='rpl_user',master_password='rpl_pass' for channel 'group_replication_recovery';
  18. #建议每个mysql通过配置唯一主机名,通过DNS或本地设置。(相同可能导致无法恢复)

   10.添加组

  1. #安装组复制插件
  2. install plugin group_replication soname 'group_replication.so';
  3. #检测插件是否安装成功
  4. show plugins;
  5. +----------------------------+----------+--------------------+----------------------+---------+ | Name | Status | Type | Library | License | +----------------------------+----------+--------------------+----------------------+---------+ ...... | group_replication | ACTIVE | GROUP REPLICATION | group_replication.so | GPL | +----------------------------+----------+--------------------+----------------------+---------+
  6. #添加到复制组(不用再设置启动,该组已在是s1时启动)
  7. set global group_replication_allow_local_disjoint_gtids_join=ON;
  8. start group_replication;
  9. #检测组是否创建并已加入新成员
  10. select * from performance_schema.replication_group_members;
  11. +---------------------------+--------------------------------------+-------------+-------------+--------------+ | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | +---------------------------+--------------------------------------+-------------+-------------+--------------+ | group_replication_applier | 21355e09-16ea-11e7-bb6b-000c29433013 | test1 | 3307 | ONLINE | | group_replication_applier | 25b39bc8-16ea-11e7-bc1e-000c29433013 | test1 | 3308 | ONLINE | +---------------------------+--------------------------------------+-------------+-------------+--------------+

   11.测试同步是否正常

  1. mysql> show databases like 'test';
  2. +-----------------+
  3. | Database (test) |
  4. +-----------------+
  5. | test |
  6. +-----------------+

问题:
#在建立第二个mysql实例s2时,会有以下现象,s2一直处于RECOVERING状态。

  1. +---------------------------+--------------------------------------+-------------+-------------+--------------+
  2. | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
  3. +---------------------------+--------------------------------------+-------------+-------------+--------------+
  4. | group_replication_applier | 21355e09-16ea-11e7-bb6b-000c29433013 | test1 | 3307 | ONLINE |
  5. | group_replication_applier | 25b39bc8-16ea-11e7-bc1e-000c29433013 | test1 | 3308 | RECOVERING |
  6. +---------------------------+--------------------------------------+-------------+-------------+--------------+

错误日志如下:

  1. [ERROR] Slave I/O for channel 'group_replication_recovery': error connecting to master 'rpl_user@test1:3307' - retry-time: 60 retries: 1, Error_code: 2003
  2. [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。然后重新启动组复制就可以。

   

参考博客 http://wangwei007.blog.51cto.com/680.19/1907145 

《Mysql Group Repaliation》京东翻译

参考博客 http://www.voidcn.com/blog/d6619309/article/p-6346153.html   

阅读全文

版权声明:


本文由【waitig】发表在等英博客
本文固定链接:Mysql Group Replication 简介及单主模式组复制配置
欢迎关注本站官方公众号,每日都有干货分享!
等英博客官方公众号
点赞 (0)分享 (0)