Create an InnoDB Replica Set With MySQL Shell

Using MySQL Shell, we can quickly and easily create an InnoDB replica set. With as little as two commands, we can create a replica set, add a secondary instance, and clone data from our primary instance to a secondary instance. In this post, we will demonstrate how to accomplish this.
The Setup
Before building this example, I deployed two MySQL sandbox instances that use ports 5555 and 5556. The instance running on port 5555 will be our primary instance, and the one on port 5556 will be our secondary instance. I loaded a schema named mysql_shorts
into the instance on port 5555, so we have data to replicate.
To show the schemas on each instance, we run the command:
On the instance running on port 5555, I see the following results:
On the instance running on port 5556, I see the following results:
Note that these results are the same, except the mysql_shorts
schema exists in the first instance but not the second.
Create Replica Set
To create a replica set, we connect to the instance on port 5555 using MySQL Shell and run the following command in JavaScript mode.
This command will set the value of rs
to the new replica set that is created. When this command completes, we will see output similar to the text below.
Check the Status
We can check the status of our new replica set by running the command below.
The output for this command will display the status of our replica set as a JSON object.
We can see only a single instance as part of the replica set named demo_set
.
Add a Secondary Instance
The next step in creating an InnoDB replica set is to add one or more secondary instances. We will add a single secondary instance. While still connected to our primary instance with MySQL Shell, we run the following command:
This command calls the addInstance()
method on our new replica set object, rs
. When we call this method, we pass a single argument: the connection string to the instance we wish to add. In this case, we want to connect to an instance on our local system on port 5556 using the user root
.
Note: I am using the root
user in this example for ease of use. In an actual production system, you should avoid using a root
for any processes, including replication.
You will be asked to select a recovery method when this command is run. I chose Clone
in this example because it is the easiest way to move data from the primary to a secondary instance.
The output from running this command will resemble the text below.
The messages we receive show the progress of the replication and that the instance on port 5556 was added to the replica set and is replication from the instance on port 5556.
Check the Status
With a secondary instance added to our replica set. Let’s take a look at the status of our replica set by running the command below:
The status of our replica set now shows the newly added secondary node.
Checking Our Work
Now that our replica set is running, let’s check to ensure it works as expected.
Secondary Schemas
To see the schemas now on the secondary instance (the one on port 5556), we connect to that instance with MySQL Shell and run the following command:
The results of this command resemble the text below.
There are now two new schemas.
mysql_shorts
- The schema copied from our primary instance.mysql_innodb_cluster_metadata
- A schema that is used to manage replication.
Insert Row Into Primary
We will insert a row of data into a table named user
in the mysql_shorts
schema. This table has four columns.
id
- The primary key of the table.first_name
- The user’s first name.last_name
- The user’s last name.email
- The user’s email address.
We connect to the primary instance (on port 5555) using MySQL Shell and run the following query:
To verify the row was added as we expected, we run the following query:
The result of this query is:
We can see that the user Skippy Dinglehoffer was added to the table on the primary instance.
Check the Replica
To see if the replication works as it should, we connect to the secondary instance (the one on port 5556) using MySQL Shell and execute the same query as above.
The output from this query will match the output from the query when run on the primary instance.
The fact that the user we added to the primary instance is now on the secondary instance tells us that the replication is working as expected.
Check read Only on Secondary
When creating a replica set the way we did, all secondary instances are created as read-only
. This means that we cannot insert data directly into any secondary instance.
We can check this by trying to insert a row of data into the mysql_shorts.user
table by running the following query.
We should get the following message to let us know the secondary instance is indeed read-only
.
Wrap Up
Using MySQL Shell, we can quickly and easily create InnoDB replica sets. With just a few commands, we can create an InnoDB replica set and add secondary instances to it. We can also test the commands to create an InnoDB replica set using MySQL sandbox instances and MySQL Shell. For more information on replica sets, check out the documentation.
Related Entries
- Running External Scripts in MySQL Shell
- Executing System Commands in MySQL Shell
- Getting Help in MySQL Shell
- Sandboxes in MySQL Shell
- Server Upgrade Checks with MySQL Shell
- MySQL Connection Status with MySQL Shell
- Managing MySQL Shell Configuration Options
- Advanced Data Dump with MySQL Shell
- Advanced Data Load with MySQL Shell
- MySQL Shell CLI Integration
- MySQL Shell Table Export Utility
- Copy Data Directly To A MySQL Instance With MySQL Shell
- Using MySQL Shell to Get MySQL Diagnostic Information
- Customizing The MySQL Shell Prompt
- Extending MySQL Shell with Startup Scripts
- Extending MySQL Shell with Plugins