MySQL Shell 8.0  /  MySQL InnoDB ReplicaSet  /  Creating an InnoDB ReplicaSet

9.3 Creating an InnoDB ReplicaSet

Once you have configured your instances, create an InnoDB ReplicaSet by completing the following steps:

  1. Connect to an instance and use dba.createReplicaSet() to create a managed ReplicaSet that uses MySQL asynchronous replication, rather than MySQL Group Replication used by InnoDB Cluster. The MySQL instance, which MySQL Shell is connected to, is used as the initial primary of the ReplicaSet.

    The dba.createReplicaSet() operation performs several checks to ensure that the instance state and configuration are compatible with a managed ReplicaSet, and if so, a metadata schema is initialized on the instance.

    If the ReplicaSet is created successfully, a ReplicaSet object is returned. Therefore, it is best practice to assign the returned ReplicaSet to a variable. This enables you to work with the ReplicaSet, for example by calling the <ReplicaSet>status() operation. To create a ReplicaSet named example on instance rs-1 and assign it to the rs variable, issue:

    mysql-js> \connect root@rs-1:3306
    ...
    mysql-js> var rs = dba.createReplicaSet("example")
    A new replicaset with instance 'rs-1:3306' will be created.
    
    * Checking MySQL instance at rs-1:3306
    
    This instance reports its own address as rs-1:3306
    rs-1:3306: Instance configuration is suitable.
    
    * Updating metadata...
    
    ReplicaSet object successfully created for rs-1:3306.
    Use rs.add_instance() to add more asynchronously replicated instances to this replicaset
    and rs.status() to check its status.
  2. Use the returned ReplicaSet object to verify that the operation was successful. For example, this provides the ReplicaSet.status() operation, which displays information about the ReplicaSet. The returned ReplicaSet is already assigned to the variable rs, so issue:

    mysql-js> rs.status()
    {
        "replicaSet": {
            "name": "example",
            "primary": "rs-1:3306",
            "status": "AVAILABLE",
            "statusText": "All instances available.",
            "topology": {
                "rs-1:3306": {
                    "address": "rs-1:3306",
                    "instanceRole": "PRIMARY",
                    "mode": "R/W",
                    "status": "ONLINE"
                }
            },
            "type": "ASYNC"
        }
    }

    This output shows that the ReplicaSet named example has been created, and that the primary is rs-1. Currently, there is only one instance, and the next task is to add more instances to the ReplicaSet.

InnoDB ReplicaSet replicationAllowedHost

When creating an InnoDB ReplicaSet using MySQL Shell 8.0.28 and later, if you have security requirements that want all accounts created automatically by AdminAPI to have strict authentication requirements, you can set a value for the replicationAllowedHost configuration option of the ReplicaSet. The replicationAllowedHost MySQL Shell option allows you to set internally managed replication accounts for a ReplicaSet to a strict subnet based filter instead of the default wildcard value of %.The replicationAllowedHost option can take a string value. For example, to set the replicationAllowedHost to 192.0.2.0/24, issue:

mysql-js> var rs = dba.createReplicaSet('example', {replicationAllowedHost:'192.0.2.0/24'})
        A new replicaset with instance 'rs-1:3306' will be created.

* Checking MySQL instance at rs-1:3306

This instance reports its own address as rs-1:3306
rs-1:3306: Instance configuration is suitable.

* Updating metadata...

ReplicaSet object successfully created for rs-1:3306.
Use rs.addInstance() to add more asynchronously replicated instances to this replicaset and rs.status() to check its status.

An InnoDB ReplicaSet can be modified after creation to set the variable replicationAllowedHost through the setOption configuration option, by issuing:

mysql-js> rs.setOption('replicationAllowedHost', '192.0.2.0/24')