#### 18.2.1.6 向组中添加实例

[](<>)

此时，该组中有一个成员，服务器 s1，其中有一些数据。现在是时候通过添加之前配置的其他两个服务器来扩展组了。

##### 18.2.1.6.1 添加第二个实例

[](<>)

为了添加第二个实例，服务器 s2，首先为其创建配置文件。配置类似于用于服务器 s1 的配置，除了诸如[`server_id`](replication-options.html#sysvar_server_id).这些不同的行在下面的列表中突出显示。

```
[mysqld]

#
# Disable other storage engines
#
disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"

#
# Replication configuration parameters
#
server_id=2
gtid_mode=ON
enforce_gtid_consistency=ON
binlog_checksum=NONE           # Not needed from 8.0.21

#
# Group Replication configuration
#
plugin_load_add='group_replication.so'
group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
group_replication_start_on_boot=off
group_replication_local_address= "s2:33061"
group_replication_group_seeds= "s1:33061,s2:33061,s3:33061"
group_replication_bootstrap_group= off
```

与服务器 s1 的过程类似，使用选项文件启动服务器。然后按如下方式配置分布式恢复凭据。这些命令与设置服务器 s1 时使用的命令相同，因为用户在组内共享。此成员需要在中配置相同的复制用户[第 18.2.1.3 节，“分布式恢复的用户凭证”](group-replication-user-credentials.html).如果您依赖分布式恢复在所有成员上配置用户，则当 s2 连接到种子 s1 时，复制用户将被复制或克隆到 s1。如果在 s1 上配置用户凭据时未启用二进制日志记录，并且未使用远程克隆操作进行状态传输，则必须在 s2 上创建复制用户。在这种情况下，连接到 s2 并发出：

```
SET SQL_LOG_BIN=0;
CREATE USER rpl_user@'%' IDENTIFIED BY 'password';
GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
GRANT BACKUP_ADMIN ON *.* TO rpl_user@'%';
FLUSH PRIVILEGES;
SET SQL_LOG_BIN=1;
```

如果您使用[`将复制源更改为`](change-replication-source-to.html)\|[`将主更改为`](change-master-to.html)声明，然后发出以下声明：

```
CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='password' \\
	FOR CHANNEL 'group_replication_recovery';

Or from MySQL 8.0.23:
CHANGE REPLICATION SOURCE TO SOURCE_USER='rpl_user', SOURCE_PASSWORD='password' \\
	FOR CHANNEL 'group_replication_recovery';
```

提示

如果您使用缓存 SHA-2 身份验证插件，这是 MySQL 8 中的默认插件，请参阅[第 18.6.3.1.1 节，“使用缓存 SHA-2 身份验证插件的复制用户”](group-replication-secure-user.html#group-replication-caching-sha2-user-credentials).

如有必要，安装 Group Replication 插件，请参阅[第 18.2.1.4 节，“启动组复制”](group-replication-launching.html).

启动 Group Replication 和 s2 启动加入组的过程。

```
mysql> START GROUP_REPLICATION;
```

或者，如果您要为分布式恢复提供用户凭据[`开始 GROUP_REPLICATION`](start-group-replication.html)声明（你可以从 MySQL 8.0.21）：

```
mysql> START GROUP_REPLICATION USER='rpl_user', PASSWORD='password';
```

与之前在 s1 上执行的步骤不同，这里有一个不同之处在于您执行的操作*不是*需要引导组，因为该组已经存在。换句话说，在 s2[`group_replication_bootstrap_group`](group-replication-options.html#sysvar_group_replication_bootstrap_group)被设定为`离开`, 你不发出`SET GLOBAL group_replication_bootstrap_group=ON;`在开始组复制之前，因为组已经由服务器 s1 创建和引导。此时服务器 s2 只需添加到已经存在的组中。

提示

当组复制成功启动并且服务器加入组时，它会检查[`super_read_only`](server-system-variables.html#sysvar_super_read_only)多变的。通过设置[`super_read_only`](server-system-variables.html#sysvar_super_read_only)在成员的配置文件中设置为 ON，您可以确保在启动 Group Replication 时因任何原因失败的服务器不接受事务。如果服务器应该作为读写实例加入组，例如作为单主组中的主或作为多主组的成员，当[`super_read_only`](server-system-variables.html#sysvar_super_read_only)变量设置为 ON，然后在加入组时设置为 OFF。

检查[`performance_schema.replication_group_members`](performance-schema-replication-group-members-table.html)表格再次显示现在有两个`在线的`组中的服务器。

```
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION | MEMBER_COMMUNICATION_STACK |
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
| group_replication_applier | 395409e1-6dfa-11e6-970b-00212844f856 |   s1        |        3306 | ONLINE       | PRIMARY     | 8.0.27         | XCom                       |
| group_replication_applier | ac39f1e6-6dfa-11e6-a69d-00212844f856 |   s2        |        3306 | ONLINE       | SECONDARY   | 8.0.27         | XCom                       |
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
```

当 s2 尝试加入群组时，[第 18.5.4 节，“分布式恢复”](group-replication-distributed-recovery.html)确保 s2 应用了与 s1 相同的事务。此过程完成后，s2 可以作为成员加入该组，此时将其标记为`在线的`.换句话说，它一定已经自动赶上了服务器 s1。一旦 s2 是`在线的`，然后它开始处理与该组的事务。验证 s2 确实与服务器 s1 同步，如下所示。

```
mysql> SHOW DATABASES LIKE 'test';
+-----------------+
| Database (test) |
+-----------------+
| test            |
+-----------------+

mysql> SELECT * FROM test.t1;
+----+------+
| c1 | c2   |
+----+------+
|  1 | Luis |
+----+------+

mysql> SHOW BINLOG EVENTS;
+---------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+
| Log_name      | Pos  | Event_type     | Server_id | End_log_pos | Info                                                               |
+---------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+
| binlog.000001 |    4 | Format_desc    |         2 |         123 | Server ver: 8.0.28-log, Binlog ver: 4                              |
| binlog.000001 |  123 | Previous_gtids |         2 |         150 |                                                                    |
| binlog.000001 |  150 | Gtid           |         1 |         211 | SET @@SESSION.GTID_NEXT= 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:1'  |
| binlog.000001 |  211 | Query          |         1 |         270 | BEGIN                                                              |
| binlog.000001 |  270 | View_change    |         1 |         369 | view_id=14724832985483517:1                                        |
| binlog.000001 |  369 | Query          |         1 |         434 | COMMIT                                                             |
| binlog.000001 |  434 | Gtid           |         1 |         495 | SET @@SESSION.GTID_NEXT= 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:2'  |
| binlog.000001 |  495 | Query          |         1 |         585 | CREATE DATABASE test                                               |
| binlog.000001 |  585 | Gtid           |         1 |         646 | SET @@SESSION.GTID_NEXT= 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:3'  |
| binlog.000001 |  646 | Query          |         1 |         770 | use `test`; CREATE TABLE t1 (c1 INT PRIMARY KEY, c2 TEXT NOT NULL) |
| binlog.000001 |  770 | Gtid           |         1 |         831 | SET @@SESSION.GTID_NEXT= 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:4'  |
| binlog.000001 |  831 | Query          |         1 |         890 | BEGIN                                                              |
| binlog.000001 |  890 | Table_map      |         1 |         933 | table_id: 108 (test.t1)                                            |
| binlog.000001 |  933 | Write_rows     |         1 |         975 | table_id: 108 flags: STMT_END_F                                    |
| binlog.000001 |  975 | Xid            |         1 |        1002 | COMMIT /* xid=30 */                                                |
| binlog.000001 | 1002 | Gtid           |         1 |        1063 | SET @@SESSION.GTID_NEXT= 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:5'  |
| binlog.000001 | 1063 | Query          |         1 |        1122 | BEGIN                                                              |
| binlog.000001 | 1122 | View_change    |         1 |        1261 | view_id=14724832985483517:2                                        |
| binlog.000001 | 1261 | Query          |         1 |        1326 | COMMIT                                                             |
+---------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+
```

如上所示，第二台服务器已添加到组中，并自动复制了服务器 s1 的更改。换句话说，在 s2 加入组之前应用在 s1 上的事务已被复制到 s2。

##### 18.2.1.6.2 添加其他实例

[](<>)

向组中添加其他实例与添加第二台服务器的步骤顺序基本相同，除了必须更改配置，因为它必须针对服务器 s2。总结所需的命令：

1.  创建配置文件。

    ```
    [mysqld]

    #
    # Disable other storage engines
    #
    disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"

    #
    # Replication configuration parameters
    #
    server_id=3
    gtid_mode=ON
    enforce_gtid_consistency=ON
    binlog_checksum=NONE           # Not needed from 8.0.21

    #
    # Group Replication configuration
    #
    plugin_load_add='group_replication.so'
    group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
    group_replication_start_on_boot=off
    group_replication_local_address= "s3:33061"
    group_replication_group_seeds= "s1:33061,s2:33061,s3:33061"
    group_replication_bootstrap_group= off
    ```

2.  启动服务器并连接到它。为分布式恢复创建复制用户。

    ```
    SET SQL_LOG_BIN=0;
    CREATE USER rpl_user@'%' IDENTIFIED BY 'password';
    GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
    GRANT BACKUP_ADMIN ON *.* TO rpl_user@'%';
    FLUSH PRIVILEGES;
    SET SQL_LOG_BIN=1;
    ```

    如果您使用[`将复制源更改为`](change-replication-source-to.html)\|[`将主更改为`](change-master-to.html)声明，然后发出以下声明：

    ```
    CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='password' \\
    	FOR CHANNEL 'group_replication_recovery';

    Or from MySQL 8.0.23:
    CHANGE REPLICATION SOURCE TO SOURCE_USER='rpl_user', SOURCE_PASSWORD='password' \\
    	FOR CHANNEL 'group_replication_recovery';
    ```

3.  如有必要，安装组复制插件。

    ```
    INSTALL PLUGIN group_replication SONAME 'group_replication.so';
    ```

4.  启动组复制。

    ```
    mysql> START GROUP_REPLICATION;
    ```

    或者，如果您要为分布式恢复提供用户凭据[`开始 GROUP_REPLICATION`](start-group-replication.html)声明（你可以从 MySQL 8.0.21）：

    ```
    mysql> START GROUP_REPLICATION USER='rpl_user', PASSWORD='password';
    ```

    此时服务器 s3 已启动并运行，已加入组并赶上了组中的其他服务器。咨询[`performance_schema.replication_group_members`](performance-schema-replication-group-members-table.html)表再次证实了这种情况。


```
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION | MEMBER_COMMUNICATION_STACK |
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
| group_replication_applier | 395409e1-6dfa-11e6-970b-00212844f856 |   s1        |        3306 | ONLINE       | PRIMARY     | 8.0.27         | XCom                       |
| group_replication_applier | 7eb217ff-6df3-11e6-966c-00212844f856 |   s3        |        3306 | ONLINE       | SECONDARY   | 8.0.27         | XCom                       |
| group_replication_applier | ac39f1e6-6dfa-11e6-a69d-00212844f856 |   s2        |        3306 | ONLINE       | SECONDARY   | 8.0.27         | XCom                       |
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
```

在服务器 s2 或服务器 s1 上发出相同的查询会产生相同的结果。此外，您可以验证服务器 s3 是否已赶上：

```
mysql> SHOW DATABASES LIKE 'test';
+-----------------+
| Database (test) |
+-----------------+
| test            |
+-----------------+

mysql> SELECT * FROM test.t1;
+----+------+
| c1 | c2   |
+----+------+
|  1 | Luis |
+----+------+

mysql> SHOW BINLOG EVENTS;
+---------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+
| Log_name      | Pos  | Event_type     | Server_id | End_log_pos | Info                                                               |
+---------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+
| binlog.000001 |    4 | Format_desc    |         3 |         123 | Server ver: 8.0.28-log, Binlog ver: 4                              |
| binlog.000001 |  123 | Previous_gtids |         3 |         150 |                                                                    |
| binlog.000001 |  150 | Gtid           |         1 |         211 | SET @@SESSION.GTID_NEXT= 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:1'  |
| binlog.000001 |  211 | Query          |         1 |         270 | BEGIN                                                              |
| binlog.000001 |  270 | View_change    |         1 |         369 | view_id=14724832985483517:1                                        |
| binlog.000001 |  369 | Query          |         1 |         434 | COMMIT                                                             |
| binlog.000001 |  434 | Gtid           |         1 |         495 | SET @@SESSION.GTID_NEXT= 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:2'  |
| binlog.000001 |  495 | Query          |         1 |         585 | CREATE DATABASE test                                               |
| binlog.000001 |  585 | Gtid           |         1 |         646 | SET @@SESSION.GTID_NEXT= 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:3'  |
| binlog.000001 |  646 | Query          |         1 |         770 | use `test`; CREATE TABLE t1 (c1 INT PRIMARY KEY, c2 TEXT NOT NULL) |
| binlog.000001 |  770 | Gtid           |         1 |         831 | SET @@SESSION.GTID_NEXT= 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:4'  |
| binlog.000001 |  831 | Query          |         1 |         890 | BEGIN                                                              |
| binlog.000001 |  890 | Table_map      |         1 |         933 | table_id: 108 (test.t1)                                            |
| binlog.000001 |  933 | Write_rows     |         1 |         975 | table_id: 108 flags: STMT_END_F                                    |
| binlog.000001 |  975 | Xid            |         1 |        1002 | COMMIT /* xid=29 */                                                |
| binlog.000001 | 1002 | Gtid           |         1 |        1063 | SET @@SESSION.GTID_NEXT= 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:5'  |
| binlog.000001 | 1063 | Query          |         1 |        1122 | BEGIN                                                              |
| binlog.000001 | 1122 | View_change    |         1 |        1261 | view_id=14724832985483517:2                                        |
| binlog.000001 | 1261 | Query          |         1 |        1326 | COMMIT                                                             |
| binlog.000001 | 1326 | Gtid           |         1 |        1387 | SET @@SESSION.GTID_NEXT= 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:6'  |
| binlog.000001 | 1387 | Query          |         1 |        1446 | BEGIN                                                              |
| binlog.000001 | 1446 | View_change    |         1 |        1585 | view_id=14724832985483517:3                                        |
| binlog.000001 | 1585 | Query          |         1 |        1650 | COMMIT                                                             |
+---------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+
```
