You're reading from MariaDB Cookbook
Replication is what allows MariaDB to scale to thousands of servers, millions of users, and petabytes of data. But let's not get ahead of ourselves. Replication on a small scale is a great way to grow the number of users our application can support with minimal effort. As we gain users, we can grow the number of replication servers to match.
There are many different ways to set up how we do replication. In this chapter, we'll only touch on a couple of basic ones: a single master to multiple slaves, and multiple masters to a single slave.
Setting up replication is not hard as long as all the various bits are in place. This recipe is all about the most basic concept of replication topologies; a single master server replicating to multiple slaves is shown in the following diagram:
This recipe assumes that we have three servers. The servers are named db01
, db02
, and db03
, and they reside on the 192.168.4.0
network with IP addresses 192.168.4.101
.to 192.168.4.103
. One server, db01
, will be our replication master, and the other two will be our replication slaves.
For the purposes of this recipe, the servers are assumed to contain fresh installs of MariaDB, with just the default databases set up.
On all three hosts, launch the
mysql
command-line client and connect to the local MariaDB server with theroot
user (or another user with theGRANT
privilege) and run the following command:GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO replicant@'192.168.4.%' IDENTIFIED BY 'sup3rs3kr37p455w0rd...
Global Transaction ID (GTID) is a new feature in MariaDB 10.0 and above. It helps us achieve greater reliability and flexibility with our replication.
This recipe builds upon the previous one, so to get ready for this recipe, simply set up a basic replication as described in the Setting up replication recipe.
On both our replication slave servers, launch the
mysql
command-line client and run the following commands:STOP SLAVE; CHANGE MASTER TO MASTER_USE_GTID = SLAVE_POS; START SLAVE;
Check on the status of our replication slave servers with the following command:
SHOW ALL SLAVES STATUS\G
Look at the bottom of the output for the following lines (the
Gtid_Slave_Pos
value will likely be different and the lines are separated by several other lines in the output):Using_Gtid: Slave_Pos Gtid_Slave_Pos: 0-101-2320
Insert more data into our
temp.doctors
table on the replication master server, and then run the followingSELECT
statement on...
A familiar replication topology is one where we have a single master server and several slave servers. Another alternative topology is where we have a single slave server connected to multiple master servers. This is called multisource replication.
For this recipe, we'll be working on the assumption that we have three servers, db01
, db02
, and db03
, which are each running a fresh install of MariaDB. The first two will be our replication masters, and the last one will be our replication slave as shown in the following diagram:
We'll further assume that all three servers are on the same subnet, 192.168.4.0
, with the final part of their individual IP addresses being 101
, 102
, and 103
, respectively.
When using replication, it's popular to set binlog_format
to row
. The only issue with this is that when we look at the binlog, it is harder to read because the statements aren't included. We can see the changes but not the SQL statement that made the changes.
For this recipe, we'll assume that we've set up replication as described in either the Setting up replication or Using multisource replication recipes earlier in this chapter. Pick a master server and a slave server to use. In this recipe, we'll call the master server db01
and the slave server db03
.
On
db01
, edit the systemmy.cnf
ormy.ini
file and add the following to the[mysqld]
section:binlog_format = row binlog_annotate_row_events
On
db03
, edit the systemmy.cnf
ormy.ini
file and add the following to the[mysqld]
section:binlog_format = row replicate_annotate_row_events
Restart MariaDB on both servers.
On
db01
, launch themysql
command-line client and...
A rare, but still possible, problem can occur if the filesystem where we store our binary and relay logs gets corrupted. It can be especially damaging if we don't detect it early on. Event checksums are a way to detect this quickly.
For this recipe, we'll assume that we've set up replication as described in either the Setting up replication or the Using multisource replication recipes earlier in this chapter. Pick a master server and a slave server to use. In this recipe, we'll call db01
as the master server and db03
as the slave server.
On
db01
, launch themysql
command-line client and run the following commands:SET GLOBAL BINLOG_CHECKSUM = 1; SET GLOBAL MASTER_VERIFY_CHECKSUM = 1;
On
db03
, launch themysql
command-line client and run the following command:SET GLOBAL SLAVE_SQL_VERIFY_CHECKSUM = 1;
Sometimes, we want to skip replicating certain events to our replication slave servers. MariaDB lets us do this dynamically.
For this recipe, we'll assume that we've set up replication as described in either the Setting up replication or Using multisource replication recipes earlier in this chapter. Pick a master server and a slave server to use. In this recipe, we'll call db01
as the master server and db03
as the slave server.
On
db01
, launch themysql
command-line client and run the following command to turn on replication skipping:SET @@skip_replication=1;
On
db01
, create an empty database:CREATE DATABASE w;
On
db03
, run the following commands to turn off replication of skipped events:STOP SLAVE; SET GLOBAL REPLICATE_EVENTS_MARKED_FOR_SKIP = FILTER_ON_MASTER; START SLAVE;
On
db01
, create another empty database:CREATE DATABASE wx;
On
db03
, switch to filtering on the slave:STOP SLAVE; SET GLOBAL REPLICATE_EVENTS_MARKED_FOR_SKIP...