* GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源。
1. 准备工作
1.1 下载RPM包
1.2 准备YUM源
2. 初始化安装
2.1 各节点安装RPM包
2.2 初始化Primary节点
2.3 初始化Secondary 和 Arbitrator 节点
3、搭建MGR集群
体验GreatSQL 8.0.25-16各个新特性。
在刚刚发布的GreatSQL 8.0.25-16中,主打以下四个新特性:
新增仲裁节点(投票节点)角色
新增快速单主模式
新增MGR网络开销阈值
自定义选主模式
本文接下来就和大家体验下这几个新特性。
本文采用CentOS 8.4的环境进行演示。
[root@c8 ~]# cat /etc/redhat-release
CentOS Linux release 8.4.2105
[root@c8 ~]# uname -a
Linux c8 4.18.0-305.19.1.el8_4.x86_64 #1 SMP Wed Sep 15 15:39:39 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
这里推荐用RPM方式安装GreatSQL,简单省事。
打开链接 https://gitee.com/GreatSQL/GreatSQL/releases/GreatSQL-8.0.25-16 下载GreatSQL 8.0.25-16安装包,因为是CentOS 8.4 x86_64的环境,所以下载 el8*x86_64 字样的几个PRM包:
greatsql-client-8.0.25-16.1.el8.x86_64.rpm
greatsql-devel-8.0.25-16.1.el8.x86_64.rpm
greatsql-server-8.0.25-16.1.el8.x86_64.rpm
greatsql-shared-8.0.25-16.1.el8.x86_64.rpm
开始安装之前,建议先配置好yum源,方便安装一些工具。这里以阿里云的YUM源为例:
[root@c8 ~]# cd /etc/yum.repos.d
[root@c8 ~]# rm -f CentOS-Base.repo CentOS-Linux-AppStream.repo CentOS-Linux-BaseOS.repo
[root@c8 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@c8 ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@c8 ~]# yum clean all ; yum makecache
本次计划在下面3台服务器上部署MGR集群:
node | ip | datadir | port | role |
---|---|---|---|---|
mgr1 | 172.16.16.10 | /data/GreatSQL/ | 3306 | PRIMARY |
mgr2 | 172.16.16.11 | /data/GreatSQL/ | 3306 | SECONDARY |
mgr3 | 172.16.16.12 | /data/GreatSQL/ | 3306 | ARBITRATOR |
在三个节点上分别安装RPM包,首次安装可能会报告一些依赖失败:
[root@c8 ~]# rpm -ivh greatsql-*rpm
error: Failed dependencies:
/usr/bin/pkg-config is needed by greatsql-devel-8.0.25-16.1.el8.x86_64
pkgconfig(openssl) is needed by greatsql-devel-8.0.25-16.1.el8.x86_64
/usr/bin/perl is needed by greatsql-server-8.0.25-16.1.el8.x86_64
libaio.so.1()(64bit) is needed by greatsql-server-8.0.25-16.1.el8.x86_64
libaio.so.1(LIBAIO_0.1)(64bit) is needed by greatsql-server-8.0.25-16.1.el8.x86_64
libaio.so.1(LIBAIO_0.4)(64bit) is needed by greatsql-server-8.0.25-16.1.el8.x86_64
libnuma.so.1()(64bit) is needed by greatsql-server-8.0.25-16.1.el8.x86_64
libnuma.so.1(libnuma_1.1)(64bit) is needed by greatsql-server-8.0.25-16.1.el8.x86_64
libnuma.so.1(libnuma_1.2)(64bit) is needed by greatsql-server-8.0.25-16.1.el8.x86_64
net-tools is needed by greatsql-server-8.0.25-16.1.el8.x86_64
openssl is needed by greatsql-server-8.0.25-16.1.el8.x86_64
根据提示信息,只需用yum安装相应的依赖包即可:
[root@c8 ~]# yum install -y openssl-libs libaio numactl-libs net-tools perl openssl openssl-devel pkgconf-pkg-config pkgconfig
另外,建议自行手动下载jemalloc 5.2.1+版本的安装包,也安装上。下载地址:https://centos.pkgs.org/8/epel-x86_64/jemalloc-5.2.1-2.el8.x86_64.rpm.html。
[root@c8 ~]# rpm -ivh jemalloc-5.2.1-2.el8.x86_64.rpm
这些依赖包都安装完毕后,再次尝试安装GreatSQL RPM包:
[root@c8 ~]# rpm -ivh greatsql-*rpm
Verifying... ################################# [100%]
Preparing... ################################# [100%]
Updating / installing...
1:greatsql-shared-8.0.25-16.1.el8 ################################# [ 25%]
2:greatsql-client-8.0.25-16.1.el8 ################################# [ 50%]
3:greatsql-server-8.0.25-16.1.el8 ################################# [ 75%]
4:greatsql-devel-8.0.25-16.1.el8 ################################# [100%]
这就安装成功了。
因为是采用RPM方式安装,因此无需创建mysql:mysql这个用户&组。
首先,在mgr1服务器上执行初始化工作,这个服务器作为Primary节点,有些配置要和其他节点区别开来。
编辑全局配置文件 /etc/my.cnf,可参考 my.cnf for GreatSQL 8.0.25-16 配置文档模板,除了和文件目录、内存相关的选项自行调整外,只需将下面这个选项值从OFF改成ON,表示这是Primary节点角色:
# loose-group_replication_bootstrap_group = OFF
loose-group_replication_bootstrap_group = ON #这是Primary节点
新建数据库主目录 /data/GreatSQL (参考 /etc/my.cnf 中 datadir 选项值设置),并修改其用户属主:
[root@c8 ~]# mkdir -p /data/GreatSQL
[root@c8 ~]# chown -R mysql:mysql /data/GreatSQL/
再执行下面的命令进行初始化:
[root@c8 ~]# cd /data/GreatSQL
[root@c8 ~]# systemctl start mysqld
[root@c8 ~]# ls
auto.cnf ca.pem '#ib_16384_0.dblwr' ib_logfile1 mysql performance_schema slow.log
binlog.000001 client-cert.pem '#ib_16384_1.dblwr' ib_logfile2 mysql.ibd private_key.pem sys
binlog.000002 client-key.pem ib_buffer_pool ibtmp1 mysql.pid public_key.pem undo_001
binlog.index error.log ibdata1 innodb_status.1322376 mysql.sock server-cert.pem undo_002
ca-key.pem GCS_DEBUG_TRACE ib_logfile0 '#innodb_temp' mysql.sock.lock server-key.pem
这就完成安装和实例初始化工作了。
首次初始化时,MySQL会为root账户自动生成随机密码,查看error log即可:
[root@c8 GreatSQL]# grep 'password.*root' error.log
[Note] [MY-010454] [Server] A temporary password is generated for root@localhost: AvBv.ftg2,T&
复制最后那段密码串,准备登入MySQL:
[root@c8 ~]# mysql -uroot -p
Enter password: <-- 此处粘贴上述密码串
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.25-16
Copyright (c) 2021-2021 GreatDB Software Co., Ltd
Copyright (c) 2009-2021 Percona LLC and/or its affiliates
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
# 首次登入后即提醒要修改密码
[root@GreatSQL][(none)]>\s
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
# 修改密码
[root@GreatSQL][(none)]> ALTER USER USER() IDENTIFIED BY 'GreatSQL-3306';
# 查看版本号
[root@GreatSQL][(none)]> \s
...
Server version: 8.0.25-16 GreatSQL (GPL), Release 16, Revision 8bb0e5af297
...
这就在Primary节点上启动GreatSQL服务并完成数据库实例的初始化。
参考 2.2 初始化Primary节点 做法,继续对 Secondary 和 Arbitrator 节点进行初始化。
对于 Secondary 节点,编辑全局配置文件 /etc/my.cnf,基本上可直接参考 my.cnf for GreatSQL 8.0.25-16(https://gitee.com/GreatSQL/GreatSQL-Doc/blob/master/docs/my.cnf-example-greatsql-8.0.25-16) 配置文档模板,修改文件目录、内存相关的选项,其余不需要调整。
对于 Arbitrator 节点也类似,除了修改文件目录、内存相关选项外,只需修改下面这行选项值即可:
# loose-group_replication_arbitrator = 0
loose-group_replication_arbitrator = 1 <-- 设置本节点为 Arbitrator角色
修改完 /etc/my.cnf 后,同样执行 systemctl start mysqld
,即可完成 GreatSQL 实例初始化。
强烈建议用mysql shell来构建部署MGR集群。
由于在GreatSQL 8.0.25-16中增加了Arbitrator角色,官方的MySQL Shell是无法识别支持的,需要用我们提供的MySQL Shell版本。下载链接 https://gitee.com/GreatSQL/GreatSQL/releases/GreatSQL-8.0.25-16,在本文的环境中,选择 greatsql-shell-8.0.25-16-Linux-glibc2.28-x86_64.tar.xz 这个包下载。
运行 MySQL Shell 8.0.25-16 需要依赖Python 3.8版本的开发包,因此也要先安装:
[root@c8 ~]# yum install -y python38-devel
而后按照文档 xx 中的步骤构建MGR集群即可,此处不再赘述。
构建完成后,在mysql shell里就能看到各节点的状态,例如:
MySQL 172.16.16.10:33060+ ssl Py > c.status()
"primary": "172.16.16.10:3306",
"ssl": "REQUIRED",
"status": "OK",
"statusText": "Cluster is ONLINE and can tolerate up to ONE failure.",
"topology": {
"172.16.16.10:3306": {
"address": "172.16.16.10:3306",
"memberRole": "PRIMARY",
"mode": "R/W",
"readReplicas": {},
"replicationLag": null,
"role": "HA",
"status": "ONLINE",
"version": "8.0.25"
},
"172.16.16.11:3306": {
"address": "172.16.16.11:3306",
"memberRole": "SECONDARY",
"mode": "R/O",
"readReplicas": {},
"replicationLag": null,
"role": "HA",
"status": "ONLINE",
"version": "8.0.25"
},
"172.16.16.12:3306": {
"address": "172.16.16.12:3306",
"memberRole": "ARBITRATOR",
"mode": "R/O",
"readReplicas": {},
"replicationLag": null,
"role": "HA",
"status": "ONLINE",
"version": "8.0.25"
}
},
"topologyMode": "Single-Primary"
},
"groupInformationSourceMember": "172.16.16.10:3306"
可以看到,mgr3节点的角色为 ARBITRATOR,在这个节点上是看不到用户数据以及新增binlog的:
$ mysqlshow
+-------------------------------+
| Databases |
+-------------------------------+
| information_schema |
| mysql |
| mysql_innodb_cluster_metadata |
| performance_schema |
| sys |
+-------------------------------+
$ ls -la binlog.*
-rw-r----- 1 mysql mysql 179 May 13 08:08 binlog.000001
-rw-r----- 1 mysql mysql 156 May 13 08:08 binlog.000002
-rw-r----- 1 mysql mysql 58 May 13 08:08 binlog.index
而在其他节点上是正常的:
$ mysqlshow+-------------------------------+
| Databases |
+-------------------------------+
| information_schema |
| mysql |
| mysql_innodb_cluster_metadata |
| performance_schema |
| sbtest |
| sys |
+-------------------------------+
$ ls -la binlog.*
-rw-r----- 1 mysql mysql 179 May 13 08:05 binlog.000001
-rw-r----- 1 mysql mysql 3848416 May 14 02:02 binlog.000002
-rw-r----- 1 mysql mysql 58 May 13 08:06 binlog.index
这就是仲裁节点的作用了,不存储用户数据和binlog,仅参与MGR状态仲裁/投票。
如果有些事务在MGR层的耗时超过阈值,则会记录类似下面的日志:
[Note] Plugin group_replication reported: 'MGR request time:130808us, id:330606, thread_id:17368'
表示当时这个事务在MGR层的网络开销耗时130808微秒(130.808毫秒),再去查看那个时段的网络监控,分析网络延迟较大的原因。
另外两个新特性 快速单主模式(single-primary-fast-mode)以及 自定义选主,我们留到以后再介绍。
Enjoy GreatSQL :)
《深入浅出MGR》视频课程
戳此小程序即可直达B站
https://www.bilibili.com/medialist/play/1363850082?business=space_collection&business_id=343928&desc=0
文章推荐:
想看更多技术好文,点个“在看”吧!