MySQL 基于 GTID 的组复制(MGR)

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
简介: MySQL 基于 GTID 的组复制(MGR)

环境准备

IP 主机名 操作系统
192.168.131.129 mgr-node1 CentOS7.6
192.168.131.130 mgr-node2 CentOS7.6
192.168.131.131 mgr-node3 CentOS7.6
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@mgr-node1 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
[root@mgr-node1 ~]# sestatus
SELinux status:                 disabled
[root@localhost ~]# hostnamectl --static set-hostname mgr-node1
[root@localhost ~]# hostnamectl --static set-hostname mgr-node2
[root@localhost ~]# hostnamectl --static set-hostname mgr-node3
[root@localhost ~]# hostnamectl --static set-hostname proxysql
[root@mgr-node1 ~]# vim /etc/hosts
192.168.131.129   mgr-node1
192.168.131.130   mgr-node2
192.168.131.131   mgr-node3
192.168.131.132   proxysql

安装mysql

# 三台都需要安装
[root@mgr-node1 ~]# wget https://repo.mysql.com/mysql57-community-release-el7-11.noarch.rpm
[root@mgr-node1 ~]# yum -y install mysql57-community-release-el7-11.noarch.rpm
[root@mgr-node1 ~]# yum -y install yum-utils   # 安装yum管理工具
[root@mgr-node1 ~]# yum-config-manager --disable mysql80-community   # 禁用8.0版本
[root@mgr-node1 ~]# yum-config-manager --enable mysql57-community    # 启用5.7版本
[root@mgr-node1 ~]# yum repolist enabled | grep mysql    # 检查一下,确保只有一个版本
mysql-connectors-community/x86_64       MySQL Connectors Community           165
mysql-tools-community/x86_64            MySQL Tools Community                115
mysql57-community/x86_64                MySQL 5.7 Community Server           444
[root@mgr-node1 ~]# yum -y install mysql-community-server mysql
[root@mgr-node1 ~]# systemctl enable mysqld --now   # 设为开机自启,并立即启动
[root@mgr-node1 ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2020-07-19 06:00:41 CST; 40s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 21909 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 21860 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 21912 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─21912 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Jul 19 06:00:36 proxysql systemd[1]: Starting MySQL Server...
Jul 19 06:00:41 proxysql systemd[1]: Started MySQL Server.

配置mysql

# 三台机器都需要修改默认密码
[root@mgr-node1 ~]# grep "temporary password" /var/log/mysqld.log
2020-07-18T22:00:38.730773Z 1 [Note] A temporary password is generated for root@localhost: H/bkI+e%2mr=
[root@mgr-node1 ~]# mysql -uroot -p'H/bkI+e%2mr='
mysql> alter user 'root'@'localhost' identified by 'Test123.com';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

[root@mgr-node1 ~]# mysql -uroot -p # 改完密码测试一下
Enter password:
mysql> show databases;  # 看一下库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> select version();   # 查看一下版本
+-----------+
| version() |
+-----------+
| 5.7.31    |
+-----------+
1 row in set (0.00 sec)

MGR组复制部署

mgr-node1配置

[root@mgr-node1 ~]# uuidgen    # 创一个uuid给MGR当组名使用
03d87c93-9d96-43ad-bcec-592e07beff3f
[root@mgr-node1 ~]# cp /etc/my.cnf{,.bak}  # 国际管理,备份配置文件,给自己留一条退路
[root@mgr-node1 ~]# egrep -v "^$|#" /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
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  # relay.info记录在table中
binlog_checksum=NONE   # 不生成checksum, 这样就可以兼容旧版本的mysql,默认NONE

log_bin = mysql-bin
log-slave-updates = 1
binlog_format = row
sync-master-info = 1
sync_binlog = 1

skip_slave_start = 1

transaction_write_set_extraction=XXHASH64   # 以便在server收集写集合的同时将其记录到二进制日志。写集合基于每行的主键,并且是行更改后的唯一标识此标识将用于检测冲突。
loose-group_replication_group_name="1f6a5a0c-162e-4c68-9520-fb5a70c090f0"  # 这里必须使用UUID的格式
loose-group_replication_start_on_boot=off  # #为了避免每次启动自动引导具有相同名称的第二个组,所以设置为OFF
loose-group_replication_local_address= "192.168.131.129:33066"    # #用于组间通信的地址
loose-group_replication_group_seeds= "192.168.131.129:33066,192.168.131.130:33066,192.168.131.131:33066"
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="192.168.131.0/24,127.0.0.1/8"  # 允许加入组复制的客户机来源的ip白名单
[root@mgr-node1 ~]# systemctl restart mysqld  # 重启mysql
[root@mgr-node1 ~]# mysql -uroot -p
Enter password:
mysql> set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)

mysql> grant replication slave on *.* to mgr_slave@'192.168.131.%' identified by 'Test123@com';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> reset master;
Query OK, 0 rows affected (0.00 sec)

mysql> set sql_log_bin=1;
Query OK, 0 rows affected (0.00 sec)

mysql> change master to master_user='mgr_slave',master_password='Test123@com' for channel 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.02 sec)

mysql> install plugin group_replication soname 'group_replication.so';
Query OK, 0 rows affected (0.02 sec)
mysql> show plugins;
+----------------------------+----------+--------------------+----------------------+---------+
| Name                       | Status   | Type               | Library              | License |
+----------------------------+----------+--------------------+----------------------+---------+
......
......
| group_replication          | ACTIVE   | GROUP REPLICATION  | group_replication.so | GPL     |
+----------------------------+----------+--------------------+----------------------+---------+
46 rows in set (0.00 sec)
mysql> set global group_replication_bootstrap_group=on;
Query OK, 0 rows affected (0.01 sec)

mysql> start group_replication;
Query OK, 0 rows affected (2.34 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;   # 要保证group_replication_applier的状态为"ONLINE"
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 1ca31701-c942-11ea-8881-000c29e23bd4 | proxysql    |        3306 | ONLINE       |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
1 row in set (0.00 sec)
# 创建一个库,写一点内容,提供给后面测试使用
mysql> create database test character set utf8 collate utf8_general_ci;
Query OK, 1 row affected (0.00 sec)

mysql> use test;
Database changed
mysql> create table if not exists hello_world (id int(10) primary key auto_increment,name varchar(50) not null);
Query OK, 0 rows affected (0.05 sec)

mysql> insert into test.hello_world values(1,"python"),(2,"shell"),(3,"yaml"),(4,"go");
Query OK, 4 rows affected (0.02 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from test.hello_world;
+----+--------+
| id | name   |
+----+--------+
|  1 | python |
|  2 | shell  |
|  3 | yaml   |
|  4 | go     |
+----+--------+
4 rows in set (0.00 sec)

mgr-node2和mgr-node3配置

[root@mgr-node2 ~]# egrep -v "^$|#" /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
server_id = 2   # 其他配置不变,需要修改server_id,不能一样
gtid_mode = on
enforce_gtid_consistency = on
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE

log_bin = mysql-bin
log-slave-updates = 1
binlog_format = row
sync-master-info = 1
sync_binlog = 1

skip_slave_start = 1

transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="1f6a5a0c-162e-4c68-9520-fb5a70c090f0"
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "192.168.131.130:33066"   # 本机ip,需要修改
loose-group_replication_group_seeds= "192.168.131.129:33066,192.168.131.130:33066,192.168.131.131:33066"
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="192.168.131.0/24,127.0.0.1/8"
[root@mgr-node3 ~]# systemctl restart mysqld
--------------------------------------------------------------------------------------
[root@mgr-node3 ~]# egrep -v "^$|#" /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
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_bin = mysql-bin
log-slave-updates = 1
binlog_format = row
sync-master-info = 1
sync_binlog = 1

skip_slave_start = 1

transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="1f6a5a0c-162e-4c68-9520-fb5a70c090f0"
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "192.168.131.131:33066"
loose-group_replication_group_seeds= "192.168.131.129:33066,192.168.131.130:33066,192.168.131.131:33066"
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="192.168.131.0/24,127.0.0.1/8"
[root@mgr-node3 ~]# systemctl restart mysqld
[root@mgr-node2 ~]# mysql -p
Enter password:
mysql> set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)

mysql> grant replication slave on *.* to mgr_slave@'192.168.131.%' identified by 'Test123@com';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> reset master;
Query OK, 0 rows affected (0.01 sec)

mysql> set sql_log_bin=1;
Query OK, 0 rows affected (0.00 sec)

mysql> change master to master_user='mgr_slave',master_password='Test123@com' for channel 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> install plugin group_replication soname 'group_replication.so';
Query OK, 0 rows affected (0.03 sec)

mysql> show plugins;
+----------------------------+----------+--------------------+----------------------+---------+
| Name                       | Status   | Type               | Library              | License |
+----------------------------+----------+--------------------+----------------------+---------+
......
......
| group_replication          | ACTIVE   | GROUP REPLICATION  | group_replication.so | GPL     |
+----------------------------+----------+--------------------+----------------------+---------+
46 rows in set (0.00 sec)
mysql> start group_replication;
Query OK, 0 rows affected (4.10 sec)

mysql> select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 0eb6361b-c8ff-11ea-bdb8-000c29afb37d | mgr-node3   |        3306 | ONLINE       |
| group_replication_applier | 1ca31701-c942-11ea-8881-000c29e23bd4 | proxysql    |        3306 | ONLINE       |
| group_replication_applier | d2710ec5-c900-11ea-98d1-000c29d7f446 | mgr-node2   |        3306 | ONLINE       |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
3 rows in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> select * from test.hello_world;
+----+--------+
| id | name   |
+----+--------+
|  1 | python |
|  2 | shell  |
|  3 | yaml   |
|  4 | go     |
+----+--------+
4 rows in set (0.00 sec)

测试

# mgr-node1上更新数据
mysql> select * from test.hello_world;
+----+--------+
| id | name   |
+----+--------+
|  1 | python |
|  2 | shell  |
|  3 | yaml   |
|  4 | go     |
+----+--------+
4 rows in set (0.00 sec)

mysql> update test.hello_world set id=7 where name="go";
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0
# mgr-node2和mgr-node3上查看
mysql> select * from test.hello_world;
+----+--------+
| id | name   |
+----+--------+
|  1 | python |
|  2 | shell  |
|  3 | yaml   |
|  7 | go     |
+----+--------+
4 rows in set (0.00 sec)

# 数据更新成功
相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
6月前
|
SQL 关系型数据库 MySQL
面试官:说一下MySQL主从复制的原理?
面试官:说一下MySQL主从复制的原理?
170 0
面试官:说一下MySQL主从复制的原理?
|
6月前
|
SQL 关系型数据库 MySQL
MySQL中主从复制的原理和配置命令
要原因包括提高性能、实现高可用性、数据备份和灾难恢复。了解两大线程( I/O 和 SQL)I/O线程:目的:I/O线程主要负责与MySQL服务器之外的其他MySQL服务器进行通信,以便复制(replication)数据。 功能: 当一个MySQL服务器作为主服务器(master)时,I/O线程会将变更日志(binary log)中的事件传输给从服务器(slave)。从服务器上的I/O线程负责接收主服务器的二进制日志,并将这些事件写入本地的中继日志(relay log)。 配置: 在MySQL配置文件中,你可以通过配置参数如和来启用二进制日志和指定服务器ID。log-bin server
134 1
MySQL中主从复制的原理和配置命令
|
3月前
|
SQL 关系型数据库 MySQL
【揭秘】MySQL binlog日志与GTID:如何让数据库备份恢复变得轻松简单?
【8月更文挑战第22天】MySQL的binlog日志记录数据变更,用于恢复、复制和点恢复;GTID为每笔事务分配唯一ID,简化复制和恢复流程。开启binlog和GTID后,可通过`mysqldump`进行逻辑备份,包含binlog位置信息,或用`xtrabackup`做物理备份。恢复时,使用`mysql`命令执行备份文件,或通过`innobackupex`恢复物理备份。GTID模式下的主从复制配置更简便。
284 2
|
6天前
|
存储 关系型数据库 MySQL
MySQL主从复制原理和使用
本文介绍了MySQL主从复制的基本概念、原理及其实现方法,详细讲解了一主两从的架构设计,以及三种常见的复制模式(全同步、异步、半同步)的特点与适用场景。此外,文章还提供了Spring Boot环境下配置主从复制的具体代码示例,包括数据源配置、上下文切换、路由实现及切面编程等内容,帮助读者理解如何在实际项目中实现数据库的读写分离。
MySQL主从复制原理和使用
|
6天前
|
SQL 关系型数据库 MySQL
Mysql中搭建主从复制原理和配置
主从复制在数据库管理中广泛应用,主要优点包括提高性能、实现高可用性、数据备份及灾难恢复。通过读写分离、从服务器接管、实时备份和地理分布等机制,有效增强系统的稳定性和数据安全性。主从复制涉及I/O线程和SQL线程,前者负责日志传输,后者负责日志应用,确保数据同步。配置过程中需开启二进制日志、设置唯一服务器ID,并创建复制用户,通过CHANGE MASTER TO命令配置从服务器连接主服务器,实现数据同步。实验部分展示了如何在两台CentOS 7服务器上配置MySQL 5.7主从复制,包括关闭防火墙、配置静态IP、设置域名解析、配置主从服务器、启动复制及验证同步效果。
Mysql中搭建主从复制原理和配置
|
5月前
|
Prometheus 监控 关系型数据库
数据库同步革命:MySQL GTID模式下主从配置的全面解析
数据库同步革命:MySQL GTID模式下主从配置的全面解析
548 0
|
4月前
|
缓存 运维 关系型数据库
数据库容灾 | MySQL MGR与阿里云PolarDB-X Paxos的深度对比
经过深入的技术剖析与性能对比,PolarDB-X DN凭借其自研的X-Paxos协议和一系列优化设计,在性能、正确性、可用性及资源开销等方面展现出对MySQL MGR的多项优势,但MGR在MySQL生态体系内也占据重要地位,但需要考虑备库宕机抖动、跨机房容灾性能波动、稳定性等各种情况,因此如果想用好MGR,必须配备专业的技术和运维团队的支持。 在面对大规模、高并发、高可用性需求时,PolarDB-X存储引擎以其独特的技术优势和优异的性能表现,相比于MGR在开箱即用的场景下,PolarDB-X基于DN的集中式(标准版)在功能和性能都做到了很好的平衡,成为了极具竞争力的数据库解决方案。
|
3月前
|
SQL 关系型数据库 MySQL
说一下MySQL主从复制的原理?
【8月更文挑战第24天】说一下MySQL主从复制的原理?
57 0
|
3月前
|
关系型数据库 MySQL 网络安全
全网首例!MySQL8 MGR多主一键搭建脚本分享
全网首例!MySQL8 MGR多主一键搭建脚本分享
48 1
|
3月前
|
存储 关系型数据库 MySQL
[mysql]MGR简介与部署
[mysql]MGR简介与部署