This blog is about how to perform replication for one MySQL primary and one MySQL replica instance, 8.0 Reference
- Replication is asynchronous by default, it could also be synchronous and
semisynchronous.
- synchronous replication is a characteristic of NDB Cluster.
- semisynchronous replication is supported in addition to the built-in
asynchronous replication.
- With semisynchronous replication, a commit performed on the source blocks before returning to the session that performed the transaction until at least one replica acknowledges that it has received and logged the events for the transaction.
- Replicas do not need to be connected permanently(replication can be paused) to receive updates from a source.
- Depending on the configuration, you can replicate all databases, selected databases, or even selected tables within a database.
Goal
Here we focus on binary log file position based replication with MySQL version 8.0.42, Reference and MySQL Shell.
Vagrant
The playground is managed by Vagrant, please launch it before move on to the next section.
Briefly, you will have a primary MySQL with bin log enabled and GTID disabled, a replica MySQL the same MySQL version with bin log off and unique server ID set.
Initial Data Seed
On primary, let’s create an animals DB and tables:
1 | CREATE DATABASE IF NOT EXISTS animals; |
Create replication user
Go to the primary and run below commands to create a dedicated replication user
repl
with password ReplP2025!!
for all hosts:
1 | -- Go to the primary and run: |
At this point, you are able to connect to primary from replica using user repl
:
1 | # 172.20.21.30 is primary IP |
Grant privileges
For replication user, we need below privileges to be granted to perform dump, load and replication actions.
Go to the primary and run below commands by root:
1 | -- for replication user to connect to the primary (master) and read the binary |
Data Dump
We use mysqlsh dump utilities instead of mysqldump.
MySQL Shell provides parallel dumping with multiple threads, file compression, and progress information display, etc.
Go to the replica and run below commands:
1 | # Create a folder to host dump |
Run mysqlsh as below in replica:
1 | # The dump log file for debugging. |
The output is like:
1 | WARNING: Using a password on the command line interface can be insecure. |
Data Load
Go to the replica and run below commands:
1 | # I am using Unix socket instead of TCP/IP to connect local MySQL. Since I don't |
The output is like:
1 | WARNING: Using a password on the command line interface can be insecure. |
If the load is good, you should find the animals.animals
table presents:
1 | mysql> SELECT * FROM animals.animals; |
Obtain binlog coordinates
Since we use consistent=true
in mysqsh dump, we can find binlog file and
position from the @.json
file in /etc/mysqlsh-dump
folder:
1 | { |
Start Replication
We need to kick off the replication within the expiration of the bin log in the primary after the data dump and load:
1 | mysql> show replica status\G |
Run replication setup in replica:
1 | -- You can set SSL if it is enabled, the fill options please see |
Check replication status, the Replica_IO_Running
, Replica_SQL_Running
are
No
since we haven’t started it yet:
1 | mysql> show replica status\G |
Now start the replication:
1 | mysql> START REPLICA; |
Check the replication status again, the Replica_IO_Running
, Replica_SQL_Running
should be YES
if no issue and Replica_SQL_Running_State
contains the current
state:
1 | mysql> show replica status\G |
Check replication
On Primary, you can show what replicas actively connected to it, if you don’t start the replication on replica, you won’t see it here:
1 | -- 1001 is the replica1 server ID |
Show all currently running threads, includes the replication:
1 | mysql> show processlist\G |
Verification
Now, let’s insert new row on animals.animals
table on primary:
1 | INSERT INTO animals.animals (name, species, age) VALUES |
It will be replicated to replica.