一、准备环境
系统环境:
三台CentOS Linux release 7.6.1810
IP:10.5.0.97、10.5.0.98、10.5.0.99 #需要在三台机器上安装MySQL+MySQL-shell
安装MySQL&关闭防火墙&selinux
数据库版本:
MySQL版本:MySQL8.0.31
MySQL8.0.31具体安装步骤:
https://shackles.cn/index.php/archives/73/
配置hosts解析:
[root@mysql-3 ~]# cat >/etc/my.cnf <<EOF
10.5.0.97 mysql-1
10.5.0.98 mysql-2
10.5.0.99 mysql-3
EOF
#三台机器都要配置
二、安装MySQL-shell
rpm -ivh https://downloads.mysql.com/archives/get/p/43/file/mysql-shell-8.0.31-1.el7.x86_64.rpm
三、MySQL配置
10.5.0.97:/etc/my.cnf配置信息:
[root@mysql-1 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
#服务ID
server-id=1
#开启binlog的GTID模式,默认为OFF
gtid_mode=on
#开启后MysQL只允许能够保障事务安全,并且能够被日志记录的sQL语句被执行,默认为OFF
enforce_gtid_consistency=on
#服务器通过编写和检查每个事件的事件长度(而不是校验和)来验证它是否将完整事件写入二进制日志
binlog_checksum=none
#写集合以XXHASH64的算法进行hash
transaction_write_set_extraction = XXHASH64
#确定组复制恢复时是否应该应用SSL,通常设置为“开”,但默认设置为“关”。此项可选
loose-group_replication_recovery_use_ssl= ON
#复制组的名称,必须是有效格式的UUID,可以使用命令uuidgen生成,所有节点必须相同。默认为空
loose-group_replication_group_name = '5d76dfe1-d4f1-4896-91b6-e0ff77a26bc3'
#服务器启动时,不启用组复制集群,这在设置组复制时很重要
loose-group_replication_start_on_boot = OFF
#用于组内成员内部通信
loose-group_replication_local_address = '10.5.0.97:33060'
#设置组成员列表,新成员使用它们建立与组的连接
loose-group_replication_group_seeds = '10.5.0.97:33060,10.5.0.98:33060,10.5.0.99:33060'
#配置此服务器为引导组,这个选项必须仅在一台服务器上设置,并且仅当第一次启动组或者重新启动整个组时。成功引导组启动后,将此选项设置为关闭。
loose-group_replication_bootstrap_group = OFF
10.5.0.98:/etc/my.cnf配置信息:
[root@mysql-2 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
server-id=2
gtid_mode=on
enforce_gtid_consistency=on
binlog_checksum=none
log_slave_updates = ON
loose-group_replication_recovery_get_public_key= ON
loose-group_replication_recovery_use_ssl= ON
loose-group_replication_group_name = '5d76dfe1-d4f1-4896-91b6-e0ff77a26bc3'
loose-group_replication_start_on_boot = OFF
loose-group_replication_local_address = '10.5.0.98:33060'
loose-group_replication_group_seeds = '10.5.0.97:33060,10.5.0.98:33060,10.5.0.99:33060'
loose-group_replication_bootstrap_group = OFF
10.5.0.99:/etc/my.cnf配置信息:
[root@mysql-3 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
server-id=3
gtid_mode=on
enforce_gtid_consistency=on
binlog_checksum=none
log_slave_updates = ON
loose-group_replication_recovery_get_public_key= ON
loose-group_replication_recovery_use_ssl= ON
loose-group_replication_group_name = '5d76dfe1-d4f1-4896-91b6-e0ff77a26bc3'
loose-group_replication_start_on_boot = OFF
loose-group_replication_local_address = '10.5.0.99:33060'
loose-group_replication_group_seeds = '10.5.0.97:33060,10.5.0.98:33060,10.5.0.99:33060'
loose-group_replication_bootstrap_group = OFF
四、创建MySQL集群
1、在10.5.0.97、10.5.0.98、10.5.0.99三台机器中使用mysqlsh连接一下3台机器的MySQL,目的是让接下来的操作不会受密码限制
举例:
[root@mysql-1 ~]# mysqlsh
MySQL Shell 8.0.31
Copyright (c) 2016, 2022, 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 '\?' for help; '\quit' to exit.
MySQL JS > \c root@mysql-1:3306 #连接第一台MySQL
Creating a session to 'root@mysql-1:3306'
Please provide the password for 'root@mysql-1:3306': ********** #输入第一台MySQL的password
Save password for 'root@mysql-1:3306'? [Y]es/[N]o/Ne[v]er (default No): y #是否保存当前密码选择y
Fetching schema names for auto-completion... Press ^C to stop.
Your MySQL connection id is 13
Server version: 8.0.31 MySQL Community Server - GPL
No default schema selected; type \use <schema> to set one.
MySQL mysql-1:3306 ssl JS > \c root@mysql-2:3306 #连接第二台MySQL
Creating a session to 'root@mysql-2:3306'
Please provide the password for 'root@mysql-2:3306': ********** #输入第二台MySQL的password
Save password for 'root@mysql-2:3306'? [Y]es/[N]o/Ne[v]er (default No): y #是否保存当前密码选择y
Fetching schema names for auto-completion... Press ^C to stop.
Closing old connection...
Your MySQL connection id is 13
Server version: 8.0.31 MySQL Community Server - GPL
No default schema selected; type \use <schema> to set one.
MySQL mysql-2:3306 ssl JS > \c root@mysql-3:3306 #连接第三台MySQL
Creating a session to 'root@mysql-3:3306'
Please provide the password for 'root@mysql-3:3306': ********** #输入第三台MySQL的password
Save password for 'root@mysql-3:3306'? [Y]es/[N]o/Ne[v]er (default No): y #是否保存当前密码选择y
Fetching schema names for auto-completion... Press ^C to stop.
Closing old connection...
Your MySQL connection id is 13
Server version: 8.0.31 MySQL Community Server - GPL
No default schema selected; type \use <schema> to set one.
2、验证三台服务器实例是否满足与InnoDB Cluster使用的要求和当前机器MySQL服务是否正常;
MySQL mysql-1:3306 ssl JS > dba.configureInstance();
MySQL mysql-1:3306 ssl JS > dba.checkInstanceConfiguration("root@mysql-1:3306");
举例:MySQL-1
3、在三台机器中安装复制组插件:
mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
4、创建集群;
以下操作只在PRIMARY节点执行
MySQL mysql-1:3306 ssl JS > var cluster = dba.createCluster('myCluster');
5、往集群中加入SECONDARY:
MySQL mysql-1:3306 ssl JS > cluster.addInstance('mysql-2:3306');
添加多个SECONDARY节点的方式与上方一样。
6、查看集群状态;
MySQL mysql-1:3306 ssl JS > cluster.status()
7、查询节点信息:
mysql> select * from performance_schema.replication_group_members;
8、测试集群
在PRIMARY节点可进行增删改查操作,SECONDARY只能查。
宕掉PRIMARY节点后,会从另外两个中选举出一个作为PRIMARY节点,并且选举出新PRIMARY具有增删改查权限。原宕掉的PRIMARY恢复后,不会成为PRIMARY节点而会变成和其他SECONDARY节点一样具有查权限。
仅登录用户可评论,点击 登录