Fork me on GitHub

从已有的组复制搭建InnoDB Cluster环境

本文描述了如何基于已有的MySQL Group Replication,创建一个 Innodb cluster。

1. 已有的MySQL Group Replication 环境信息

ip地址 主机名 server_id
172.17.84.71 mysql001 1
172.17.84.72 mysql002 2
172.17.84.73 mysql003 3

查看组复制当前状态

1
2
3
4
5
6
7
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 80af8598-1520-11e7-a8b9-08002730b4d8 | mysql001 | 3306 | ONLINE |
| group_replication_applier | 8abf4eab-1521-11e7-9cc9-080027b95fc4 | mysql002 | 3306 | ONLINE |
| group_replication_applier | dcd3068d-15bc-11e7-b264-080027dad0d6 | mysql003 | 3306 | ONLINE |

2. Yum 安装MySQL Shell

wget https://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
rpm -ivh mysql57-community-release-el7-10.noarch.rpm
yum install mysql-shell -y

3. 创建cluster

通过指定 adoptFromGR option,使用dba.createCluster()

1
2
3
4
5
6
7
8
9
10
mysqlsh --uri root@172.17.84.71:3306
mysql-js> var cluster = dba.createCluster('prodCluster', {adoptFromGR: true});
A new InnoDB cluster will be created on instance 'root@172.17.84.7:3306'.
Creating InnoDB cluster 'prodCluster' on 'root@172.17.84.72:3306'...
Adding Seed Instance...
Cluster successfully created. Use Cluster.addInstance() to add MySQL instances.
At least 3 instances are needed for the cluster to be able to withstand up to
one server failure.

查看cluster状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
mysql-js> var cluster=dba.getCluster()
mysql-js> cluster.status()
{
"clusterName": "prodCluster",
"defaultReplicaSet": {
"name": "default",
"primary": "172.17.84.71:3306",
"status": "OK",
"statusText": "Cluster is ONLINE and can tolerate up to ONE failure.",
"topology": {
"172.17.84.71:3306": {
"address": "172.17.84.71:3306",
"mode": "R/W",
"readReplicas": {},
"role": "HA",
"status": "ONLINE"
},
"mysql002:3306": {
"address": "mysql002:3306",
"mode": "R/O",
"readReplicas": {},
"role": "HA",
"status": "ONLINE"
},
"mysql003:3306": {
"address": "mysql003:3306",
"mode": "R/O",
"readReplicas": {},
"role": "HA",
"status": "ONLINE"
}
}
}
}

4. 持久化配置文件

对于已经在cluster中的实例,可以持久化cluster的配置

1
dba.configureLocalInstance(root@localhost:3306)

查看配置文件的变化my.cnf

5.简单测试Failover

关闭mysql001实例

1
systemctl stop mysqld

重启mysql001实例

1
systemctl start mysqld

查看cluster状态,发现Primary Master已经切换到mysql002

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
mysql-js> cluster.status()
{
"clusterName": "prodCluster",
"defaultReplicaSet": {
"name": "default",
"primary": "172.17.84.72:3306",
"status": "OK",
"statusText": "Cluster is ONLINE and can tolerate up to ONE failure.",
"topology": {
"172.17.84.72:3306": {
"address": "172.17.84.72:3306",
"mode": "R/W",
"readReplicas": {},
"role": "HA",
"status": "ONLINE"
},
"mysql001:3306": {
"address": "mysql001:3306",
"mode": "R/O",
"readReplicas": {},
"role": "HA",
"status": "ONLINE"
},
"mysql003:3306": {
"address": "mysql003:3306",
"mode": "R/O",
"readReplicas": {},
"role": "HA",
"status": "ONLINE"
}
}
}
}

6.createCluster语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
mysql-js> dba.help(createCluster());
ReferenceError: createCluster is not defined
mysql-js> dba.help('createCluster');
Creates a MySQL InnoDB cluster.
SYNTAX
<Dba>.createCluster(name[, options])
WHERE
name: The name of the cluster object to be created.
options: Dictionary with options that modify the behavior of this function.
DESCRIPTION
Creates a MySQL InnoDB cluster taking as seed instance the active global
session.
The options dictionary can contain the next values:
- clusterAdminType: defines the type of management to be done on the cluster
instances.
- multiMaster: boolean value used to define an InnoDB cluster with multiple
writable instances.
- force: boolean, confirms that the multiMaster option must be applied.
- adoptFromGR: boolean value used to create the InnoDB cluster based on
existing replication group.
- memberSslMode: SSL mode used to configure the members of the cluster.
- ipWhitelist: The list of hosts allowed to connect to the instance for group
replication.
The values for clusterAdminType options include: local, manual, guided or ssh,
however, at the moment only local is supported and is used as default value if
this attribute is not specified.
A InnoDB cluster may be setup in two ways:
- Single Master: One member of the cluster allows write operations while the
rest are in read only mode.
- Multi Master: All the members in the cluster support both read and write
operations.
By default this function create a Single Master cluster, use the multiMaster
option set to true if a Multi Master cluster is required.
The memberSslMode option supports these values:
- REQUIRED: if used, SSL (encryption) will be enabled for the instances to
communicate with other members of the cluster
- DISABLED: if used, SSL (encryption) will be disabled
- AUTO: if used, SSL (encryption) will be enabled if supported by the
instance, otherwise disabled
If memberSslMode is not specified AUTO will be used by default.
The ipWhitelist format is a comma separated list of IP addresses or subnet CIDR
notation, for example: 192.168.1.0/24,10.0.0.1. By default the value is set to
AUTOMATIC, allowing addresses from the instance private network to be
automatically set for the whitelist.

参考

  1. Creating an InnoDB Cluster From an Existing Group Replication Deployment
  2. https://ronniethedba.wordpress.com/2017/04/23/creating-an-innodb-cluster-router-from-an-existing-group-replication-deployment/
好记性不如烂笔头,生命不息,学习不止!

分享