All pages
Powered by GitBook
1 of 31

MariaDB Replication

Learn about standard replication in MariaDB Server for high availability. This section covers configuring and managing master-replica setups to ensure data redundancy and improve read scalability.

Replication Overview

Get an overview of standard replication in MariaDB Server. This section introduces the core concepts, benefits, and components of setting up master-replica configurations for high availability and dat

Replication is a feature allowing the contents of one or more servers (called primaries) to be mirrored on one or more servers (called replicas).

You can exert control over which data to replicate. All databases, one or more databases, or tables within a database can each be selectively replicated.

The main mechanism used in replication is the binary log. If binary logging is enabled, all updates to the database (data manipulation and data definition) are written into the binary log as binlog events. Replicas read the binary log from each primary in order to access the data to replicate. A relay log is created on the replica, using the same format as the binary log, and this is used to perform the replication. Old relay log files are removed when no longer needed.

A replica server keeps track of the position in the primary's binlog of the last event applied on the replica. This allows the replica server to re-connect and resume from where it left off after replication has been temporarily stopped. It also allows a replica to disconnect, be cloned and then have the new replica resume replication from the same primary.

Primaries and replicas do not need to be in constant communication with each other. It's quite possible to take servers offline or disconnect from the network, and when they come back, replication will continue where it left off.

Replication Uses

Replication is used in a number of common scenarios. Uses include:

  • Scalability. By having one or more replicas, reads can be spread over multiple servers, reducing the load on the primary. The most common scenario for a high-read, low-write environment is to have one primary, where all the writes occur, replicating to multiple replicas, which handle most of the reads.

  • Data analysis. Analyzing data may have too much of an impact on a primary server, and this can similarly be handled on a replica, while the primary continues unaffected by the extra load.

  • Backup assistance. Backups can more easily be run if a server is not actively changing the data. A common scenario is to replicate the data to a replica, which is then disconnected from the primary with the data in a stable state. Backup is then performed from this server. See Replication as a Backup Solution.

  • Distribution of data. Instead of being connected to a remote primary, it's possible to replicate the data locally and work from this data instead.

Common Replication Setups

Standard Replication

standard_replication
  • Provides infinite read scale out.

  • Provides high-availability by upgrading replica to primary.

  • Setting up standard replication

Ring Replication

ring_replication
  • Provides read and write scaling.

  • Doesn’t handle conflicts.

  • If one primary fails, replication stops.

  • More about Multi-master ring replication

Ring Replication with slaves

  • Provides read and write scaling.

  • Doesn’t handle conflicts.

  • If one primary fails, replication stops.

  • More about Multi-master ring replication

Ring Replication with replication through slaves

  • Provides read and write scaling.

  • Doesn’t handle conflicts.

  • If one primary fails, replication stops.

  • More about Multi-master ring replication

Star Replication

star_replication
  • Provides read and write scaling.

  • Doesn’t handle conflicts.

  • Have to use replication filters to avoid duplication of data.

  • MariaDB Galera Cluster, which is a virtually synchronous multi-primary (multi-master) cluster for MariaDB, has a similar configuration and can handle conflicts.

Multi-Source Replication

multi_source_replication
  • Allows you to combine data from different sources.

  • Different domains executed independently in parallel on all replicas.

  • More about Multi-Source replication

Cross-Version Replication Compatibility

The following table describes replication compatibility between different MariaDB Server versions. In general, the replica should be of the same or a later version. The constraint also applies to minor/patch releases:

Primary→

MariaDB 10.3

MariaDB 10.4

MariaDB 10.5

MariaDB 10.6

MariaDB 10.11

MariaDB 11.4

Replica ↓

MariaDB 10.3

MariaDB 10.4

MariaDB 10.5

MariaDB 10.6

MariaDB 10.11

MariaDB 11.4

Primary→

MariaDB 10.3

MariaDB 10.4

MariaDB 10.5

MariaDB 10.6

MariaDB 10.11

MariaDB 11.4

Replica ↓

MariaDB 10.3

✅

⛔

⛔

⛔

⛔

⛔

MariaDB 10.4

✅

✅

⛔

⛔

⛔

⛔

MariaDB 10.5

✅

✅

✅

⛔

⛔

⛔

MariaDB 10.6

✅

✅

✅

✅

⛔

⛔

MariaDB 10.11

✅

✅

✅

✅

✅

⛔

MariaDB 11.4

✅

✅

✅

✅

✅

✅

  • ✅: This combination is supported.

  • ⛔: This combination is not supported.

Note: where it is not officially supported to replicate to a server with a lesser minor version, replication can still be safe for:

  • DMLs logged in ROW binlog_format, and

  • DMLS logged in STATEMENT format and DDLs where neither use features that do not yet exist on the replica

provided the configurations for each server allow for consistent behavior in the execution of the events (i.e. the execution of the event should not be reliant on newer configuration variables, character sets/collations, etc, that don't exist on the replica). Additionally note, if binlog_format=MIXED, it may be possible that the higher-versioned server (primary) may consider it safe to log a transaction using STATEMENT binlog format, while the older-versioned replica categorizes it as unsafe, which will result in an error while the replica tries to execute the transaction. See this page for more details on unsafe statements.

For replication compatibility details between MariaDB and MySQL, see MariaDB versus MySQL - Compatibility: Replication Compatibility.

See Also

  • Setting Up Replication

  • Replication Compatibility Between MariaDB and MySQL

  • MariaDB Galera Cluster and M/S replication (video)

This page is licensed: CC BY-SA / Gnu FDL

Replication Statements

Replication SQL statements can be found in the Reference section.

Setting Up Replication

Learn to set up standard replication in MariaDB Server. This section provides step-by-step instructions for configuring master and replica servers to ensure data redundancy and high availability.

The terms master and slave have historically been used in replication, and MariaDB has begun the process of adding primary and replica synonyms. The old terms will continue to be used to maintain backward compatibility - see MDEV-18777 to follow progress on this effort.

Getting replication working involves steps on both the master server/s and steps on the replica server/s.

Setting up a Replication Replica with MariaDB-Backup

If you would like to use mariadb-backup to set up a replication slave, then you might find the information at Setting up a Replication Replica with MariaDB-Backup helpful.

Versions

In general, when replicating across different versions of MariaDB, it is best that the master is an older version than the slave. MariaDB versions are usually backward compatible, while of course older versions cannot always be forward compatible. See also Replicating from MySQL Master to MariaDB Replica.

Configuring the Master

  • Enable binary logging if it's not already enabled. See Activating the Binary Log and Binary log formats for details.

  • Give the master a unique server_id. All slaves must also be given a server_id. This can be a number from 1 to 232-1, and must be unique for each server in the replicating group.

  • Specify a unique name for your replication logs with --log-basename. If this is not specified your host name will be used and there will be problems if the hostname ever changes.

  • Slaves will need permission to connect and start replicating from a server. Usually this is done by creating a dedicated slave user, and granting that user permission only to replicate (REPLICATION SLAVE permission).

Example Enabling Replication for MariaDB

Add the following into your my.cnf file and restart the database.

[mariadb]
log-bin
server_id=1
log-basename=master1
binlog-format=mixed

The server id is a unique number for each MariaDB/MySQL server in your network.binlog-format specifies how your statements are logged. This mainly affects the size of the binary log that is sent between the Master and the Replicas.

Then execute the following SQL with the mysql command line client:

CREATE USER 'replication_user'@'%' IDENTIFIED BY 'bigs3cret';
GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'%';

Example Enabling Replication for MySQL

If you want to enable replication from MySQL 5.7 or earlier to MariaDB, you can do it in almost the same way as between MariaDB servers. The main difference is that MySQL doesn't support log-basename.

[mysqld]
log-bin
server_id=1

For replication from MySQL 8.0 to MariaDB requires slight more configurations.

Settings to Check

There are a number of options that may impact or break replication. Check the following settings to avoid problems.

  • skip-networking. If skip-networking=1, the server will limit connections to localhost only, and prevent all remote slaves from connecting.

  • bind-address. Similarly, if the address the server listens for TCP/IP connections is 127.0.0.1 (localhost), remote slaves connections will fail.

Configuring the Replica

  • Give the slave a unique server_id. All servers, whether masters or replicas, are given a server_id. This can be a number from 1 to 232-1, and must be unique for each server in the replicating group. The server will need to be restarted in order for a change in this option to take effect.

Getting the Master's Binary Log Co-ordinates

Now you need prevent any changes to the data while you view the binary log position. You'll use this to tell the slave at exactly which point it should start replicating from.

  • On the master, flush and lock all tables by running FLUSH TABLES WITH READ LOCK. Keep this session running - exiting it will release the lock.

  • Get the current position in the binary log by running SHOW MASTER STATUS:

SHOW MASTER STATUS;
+--------------------+----------+--------------+------------------+
| File               | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| master1-bin.000096 |      568 |              |                  |
+--------------------+----------+--------------+------------------+
  • Record the File and Position details. If binary logging has just been enabled, these will be blank.

  • Now, with the lock still in place, copy the data from the master to the slave. See Backup, Restore and Import for details on how to do this.

  • Note for live databases: You just need to make a local copy of the data, you don't need to keep the master locked until the slave has imported the data.

  • Once the data has been copied, you can release the lock on the master by running UNLOCK TABLES.

UNLOCK TABLES;

Start the Slave

  • Once the data has been imported, you are ready to start replicating. Begin by running a CHANGE MASTER TO, making sure that MASTER_LOG_FILE matches the file and MASTER_LOG_POS the position returned by the earlier SHOW MASTER STATUS. For example:

CHANGE MASTER TO
  MASTER_HOST='master.domain.com',
  MASTER_USER='replication_user',
  MASTER_PASSWORD='bigs3cret',
  MASTER_PORT=3306,
  MASTER_LOG_FILE='master1-bin.000096',
  MASTER_LOG_POS=568,
  MASTER_CONNECT_RETRY=10;

If you are starting a slave against a fresh master that was configured for replication from the start, then you don't have to specify MASTER_LOG_FILE and MASTER_LOG_POS.

Use Global Transaction Id (GTID)

It is generally recommended to use (GTIDs), as it has a number of benefits. All that is needed is to add the MASTER_USE_GTID option to the CHANGE MASTER statement, for example:

CHANGE MASTER TO MASTER_USE_GTID = slave_pos

See Global Transaction ID for a full description. <>

  • Now start the slave with the START SLAVE command:

START SLAVE;
  • Check that the replication is working by executing the SHOW SLAVE STATUS command:

SHOW SLAVE STATUS \G
  • If replication is working correctly, both the values of Slave_IO_Running and Slave_SQL_Running should be Yes:

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

Replicating from MySQL Master to MariaDB Replica

  • Replicating from MySQL 5.5 to MariaDB should just work. When using a MariaDB as a replica, it may be necessary to set binlog_checksum to NONE.

  • Replicating from MySQL 5.6 without GTID to MariaDB 10+ should work.

  • Replication from MySQL 5.6 with GTID, binlog_rows_query_log_events and ignorable events works. In this case MariaDB will remove the MySQL GTIDs and other unneeded events and instead adds its own GTIDs.

  • Replication from MySQL 8 to MariaDB requires MariaDB 11.4.5 or newer.

See Also

  • Differences between Statement-based, mixed and row logging

  • Replication and Foreign Keys

  • Replication as a Backup Solution

  • Multi-source Replication

  • Global Transaction ID

  • Parallel Replication

  • Replication and Binary Log System Variables

  • Replication and Binary Log Status Variables

  • Semisynchronous Replication

  • Delayed Replication

  • Replication Compatibility Between MariaDB and MySQL

This page is licensed: CC BY-SA / Gnu FDL

Global Transaction ID

Learn about Global Transaction IDs (GTIDs) in MariaDB Server. This section explains how GTIDs simplify replication management, ensuring data consistency and enabling automatic failover and repair.

The terms master and slave have historically been used in replication, and MariaDB has begun the process of adding primary and replica synonyms. The old terms will continue to be used to maintain backward compatibility - see MDEV-18777 to follow progress on this effort.

Note that MariaDB and MySQL have different GTID implementations, and that these are not compatible with each other. MariaDB can be a replica for a MySQL primary but MySQL cannot be a replica for a MariaDB primary.

Overview

MariaDB replication in general works as follows (see Replication overview for more information):

On a master server, all updates to the database (DML and DDL) are written into the binary log as binlog events. A replica server connects to the primary and reads the binlog events, then applies the events locally to replicate the same changes as done on the primary. A server can be both a primary and a replica at the same time, and it is thus possible for binlog events to be replicated through multiple levels of servers.

A replica server keeps track of the position in the primary's binlog of the last event applied on the replica. This allows the replica server to re-connect and resume from where it left off after replication has been temporarily stopped. It also allows a replica to disconnect, be cloned and then have the new replica resume replication from the same primary.

Global transaction ID (GTID) introduces a new event attached to each event group in the binlog. An event group is a collection of events that are always applied as a unit. They are best thought of as a "transaction", though they also include non-transactional DML statements, as well as DDL. When an event group is replicated from primary server to replica server, the global transaction ID is preserved. The GTID is globally unique across an entire group of servers, making it easy to uniquely identify the same binlog events on different servers that replicate each other. GTIDs are generated for all event groups, independent of binlog_format (i.e. ROW, STATEMENT, andMIXED formats are all supported).

Benefits

Using global transaction ID provides two main benefits:

  1. Easy to change a replica server to connect to and replicate from a different primary server.

The replica remembers the global transaction ID of the last event group applied from the old primary. This makes it easy to know where to resume replication on the new primary, since the global transaction IDs are known throughout the entire replication hierarchy. This is not the case when using old-style replication; in this case the replica knows only the specific file name and offset of the old primary server of the last event applied. There is no simple way to guess from this the correct file name and offset on a new primary.

  1. The state of the replica is recorded in a crash-safe way.

The replica keeps track of its current position (the global transaction ID of the last transaction applied) in the mysql.gtid_slave_pos system table. If this table is using a transactional storage engine (such as InnoDB, which is the default), then updates to the state are done in the same transaction as the updates to the data. This makes the state crash-safe; if the replica server crashes, crash recovery on restart will make sure that the recorded replication position matches the changes that were actually replicated. This is not the case for old-style replication, where the state is recorded in a file relay-log.info, which is updated independently of the actual data changes and can easily get out of sync if the replica server crashes. (This works for DML to transactional tables; non-transactional tables and DDL in general are not crash-safe in MariaDB.)

Because of these two benefits, it is generally recommended to use global transaction ID for any replication setups based on MariaDB 10.0.2 or later. However, old-style replication continues to work as always, so there is no pressing need to change existing setups. Global transaction ID integrates smoothly with old-style replication, and the two can be used freely together in the same replication hierarchy. There is no special configuration needed of the server to start using global transaction ID. However, it must be explicitly set for a replica server with the appropriate CHANGE MASTER option; by default old-style replication is used by a replication replica, to maintain backwards compatibility.

Implementation

A global transaction ID, or GTID for short, consists of three numbers separated with dashes '-'. For example:

0-1-10

  • The first number 0 is the domain ID, which is specific for global transaction ID (more on this below). It is a 32-bit unsigned integer.

  • The second number is the server ID, the same as is also used in old-style replication. It is a 32-bit unsigned integer.

  • The third number is the sequence number. This is a 64-bit unsigned integer that is monotonically increasing for each new event group logged into the binlog.

The server ID is set to the server ID of the server where the event group is first logged into the binlog. The sequence number is increased on a server for every event group logged. Since server IDs must be unique for every server, this makes the (server_id, sequence_number) pair, and hence the whole GTID, globally unique.

Using a 64-bit number provides ample range that there should be no risk of it overflowing in the foreseeable future. However, one should not artificially (by setting gtid_seq_no) inject a GTID with a very high sequence number close to the limit of 64-bit.

The Domain ID

When events are replicated from a primary server to a replica server, the events are always logged into the replica's binlog in the same order that they were read from the primary's binlog. Thus, if there is only ever a single primary server receiving (non-replication) updates at a time, then the binlog order will be identical on every server in the replication hierarchy.

This consistent binlog order is used by the replica to keep track of its current position in the replication. Basically, the replica remembers the GTID of the last event group replicated from the primary. When reconnecting to a primary, whether the same one or a new one, it sends this GTID position to the primary, and the primary starts sending events from the first event after the corresponding event group.

However, if user updates are done independently on multiple servers at the same time, then in general it is not possible for binlog order to be identical across all servers. This can happen when using multi-source replication, with multi-primary ring topologies, or just if manual updates are done on a replica that is replicating from active primary. If the binlog order is different on the new primary from the order on the old primary, then it is not sufficient for the replica to keep track of a single GTID to completely record the current state.

The domain ID, the first component of the GTID, is used to handle this.

In general, the binlog is not a single ordered stream. Rather, it consists of a number of different streams, each one identified by its own domain ID. Within each stream, GTIDs always have the same order in every server binlog. However, different streams can be interleaved in different ways on different servers.

A replica server then keeps track of its replication position by recording the last GTID applied within each replication stream. When connecting to a new primary, the replica can start replication from a different point in the binlog for each domain ID.

For more details on using multi-primary setups and multiple domain IDs, see Use with multi-source replication and other multi-primary setups.

Simple replication setups only have a single primary being updated by the application at any one time. In such setups, there is only a single replication stream needed. Then domain ID can be ignored, and left as the default of 0 on all servers.

Using Global Transaction IDs

Global transaction ID is enabled automatically. Each event group logged to the binlog receives a GTID event, as can be seen with mariadb-binlog or SHOW BINLOG EVENTS.

The replica automatically keeps track of the GTID of the last applied event group, as can be seen from the gtid_slave_pos variable:

SELECT @@GLOBAL.gtid_slave_pos
0-1-1

When a replica connects to a primary, it can use either global transaction ID or old-style filename/offset to decide where in the primary binlogs to start replicating from. To use global transaction ID, use the CHANGE MASTER master_use_gtid option:

CHANGE MASTER TO master_use_gtid = { slave_pos | current_pos | no }

A replica is configured to use GTID by CHANGE MASTER TO master_use_gtid=slave_pos. When the replica connects to the primary, it will start replication at the position of the last GTID replicated to the replica, which can be seen in the variable gtid_slave_pos. Since GTIDs are the same across all replication servers, the replica can then be pointed to a different primary, and the correct position will be determined automatically.

But suppose that we set up two servers A and B and let A be the primary and B the replica. It runs for a while. Then at some point we take down A, and B becomes the new primary. Then later we want to add A back, this time as a replica.

Since A was never a replica before, it does not have any prior replicated GTIDs, and gtid_slave_pos will be empty. To allow A to be added as a replica automatically,master_use_gtid=current_pos can be used. This will connect using the value of the variable gtid_current_pos instead ofgtid_slave_pos, which also takes into account GTIDs written into the binlog when the server was a primary.

When using master_use_gtid=current_pos there is no need to consider whether a server was a primary or a replica prior to using CHANGE MASTER. But care must be taken not to inject extra transactions into the binlog on the replica server that are not intended to be replicated to other servers. If such an extra transaction is the most recent when the replica starts, it will be used as the starting point of replication. This will probably fail because that transaction is not present on the primary. To avoid local changes on a replica server to go into the binlog, set sql_log_bin to 0.

If it is undesirable that changes to the binlog on the replica affects the GTID replication position, then master_use_gtid=slave_pos should be used. Then the replica will always connect to the primary at the position of the last replicated GTID. This may avoid some surprises for users that expect behavior consistent with traditional replication, where the replication position is never changed by local changes done on a server.

When GTID strict mode is enabled (by setting@@GLOBAL.gtid_strict_mode to 1), it is normally best to usecurrent_pos. In strict mode, extra transactions on the Replica are disallowed as they would generate a local gtid. The local gtid would contain the current seqno the Replica is at incremented by 1, at the next transaction that will come from the Primary, the Replica would find such seqno already used by its own local transaction and it will stop replicating for safety until the situation is assessed.

If a replica is configured with the binlog disabled,current_pos and slave_pos are equivalent.

Even when a replica is configured to connect with the old-style binlog filename and offset (CHANGE MASTER TO master_log_file=..., master_log_pos=...), it will still keep track of the current GTID position in @@GLOBAL.gtid_slave_pos. This means that an existing replica previously configured and running can be changed to connect with GTID (to the same or a new master) simply with:

CHANGE MASTER TO master_use_gtid = slave_pos

The replica remembers that master_use_gtid=slave_pos|master_pos was specified and will use it also for subsequent connects, until it is explicitly changed by specifyingmaster_log_file/pos=... ormaster_use_gtid=no. The current value can be seen as the field Using_Gtid of SHOW SLAVE STATUS:

SHOW SLAVE STATUS\G
...
Using_Gtid: Slave_pos

The replica server internally uses the mysql.gtid_slave_pos table to store the GTID position (and so preserve the value of @@GLOBAL.gtid_slave_pos across server restarts). After upgrading a server to 10.0, it is necessary to run mysql_upgrade (as always) to get the table created.

In order to be crash-safe, this table must use a transactional storage engine such as InnoDB. When MariaDB is first installed (or upgraded to 10.0.2+) the table is created using the default storage engine - which itself defaults to InnoDB. If there is a need to change the storage engine for this table (to make it transactional on a system configured with MyISAM as the default storage engine, for example), use ALTER TABLE:

ALTER TABLE mysql.gtid_slave_pos ENGINE = InnoDB

The mysql.gtid_slave_pos table should not be modified in any other way. In particular, do not try to update the rows in the table to change the replica's idea of the current GTID position; instead use

SET GLOBAL gtid_slave_pos = '0-1-1'

Starting from MariaDB 10.3.1, the server variable gtid_pos_auto_engines can preferably be set to make the server handle this automatically. See the description of the mysql.gtid_slave_pos table for details.

Using current_pos vs. slave_pos

When setting the MASTER_USE_GTID replication parameter, you have the option of enabling Global Transaction IDs to use either the current_pos or slave_pos values.

Using the value current_pos causes the replica to set its position based on the gtid_current_pos system variable, which is a union of gtid_binlog_pos and gtid_slave_pos. Using the value slave_pos causes the replica to instead set its position based on the gtid_slave_pos system variable.

You may run into issues when you use the value current_pos if you write any local transactions on the replica. For instance, if you issue an INSERT statement or otherwise write to a table while the replica threads are stopped, then new local GTIDs may be generated in gtid_binlog_pos, which will affect the replica's value of gtid_current_pos. This may cause errors when the replica threads are restarted, since the local GTIDs will be absent from the primary.

You can correct this issue by setting the MASTER_USE_GTID replication parameter to slave_pos instead of current_pos. For example:

CHANGE MASTER TO MASTER_USE_GTID = slave_pos;
START SLAVE;

Using GTIDs with Parallel Replication

If parallel replication is in use, then events that were logged with GTIDs with different gtid_domain_id values can be applied in parallel in an out-of-order manner.

Using GTIDs with MariaDB Galera Cluster

Starting with MariaDB 10.1.4, MariaDB Galera Cluster has limited support for GTIDs. See Using MariaDB GTIDs with MariaDB Galera Cluster for more information.

Setting up a New Replica Server with Global Transaction ID

Setting up a new replica server with global transaction ID is not much different from setting up an old-style replica. The basic steps are:

  1. Setup the new server and load it with the initial data.

  2. Start the replica replicating from the appropriate point in the primary's binlog.

Setting up a New Replica with an Empty Server

The simplest way for testing purposes is probably to setup a new, empty replica server and replicate all of the primary's binlogs from the start (this is usually not feasible in a realistic production setup, as the initial binlog files will probably have been purged or take too long to apply).

The replica server is installed in the normal way. By default, the GTID position for a newly installed server is empty, which makes the replica replicate from the start of the primary's binlogs. But if the replica was used for other purposes before, the initial position can be explicitly set to empty first:

SET GLOBAL gtid_slave_pos = "";

Next, point the replica to the master with CHANGE MASTER. Specify master_host etc. as usual. But instead of specifying master_log_file and master_log_pos manually, use master_use_gtid=current_pos (orslave_pos to have GTID do it automatically:

CHANGE MASTER TO 
 master_host="127.0.0.1", 
 master_port=3310, 
 master_user="root", 
 master_use_gtid=current_pos;
START SLAVE;

Setting up a New Replica From a Backup

The normal way to set up a new replication replica is to take a backup from an existing server (either a primary or replica in the replication topology), and then restore that backup on the server acting as the new replica, and the configure it to start replicating from the appropriate position in the primary's binary log.

It is important that the position at which replication is started corresponds exactly to the state of the data at the point in time that the backup was taken. Otherwise, the replica can end up with different data than the primary because of missing or duplicated transactions. Of course, if there are no writes to the server being backed up during the backup process, then a simple SHOW MASTER STATUS will give the correct position.

See the description of the specific backup tool to determine how to get the binary log position that corresponds to the backup.

Once the current binary log position for the backup has been obtained, in the form of a binary log file name and position, the corresponding GTID position can be obtained from BINLOG_GTID_POS() on the server that was backed up:

SELECT BINLOG_GTID_POS("master-bin.000001", 600);

The new replica can then start replicating from the primary by setting the correct value forgtid_slave_pos, and then executing CHANGE MASTER with the relevant values for the primary, and then starting the replica threads by executing START SLAVE. For example:

SET GLOBAL gtid_slave_pos = "0-1-2";
CHANGE MASTER TO 
 master_host="127.0.0.1", 
 master_port=3310, 
 master_user="root", 
 master_use_gtid=slave_pos;
START SLAVE;

This method is particularly useful when setting up a new replica from a backup of the primary. Remember to ensure that the value of server_id configured on the new replica is different from that of any other server in the replication topology.

If the backup was taken of an existing replica server, then the new replica should already have the correct GTID position stored in the mysql.gtid_slave_pos table. This is assuming that this table was backed up and that it was backed up in a consistent manner with changes to other tables. In this case, there is no need to explicitly look up the GTID position on the old server and set it on the new replica - it will be already correctly loaded from the mysql.gtid_slave_pos table. This however does not work if the backup was taken from the primary - because then the current GTID position is contained in the binary log, not in the mysql.gtid_slave_pos table or any other table.

Setting up a New Replica with mariadb-backup

A new replica can easily be set up with mariadb-backup, which is a fork of Percona XtraBackup. See Setting up a Replica with mariadb-backup for more information.

Setting up a New Replica with mariadb-dump

A new replica can also be set up with mariadb-dump.

mariadb-dump automatically includes the GTID position as a comment in the backup file if either the --master-data or --dump-slave option is used. It also automatically includes the commands to set gtid_slave_pos and execute CHANGE MASTER in the backup file if the --gtid option is used with either the --master-data or --dump-slave option.

Switching An Existing Old-Style Replica To Use GTID.

If there is already an existing replica running using old-style binlog filename/offset position, then this can be changed to use GTID directly. This can be useful for upgrades for example, or where there are already tools to setup new replica using old-style binlog positions.

When a replica connects to a primary using old-style binlog positions, and the primary supports GTID (i.e. is MariaDB 10.0.2 or later), then the replica automatically downloads the GTID position at connect and updates it during replication. Thus, once a replica has connected to the GTID-aware primary at least once, it can be switched to using GTID without any other actions needed;

STOP SLAVE;
CHANGE MASTER TO 
 master_host="127.0.0.1", 
 master_port=3310, 
 master_user="root", 
 master_use_gtid=current_pos;
START SLAVE;

(A later version will probably add a way to setup the replica so that it will connect with old-style binlog file/offset the first time, and automatically switch to using GTID on subsequent connects.)

Changing a Replica to Replicate From a Different Primary

Once replication is running with GTID (master_use_gtid=current_pos|slave_pos), the replica can be pointed to a new primary simply by specifying in CHANGE MASTER the new master_host (and if required master_port, master_user, and master_password):

STOP SLAVE;
CHANGE MASTER TO 
 master_host='127.0.0.1', 
 master_port=3312;
START SLAVE;

The replica has a record of the GTID of the last applied transaction from the old primary, and since GTIDs are identical across all servers in a replication hierarchy, the replica will just continue from the appropriate point in the new primary's binlog.

It is important to understand how this change of primary work. The binlog is an ordered stream of events (or multiple streams, one per replication domain, (see Use with multi-source replication and other multi-primary setups). Events within the stream are always applied in the same order on every replica that replicates it. The MariaDB GTID relies on this ordering, so that it is sufficient to remember just a single point within the stream. Since event order is the same on every server, switching to the point of the same GTID in the binlog of another server will give the same result.

This translates into some responsibility for the user. The MariaDB GTID replication is fully asynchronous, and fully flexible in how it can be configured. This makes it possible to use it in ways where the assumption that binlog sequence is the same on all servers is violated. In such cases, when changing primary, GTID will still attempt to continue at the point of current GTID in the new binlog.

The most common way that binlog sequence gets different between servers is when the user/DBA does updates directly on a replica server (and these updates are written into the replica's binlog). This results in events in the replica's binlog that are not present on the primary or any other replicas. This can be avoided by setting the session variable sql_log_bin false while doing such updates, so they do not go into the binlog.

It is normally best to avoid any differences in binlogs between servers. That being said, MariaDB replication is designed for maximum flexibility, and there can be valid reasons for introducing such differences from time to time. It this case, it just needs to be understood that the GTID position is a single point in each binlog stream (one per replication domain), and how this affects the users particular setup.

Differences can also occur when two primary are active at the same time in a replication hierarchy. This happens when using a multi-primary ring. But it can also occur in a simple primary-replica setup, during switch to a new primary, if changes on the old primary is not allowed to fully replicate to all replica servers before switching primary. Normally, to switch primary, first writes to the old primary should be stopped, then one should wait for all changes to be replicated to the new primary, and only then should writes begin on the new primary. Deliberately using multiple active primary is also supported, this is described in the next section.

The GTID strict mode can be used to enforce identical binlogs across servers. When it is enabled, most actions that would cause differences are rejected with an error.

Use With Multi-Source Replication and Other Multi-Primary Setups

MariaDB global transaction ID supports having multiple primarys active at the same time. Typically this happens with either multi-source replication or multi-primary ring setups.

In such setups, each active primary must be configured with its own distinct replication domain ID, gtid_domain_id. The binlog will then in effect consists of multiple independent streams, one per active primary. Within one replication domain, binlog order is always the same on every server. But two different streams can be interleaved differently in different server binlogs.

The GTID position of a given replica is then not a single GTID. Rather, it becomes the GTID of the last event group applied for each value of domain ID, in effect the position reached in each binlog stream. When the replica connects to a primary, it can continue from one stream in a different binlog position than another stream. Since order within one stream is consistent across all servers, this is sufficient to always be able to continue replication at the correct point in any new primary server(s).

Domain IDs are assigned by the DBA, according to the need of the application. The default value of @@GLOBAL.gtid_domain_id is 0. This is appropriate for most replication setups, where only a single primary is active at a time. The MariaDB server will never by itself introduce new domain_id values into the binlog.

When using multi-source replication, where a single replica connects to multiple primaries at the same time, each such primary should be configured with its own distinct domain ID.

Similarly, in a multi-primary ring topology, where all primary in the ring are updated by the application concurrently (with some mechanism to avoid conflicts), a distinct domain ID should be configured for each server (In a multi-primary ring where the application is careful to only do updates on one primary at a time, a single domain ID is sufficient).

Normally, a replica server should not receive direct updates (as this creates binlog differences compared to the primary). Thus it does not matter what value of gtid_domain_id is set on a replica, though it may make sense to make it the same as the primary (if not using multi-primary) to make it easy to promote the replica as a new primary. Of course, if a replica is itself an active primary, as in a multi-primary ring topology, the domain ID should be set according to the server's role as active primary.

Note that domain ID and server ID are distinct concepts. It is possible to use a different domain ID on each server, but this is normally not desirable. It makes the current GTID position (@@global.gtid_slave_pos) more complicated to understand and work with, and loses the concept of a single ordered binlog stream across all servers. It is recommended only to configure as many domain IDs as there are primary servers actively being updated by the application at the same time.

It is not an error in itself to configure domain IDs incorrectly (for example, not configuring them at all). For example, this will be typical in an upgrade scenario where a multi-primary ring using 5.5 is upgraded to 10.0. The ring will continue to work as before even though everything is configured to use the default domain ID 0. It is even possible to use GTID for replication between the servers. However, care must be taken when switching a replica to a different primary. If the binlog order between the old and the new primary differs, then a single GTID position to start replication from in the new primary's binlog may not be sufficient.

Multiple Redundant Replication Paths

Using GTID with multi-source replication, it is possible to set up multiple redundant replication paths. For example:

M1 <-> M2
  M1 -> S1
  M1 -> S2
  M2 -> S1
  M2 -> S2

Here, M1 and M2 are setup in a master-master ring. S1 and S2 both replicate from each of M1 and M2. Each event generated on M1 will now arrive twice at S1, through the paths M1->S1 and M1->M2->S1. This way, if the network connection between M1 and S1 is broken, the replication can continue uninterrupted through the alternate path through M2. Note that this is an advanced setup, and good familiarity with MariaDB replication is recommended to successfully operate it.

The option --gtid-ignore-duplicates must be enabled to use multiple redundant replication paths. This is necessary to avoid each event being applied twice on the replica as it arrives through each path. The GTID of every event will be compared against the sequence number of the current GTID replica position (within each domain), and will be skipped if less than or equal. Thus it is required that sequence numbers are strictly increasing within each domain for --gtid-ignore-duplicates to function correctly, and setting --gtid-strict-mode=1 to help enforce this is recommended.

The --gtid-ignore-duplicates options also relaxes the requirement for connection to the master. In the above example, when S1 connects to M2, it may connect at a GTID position from M1 that has not yet been applied on M2.

When --gtid-ignore-duplicates is enabled, the connection will be allowed, and S1 will start receiving events from M2 once the GTID has been replicated from M1 to M2. This can also be used to use replication filters in parts of a replication topology, to allow a replica to connect to a GTID position which was filtered on a master. When --gtid-ignore-duplicates is enabled, the connecting replica will start receiving events from the master at the first GTID sequence number that is larger than the connect-position.

Deleting Unused Domains

FLUSH BINARY LOGS DELETE_DOMAIN_ID=(list-of-domains) can be used to discard obsolete GTID domains from the server's binary log state. In order for this to be successful, no event group from the listed GTID domains can be present in existing binary log files. If some still exist, then they must be purged prior to executing this command.

If the command completes successfully, then it also rotates the binary log.

The old domains will still appear in gtid_io_pos. To get rid of these, you can stop the replica and execute on the replica:

SET gtid_slave_pos="<position WITH domains removed>"

Additional Syntax For Global Transaction ID

CHANGE MASTER

CHANGE MASTER has an option, master_use_gtid=[current_pos|slave_pos|no]. When enabled (set tocurrent_pos or slave_pos), the replica will connect to the master using the GTID position. When disabled (set to "no"), the old-style binlog filename/offset position is used to decide where to start replicating when connecting. Unlike in the old-style, when GTID is enabled, the values of the MASTER_LOG_FILE and MASTER_LOG_POS options are not updated per received event in master_info_file file.

The value of master_use_gtid is saved across server restarts (in master.info). The current value can be seen as the field Using_Gtid in the output of SHOW SLAVE STATUS.

For a detailed look at the difference between the current_pos and slave_pos options, see Using global transaction IDs

START SLAVE UNTIL master_gtid_pos=xxx

When starting replication with START SLAVE, it is possible to request the replica to run only until a specific GTID position is reached. Once that position is reached, the replica will stop.

The syntax for this is:

START SLAVE UNTIL master_gtid_pos = <GTID position>

The replica will start replication from the current GTID position, run up to and including the event with the GTID specified, and then stop. Note that this stops both the IO thread and the SQL thread (unlike START SLAVE UNTIL MASTER_LOG_FILE/MASTER_LOG_POS, which stops only the SQL thread).

If multiple GTIDs are specified, then they must be with distinct replication domain ID, for example:

START SLAVE UNTIL master_gtid_pos = "1-11-100,2-21-50"

With multiple domains in the UNTIL condition, each domain runs only up to and including the specified position, so it is possible for different domains to stop at different places in the binlog (each domain will resume from the stopped position when the replica is started the next time).

Not specifying a replication domain at all in the UNTIL condition means that the domain is stopped immediately, nothing is replicated from that domain. In particular, specifying the empty string will stop the replica immediately.

When using START SLAVE UNTIL master_gtid_pos = XXX, if the UNTIL position is present in the primary's binlog then it is permissible for the start position to be missing on the primary. In this case, replication for the associated domains stop immediately.

Both replica threads must be already stopped when using UNTIL master_gtid_pos, otherwise an error occurs. It is also an error if the replica is not configured to use GTID (CHANGE MASTER TO master_use_gtid=current_pos|slave_pos). And both threads must be started at the same time, the IO_THREAD or SQL_THREAD options can not be used to start only one of them.

START SLAVE UNTIL master_gtid_pos=XXX is particularly useful for promoting a new primary among a set of replicas when the old master goes away and replicas may have reached different positions in the old primary's binlog. The new primary needs to be ahead of all the other replicas to avoid losing events. This can be achieved by picking one server, say S1, and replicating any missing events from each other server S2, S3, ..., Sn:

CHANGE MASTER TO master_host="S2";
    START SLAVE UNTIL master_gtid_pos = "<S2 GTID position>";
    ...
    CHANGE MASTER TO master_host="Sn";
    START SLAVE UNTIL master_gtid_pos = "<Sn GTID position>";

Once this is completed, S1 will have all events present on any of the servers. It can now be selected as the new primary, and all the other servers set to replicate from it.

MariaDB starting with 11.3.0

SQL_BEFORE_GTIDS|SQL_AFTER_GTIDS

MariaDB 11.3 extended the START SLAVE UNTIL command with the options SQL_BEFORE_GTIDS and SQL_AFTER_GTIDS to allow control of whether the replica stops before or after a provided GTID state. Its syntax is:

START SLAVE UNTIL (SQL_BEFORE_GTIDS|SQL_AFTER_GTIDS)="<gtid_list>"

When providing SQL_BEFORE_GTIDS=”<gtid_list>”, the replica will execute all transactions up to the first GTID found in the provided list, and stop immediately. In contrast to the default behavior of UNTIL, this will execute transactions from all domains on the primary until the replica stops due to seeing a GTID on the list. START SLAVE UNTIL SQL_AFTER_GTIDS=”<gtid_list>” is an alias to the default behavior of START SLAVE UNTIL master_gtid_pos=”<gtid_list>”. That is, the replica will only execute transactions originating from domain ids provided in the list, and will stop once all transactions provided in the UNTIL list have all been executed.

Example

If a primary server has a binary log consisting of the following GTIDs:

  • 0-1-1

  • 1-1-1

  • 0-1-2

  • 1-1-2

  • 0-1-3

  • 1-1-3

If a fresh replica (i.e. one with an empty GTID position, @@gtid_slave_pos='') is started with SQL_BEFORE_GTIDS, i.e. START SLAVE UNTIL SQL_BEFORE_GTIDS=”1-1-2”, the resulting gtid_slave_pos of the replica will be “0-1-2,1-1-1”. This is because the replica will execute all events until it sees the transaction with GTID 1-1-2 and immediately stop without executing it. However, if a replica is started with SQL_AFTER_GTIDS, i.e. START SLAVE UNTIL SQL_AFTER_GTIDS=”1-1-2” then the resulting gtid_slave_pos of the replica will be “1-1-2”. This is because it will only execute events from domain 1 until it has executed the provided GTID.

BINLOG_GTID_POS().

The BINLOG_GTID_POS() function takes as input an old-style binary log position in the form of a file name and a file offset. It looks up the position in the current binlog, and returns a string representation of the corresponding GTID position. If the position is not found in the current binlog, NULL is returned.

MASTER_GTID_WAIT

The MASTER_GTID_WAIT function is useful in replication for controlling primary/replica synchronization, and blocks until the replica has read and applied all updates up to the specified position in the primary log. See MASTER_GTID_WAIT for details.

Binlog Indexing

MariaDB starting with 11.4

Prior to MariaDB 11.4, when a replica connects, MariaDB needs to scan binlog files from the beginning in order to find the place to start replicating. If replica reconnects are frequent, this can be slow.MariaDB 11.4 introduces indexing on the binlog files, allowing GTIDs to be quickly found. This also detects if old-style replication tries to connect at an incorrect file offset (eg. in the middle of an event), avoiding sending potentially corrupted events. The feature is enabled by default. The size of the binlog index file (.idx) is generally less than 1% the size of the binlog, so should not have any negative impacts and should not normally need tuning. However, the feature can be disabled or managed with the following system variables:

  • binlog_gtid_index - enable/disable the feature

  • binlog_gtid_index_page_size - adjust the size of the pages

  • binlog_gtid_index_span_min - adjust the sparseness of the index

There are two status variables that can be used to monitor the effectiveness of the index:

  • binlog_gtid_index_hit - incremented for each successful lookup in a GTID index.

  • binlog_gtid_index_miss - incremented when a GTID index lookup is not possible, which indicates that the index file is missing (eg. binlog written by old server version without GTID index support), or corrupt.

System Variables

binlog_gtid_index

Enables/disables binlog indexing.

binlog_gtid_index_page_size

Adjusts the size of the pages

binlog_gtid_index_span_min

Adjusts the sparseness of the index

gtid_slave_pos

This system variable contains the GTID of the last transaction applied to the database by the server's replica threads for each replication domain. This system variable's value is automatically updated whenever a replica thread applies an event group. This system variable's value can also be manually changed by users, so that the user can change the GTID position of the replica threads.

When using multi-source replication, the same GTID position is shared by all replica connections. In this case, different primaries should use different replication domains by configuring different gtid_domain_id values. If one primary was using a gtid_domain_id value of 1, and if another primary was using a gtid_domain_id value of 2, then any replicas replicating from both primaries would have GTIDs with both gtid_domain_id values in gtid_slave_pos.

This system variable's value can be manually changed by executing SET GLOBAL, but all replica threads to be stopped with STOP SLAVE first. For example:

STOP ALL SLAVES;
SET GLOBAL gtid_slave_pos = "1-10-100,2-20-500";
START ALL SLAVES;

This system variable's value can be reset by manually changing its value to the empty string. For example:

SET GLOBAL gtid_slave_pos = '';

The GTID position defined by gtid_slave_pos can be used as a replica's starting replication position by setting MASTER_USE_GTID=slave_pos when the replica is configured with the CHANGE MASTER TO statement. As an alternative, the gtid_current_pos system variable can also be used as a replica's starting replication position.

If a user sets the value of the gtid_slave_pos system variable, and gtid_binlog_pos contains later GTIDs for certain replication domains, then gtid_current_pos will contain the GTIDs from gtid_binlog_pos for those replication domains. To protect users in this scenario, if a user sets the gtid_slave_pos system variable to a GTID position that is behind the GTID position in gtid_binlog_pos, then the server will give the user a warning.

This can help protect the user when the replica is configured to use gtid_current_pos as its replication position. This can also help protect the user when a server has been rolled back to restart replication from an earlier point in time, but the user has forgotten to reset gtid_binlog_pos with RESET MASTER.

The mysql.gtid_slave_pos system table is used to store the contents of global.gtid_slave_pos and preserve it over restarts.

  • Commandline: None

  • Scope: Global

  • Dynamic: Yes

  • Data Type: string

  • Default: Null

gtid_binlog_pos

This variable is the GTID of the last event group written to the binary log, for each replication domain.

Note that when the binlog is empty (such as on a fresh install with --skip-test-db, or after RESET MASTER), there are no event groups written in any replication domain, so in this case the value of gtid_binlog_pos will be the empty string.

The value is read-only, but it is updated whenever a DML or DDL statement is written to the binary log. The value can be reset by executing RESET MASTER, which will also delete all binary logs. However, note that RESET MASTER does not also reset gtid_slave_pos. Since gtid_current_pos is the union of gtid_slave_pos and gtid_binlog_pos, that means that new GTIDs added to gtid_binlog_pos can lag behind those in gtid_current_pos if gtid_slave_pos contains GTIDs in the same domain with higher sequence numbers. If you want to reset gtid_current_pos for a specific GTID domain in cases like this, then you will also have to change gtid_slave_pos in addition to executing RESET MASTER. See gtid_slave_pos for notes on how to change its value.

  • Commandline: None

  • Scope: Global

  • Dynamic: Read-only

  • Data Type: string

  • Default: Null

gtid_binlog_state

The variable gtid_binlog_state holds the internal state of the binlog. The state consists of the last GTID ever logged to the binary log for every combination of domain_id and server_id. This information is used by the primary to determine whether a given GTID has been logged to the binlog in the past, even if it has later been deleted due to binlog purge. For each domain_id, the last entry in @@gtid_binlog_state is the last GTID logged into binlog, ie. this is the value that appears in @@gtid_binlog_pos.

Normally this internal state is not needed by users, as @@gtid_binlog_pos is more useful in most cases. The main usage of @@gtid_binlog_state is to restore the state of the binlog after RESET MASTER (or equivalently if the binlog files are lost). If the value of @@gtid_binlog_state is saved before RESET MASTER and restored afterwards, the primary will retain information about past history, same as if PURGE BINARY LOGS had been used (of course the actual events in the binary logs are still deleted).

Note that to set the value of @@gtid_binlog_state, the binary log must be empty, that is it must not contain any GTID events and the previous value of @@gtid_binlog_state must be the empty string. If not, then RESET MASTER must be used first to erase the binary log first.

The value of @@gtid_binlog_state is preserved by the server across restarts by writing a file MASTER-BIN.state, where MASTER-BIN is the base name of the binlog set with the --log-bin option. This file is written at server shutdown, and re-read at next server start. (In case of a server crash, the data in the MASTER-BIN.state is not correct, and the server instead recovers the correct value during binlog crash recovery by scanning the binlog files and recording each GTID found).

For completeness, note that setting @@gtid_binlog_state internally executes a RESET MASTER. This is normally not noticeable as it can only be changed when the binlog is empty of GTID events. However, if executed e.g. immediately after upgrading to MariaDB 10, it is possible that the binlog is non-empty but without any GTID events, in which case all such events will be deleted, just as if RESET MASTER had been run.

  • Commandline: None

  • Scope: Global

  • Dynamic: Yes

  • Data Type: string

  • Default: Null

gtid_current_pos

This system variable contains the GTID of the last transaction applied to the database for each replication domain.

The value of this system variable is constructed from the values of the gtid_binlog_pos and gtid_slave_pos system variables. It gets GTIDs of transactions executed locally from the value of the gtid_binlog_pos system variable. It gets GTIDs of replicated transactions from the value of the gtid_slave_pos system variable.

For each replication domain, if the server_id of the corresponding GTID ingtid_binlog_pos is equal to the servers own server_id,and the sequence number is higher than the corresponding GTID ingtid_slave_pos, then the GTID fromgtid_binlog_pos will be used. Otherwise the GTID fromgtid_slave_pos will be used for that domain.

GTIDs from gtid_binlog_pos in which the server_id of the GTID is not equal to the server's own server_id are effectively ignored. If gtid_binlog_pos contains a GTID for a given replication domain, but the server_id of the GTID is not equal to the server's own server_id, and gtid_slave_pos does not contain a GTID for that given replication domain, then gtid_current_pos will not contain any GTID for that replication domain.

Thus, gtid_current_pos contains the most recent GTID executed on the server, whether this was done as a primary or as a replica.

The GTID position defined by gtid_current_pos can be used as a replica's starting replication position by setting MASTER_USE_GTID=current_pos when the replica is configured with the CHANGE MASTER TO statement. As an alternative, the gtid_slave_pos system variable can also be used as a replica's starting replication position.

The value of gtid_current_pos is read-only, but it is updated whenever a transaction is written to the binary log and/or replicated by a replica thread, and that transaction's GTID is considered newer than the current GTID for that domain. See above for the rules on how to determine if a GTID would be considered newer.

If you need to reset the value, see the notes on resetting gtid_slave_pos and gtid_binlog_pos, since gtid_current_pos is formed from the values of those variables.

  • Commandline: None

  • Scope: Global

  • Dynamic: Read-only

  • Data Type: string

  • Default: Null

gtid_strict_mode

The GTID strict mode is an optional setting that can be used to help the DBA enforce a strict discipline about keeping binlogs identical across multiple servers replicating using global transaction ID.

When GTID strict mode is enabled, some additional errors are enabled for situations that could otherwise cause differences between binlogs on different servers in a replication hierarchy:

  1. If a replica server tries to replicate a GTID with a sequence number lower than what is already in the binlog for that replication domain, the SQL thread stops with an error (this indicates an extra transaction in the replica binlog not present on the primary).

  2. Similarly, an attempt to manually binlog a GTID with a lower sequence number (by setting @@SESSION.gtid_seq_no) is rejected with an error.

  3. If the replica tries to connect starting at a GTID that is missing in the primary's binlog, this is an error in GTID strict mode even if a GTID exists with a higher sequence number (this indicates a GTID on the replica missing on the primary). Note that this error is controlled by the setting of GTID strict mode on the connecting replica server.

GTID mode is off by default; this is needed to preserve backwards compatibility with existing replication setups (older versions of the server did not enforce any strict mode for binlog order). Global transaction ID is designed to work correctly even when strict mode is not enabled. However, with strict mode enforced, the semantics is simpler and thus easier to understand, because binlog order is always identical across servers and sequence numbers are always strictly increasing within each replication domain. This can also make automated scripting of large replication setups easier to implement correctly.

When GTID strict mode is enabled, the replica will stop with an error when a problem is encountered. This allows the DBA to become aware of the problem and take corrective actions to avoid similar issues in the future. One way to recover from such an error is to temporarily disable GTID strict mode on the offending replica, to be able to replicate past the problem point (perhaps usingSTART SLAVE UNTIL master_gtid_pos=XXX).

  • Commandline: --gtid-strict-mode[={0|1}]

  • Scope: Global

  • Dynamic: Yes

  • Data Type: boolean

  • Default: Off

gtid_domain_id

  • Description: This variable is used to decide which replication domain new GTIDs are logged in for a primary server. See Use with multi-source replication and other multi-primary setups for details. This variable can also be set on the session level by a user with the SUPER privilege. This is used by mariadb-binlog to preserve the domain ID of GTID events.

  • Commandline: --gtid-domain-id=#

  • Scope: Global, Session

  • Dynamic: Yes

  • Data Type: numeric (32-bit unsigned integer)

  • Default Value: 0

  • Range: 0 to 4294967295

last_gtid

  • Description: Holds the GTID that was assigned to the last transaction, or statement that was logged to the binary log. If the binary log is disabled, or if no transaction or statement was executed in the session yet, then the value is an empty string.

  • Scope: Session

  • Dynamic: Read-only

  • Data Type: string

server_id

  • Description: Server_id can be set on the session level to change which server_id value is logged in binlog events (both GTID and other events). This is used by mariadb-binlog to preserve the server ID of GTID events.

  • Scope: Global, Session

  • Dynamic: Yes

  • Data Type: numeric (32-bit unsigned integer)

gtid_seq_no

  • Description: gtid_seq_no can be set on the session level to change which sequence number is logged in the following GTID event. The variable, along with @@gtid_domain_id and @@server_id, is typically used by mariadb-binlog to set up the gtid value of the transaction being decoded into the output.

  • Commandline: None

  • Scope: Session

  • Dynamic: Yes

  • Data Type: numeric (64-bit unsigned integer)

  • Default: Null

gtid_ignore_duplicates

  • Description: When set, different primary connections in multi-source replication are allowed to receive and process event groups with the same GTID (when using GTID mode). Only one will be applied, any others will be ignored. Within a given replication domain, just the sequence number will be used to decide whether a given GTID has been already applied; this means it is the responsibility of the user to ensure that GTID sequence numbers are strictly increasing. With gtid_ignore_duplicates=OFF, a duplicate event based on domain id and sequence number, will be executed. When --gtid-ignore-duplicate is set, a replica is allowed to connect at a GTID position that does not exist on the primary. The replica will start receiving events once a GTID with a higher sequence number is available on the primary (within that domain). This can be used to allow a replica to connect at a GTID position that was filtered on the primary, eg. using --replicate-ignore-table. See also Multiple Redundant Replication Paths

  • Commandline: --gtid-ignore-duplicates=#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: boolean

  • Default: OFF

gtid_pos_auto_engines

This variable is used to enable multiple versions of the mysql.gtid_slave_pos table, one for each transactional storage engine in use. This can improve replication performance if a server is using multiple different storage engines in different transactions.

The value is a list of engine names, separated by commas (','). Replication of transactions using these engines will automatically create new versions of the mysql.gtid_slave_pos table in the same engine and use that for future transactions (table creation takes place in a background thread). This avoids introducing a cross-engine transaction to update the GTID position. Only transactional storage engines are supported for gtid_pos_auto_engines (this currently means InnoDB, TokuDB, or MyRocks).

The variable can be changed dynamically, but replica SQL threads should be stopped when changing it, and it will take effect when the replicas are running again.

When setting the variable on the command line or in a configuration file, it is possible to specify engines that are not enabled in the server. The server will then still start if, for example, that engine is no longer used. Attempting to set a non-enabled engine dynamically in a running server (with SET GLOBAL gtid_pos_auto_engines) will still result in an error.

Removing a storage engine from the variable will have no effect once the new tables have been created - as long as these tables are detected, they will be used.

  • Commandline: --gtid-pos-auto-engines=value

  • Scope: Global

  • Dynamic: Yes

  • Data Type: string (comma-separated list of engine names)

  • Default: empty

gtid_cleanup_batch_size

  • Description: Normally does not need tuning. How many old rows must accumulate in the mysql.gtid_slave_pos table before a background job will be run to delete them. Can be increased to reduce number of commits if using many different engines with gtid_pos_auto_engines, or to reduce CPU overhead if using a huge number of different gtid_domain_ids. Can be decreased to reduce number of old rows in the table.

  • Commandline: --gtid-cleanup-batch-size=#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default: 64

  • Range: 0 to 2147483647

  • Introduced: MariaDB 10.4.1

See Also

  • FLUSH binary logs

This page is licensed: CC BY-SA / Gnu FDL

Read-Only Replicas

Utilize read-only replicas in MariaDB Server for enhanced performance. This section explains how to configure replicas to offload read traffic from the master, improving scalability.

A common replication setup is to have the replicasread-only to ensure that no one accidentally updates them. If the replica has binary logging enabled and gtid_strict_mode is used, then any update that causes changes to the binary log will stop replication.

When the variable read_only is set to 1, no updates are permitted except from users with the SUPER privilege (<= MariaDB 10.5.1) or READ ONLY ADMIN privilege (>= MariaDB 10.5.2) or replica servers updating from a primary. Inserting rows to log tables, updates to temporary tables and OPTIMIZE TABLE or ANALYZE TABLE statements on temporary tables are excluded from this limitation.

If read_only is set to 1, then the SET PASSWORD statement is limited only to users with the SUPER privilege (<= MariaDB 10.5.1) or READ ONLY ADMIN privilege (>= MariaDB 10.5.2).

Attempting to set the read_only variable to 1 will fail if the current session has table locks or transactions pending.

The statement will wait for other sessions that hold table locks. While the attempt to set read_only is waiting, other requests for table locks or transactions will also wait until read_only has been set.

From MariaDB 10.3.19, some issues related to read only replicas are fixed:

  • CREATE, DROP, ALTER, INSERT and DELETE of temporary tables are not logged to binary log, even in statement or mixed mode. With earlier MariaDB versions, one can avoid the problem with temporary tables by using binlog_format=ROW in which cases temporary tables are never logged.

  • Changes to temporary tables created during read_only will not be logged even after read_only mode is disabled (for example if the replica is promoted to a primary).

  • The admin statements ANALYZE, CHECK, OPTIMIZE and REPAIR will not be logged to the binary log under read-only.

Older MariaDB Versions

If you are using an older MariaDB version with read-only replicas and binary logging enabled on the replica, and you need to do some changes but don't want to have them logged to the binary log, the easiest way to avoid the logging is to disable binary logging while running as root during maintenance:

SET sql_log_bin=0;
ALTER TABLE test ENGINE=rocksdb;

The above changes the test table on the replica to rocksdb without registering the change in the binary log.

This page is licensed: CC BY-SA / Gnu FDL

Multi-Source Replication

Implement multi-source replication in MariaDB Server. This section explains how a replica can receive events from multiple masters, enhancing data aggregation and complex high-availability setups.

The terms master and slave have historically been used in replication, and MariaDB has begun the process of adding primary and replica synonyms. The old terms will continue to be used to maintain backward compatibility - see MDEV-18777 to follow progress on this effort.

Multi-source replication means that one server has many primaries from which it replicates.

multi_source_replication_small

New Syntax

You specify which primary connection you want to work with by either specifying the connection name in the command or settingdefault_master_connection to the connection you want to work with.

The connection name may include any characters and should be less than 64 characters. Connection names are compared without regard to case (case insensitive). You should preferably keep the connection name short as it will be used as a suffix for relay logs and primary info index files.

The new syntax introduced to handle many connections:

  • CHANGE MASTER ['connection_name'] TO .... This creates or modifies a connection to a primary.

  • FLUSH RELAY LOGS ['connection_name']

  • MASTER_POS_WAIT(....,['connection_name'])

  • [RESET SLAVE ['connection_name'] [ALL](../../../reference/sql-statements-and-structure/sql-statements/administrative-sql-statements/replication-statements/reset-replica.md)]. This is used to reset a replica's replication position or to remove a replica permanently.

  • SHOW RELAYLOG ['connection_name'] EVENTS

  • SHOW SLAVE ['connection_name'] STATUS

  • SHOW ALL SLAVES STATUS

  • [START SLAVE ['connection_name'](../../../reference/sql-statements-and-structure/sql-statements/administrative-sql-statements/replication-statements/start-replica.md)...]]

  • START ALL SLAVES ...

  • STOP SLAVE ['connection_name'] ...

  • STOP ALL SLAVES ...

The original old-style connection is an empty string ''. You don't have to use this connection if you don't want to.

You create new primary connections with CHANGE MASTER. You delete the connection permanently with RESET SLAVE 'connection_name' ALL.

Replication Variables for Multi-Source

The new replication variable default_master_connection specifies which connection will be used for commands and variables if you don't specify a connection. By default this is '' (the default connection name).

The following replication variables are local for the connection. (In other words, they show the value for the@@default_master_connection connection). We are working on making all the important ones local for the connection.

Type
Name
Description

Variable

max_relay_log_size

Max size of relay log. Is set at startup to max_binlog_size if 0

Variable

replicate_do_db

Tell the replica to restrict replication to updates of tables whose names appear in the comma-separated list. For statement-based replication, only the default database (that is, the one selected by USE) is considered, not any explicitly mentioned tables in the query. For row-based replication, the actual names of table(s) being updated are checked.

Variable

replicate_do_table

Tells the replica to restrict replication to tables in the comma-separated list

Variable

replicate_ignore_db

Tell the replica to restrict replication to updates of tables whose names do not appear in the comma-separated list. For statement-based replication, only the default database (that is, the one selected by USE) is considered, not any explicitly mentioned tables in the query. For row-based replication, the actual names of table(s) being updated are checked.

Variable

replicate_ignore_table

Tells the replica thread to not replicate any statement that updates the specified table, even if any other tables might be updated by the same statement.

Variable

replicate_rewrite_db

From MariaDB 10.11. Allows one to configure a replica to rewrite database names. It uses the format primary_database->replica_database. If a replica encounters a binary log event in which the default database (i.e. the one selected by the USE statement) is primary_database, then the replica will apply the event in replica_database instead.

Variable

replicate_wild_do_table

Tells the replica thread to restrict replication to statements where any of the updated tables match the specified database and table name patterns.

Variable

replicate_wild_ignore_table

Tells the replica thread to not replicate to the tables that match the given wildcard pattern.

Status

Slave_heartbeat_period

How often to request a heartbeat packet from the primary (in seconds).

Status

Slave_received_heartbeats

How many heartbeats we have got from the primary.

Status

Slave_running

Shows if the replica is running. YES means that the sql thread and the IO thread are active. No means either one is not running. '' means that @@default_master_connection doesn't exist.

Variable

Sql_slave_skip_counter

How many entries in the replication log that should be skipped (mainly used in case of errors in the log).

You can access all of the above variables with eitherSESSION or GLOBAL.

Note that in contrast to MySQL, all variables always show the correct active value!

Example:

set @@default_master_connection='';
show status like 'Slave_running';
set @@default_master_connection='other_connection';
show status like 'Slave_running';

If @@default_master_connection contains a non existing name, you will get a warning.

All other primary-related variables are global and affect either only the '' connections or all connections. For example,Slave_retried_transactions now shows the total number of retried transactions over all replicas.

If you need to set gtid_slave_pos you need to set this for all primaries at the same time.

New status variables:

Name
Description

Com_start_all_slaves

Number of executed START ALL SLAVES commands.

Com_start_slave

Number of executed START SLAVE commands. This replaces Com_slave_start.

Com_stop_slave

Number of executed STOP SLAVE commands. This replaces Com_slave_stop.

Com_stop_all_slaves

Number of executed STOP ALL SLAVES commands.

SHOW ALL SLAVES STATUS has the following new columns:

Name
Description

Connection_name

Name of the primary connection. This is the first variable.

Slave_SQL_State

State of SQL thread.

Retried_transactions

Number of retried transactions for this connection.

Max_relay_log_size

Max relay log size for this connection.

Executed_log_entries

How many log entries the replica has executed.

Slave_received_heartbeats

How many heartbeats we have got from the primary.

Slave_heartbeat_period

How often to request a heartbeat packet from the primary (in seconds).

New Files

The basic principle of the new files used by multi source replication is that they have the same name as the original relay log files suffixed withconnection_name before the extension. The main exception is the file that holds all connection is named as the normalmaster-info-file with a multi- prefix.

When you are using multi source, the following new files are created:

Name
Description

multi-master-info-file

The master-info-file (normally master.info) with a multi- prefix. This contains all primary connections in use.

master-info-file-connection_name.extension

Contains the current primary position for what's applied to in the replica. Extension is normally .info

relay-log-connection_name.xxxxx

The relay-log name with a connection_name suffix. The xxxxx is the relay log number. This contains the replication data read from the primary.

relay-log-index-connection_name.extension

Contains the name of the active relay-log-connection_name.xxxxx files. Extension is normally .index

relay-log-info-file-connection_name.extension

Contains the current primary position for the relay log. Extension is normally .info

When creating the file, the connection name is converted to lower case and all special characters in the connection name are converted, the same way as MySQL table names are converted. This is done to make the file name portable across different systems.

Hint:

Instead of specifying names for mysqld with --relay-log, --relay-log-index, --general-log-file, --slow-query-log-file,--log-bin and --log-bin-index, you can just specify --log-basename and all the other variables are set with this as a prefix.

Other Things

  • All error messages from a replica with a connection name, that are written to the error log, are prefixed with Master 'connection_name':. This makes it easy to see from where an error originated.

  • Errors ER_MASTER_INFO and WARN_NO_MASTER_INFO now includes connection_name.

  • There is no conflict resolution. The assumption is that there are no conflicts in data between the different primaries.

  • All executed commands are stored in the normal binary log (nothing new here).

  • If the server variable log_warnings > 1 then you will get some information in the log about how the multi-master-info file is updated (mainly for debugging).

  • The output of SHOW ALL SLAVES STATUS has one more column than SHOW SLAVE STATUS, since it includes the connection_name column.

  • RESET SLAVE now deletes all relay-log files.

replicate-... Variables

  • One can set the values for the replicate-... variables from the command line or in my.cnf for a given connection by prefixing the variable with the connection name.

  • If one doesn't use any connection name prefix for a replicate.. variable, then the value will be used as the default value for all connections that don't have a value set for this variable.

Example:

mysqld --main_connection.replicate_do_db=main_database --replicate_do_db=other_database

The have sets the replicate_do_db variable to main_database for the connection named main_connection. All other connections will use the value other_database.

One can also use this syntax to set replicate-rewrite-db for a given connection.

Typical Use Cases

  • You are partitioning your data over many primaries and would like to get it all together on one machine to do analytical queries on all data.

  • You have many databases spread over many MariaDB/MySQL servers and would like to have all of them on one machine as an extra backup.

  • In a Galera cluster the default replication filter rules like replicate-do-db do not apply to replication connections, but also to Galera write set applier threads. By using a named multi-primary replication connection instead, even when replicating from just one primary into the cluster, the primary-replica replication rules can be kept separate from the Galera intra-node replication traffic.

Limitations

  • Each active connection will create 2 threads (as is normal for MariaDB replication).

  • You should ensure that all primaries have different server-id's. If you don't do this, you will get into trouble if you try to replicate from the multi-source replica back to your primaries.

  • One can change max_relay_log_size for any active connection, but new connections will always use the server startup value for max_relay_log_size, which can't be changed at runtime.

  • Option innodb-recovery-update-relay-log (xtradb feature to store and restore relay log position for replicas) only works for the default connection ''. As this option is not really safe and can easily cause loss of data if you use storage engines other than InnoDB, we don't recommend this option be used.

  • slave_net_timeout affects all connections. We don't check anymore if it's less than Slave_heartbeat_period, as this doesn't make sense in a multi-source setup.

Incompatibilities with MariaDB/MySQL 5.5

  • max_relay_log_size is now (almost) a normal variable and not automatically changed if max_binlog_size is changed. To keep things compatible with old config files, we set it to max_binlog_size at startup if its value is 0.

  • You can now access replication variables that depend on the active connection with either GLOBAL or SESSION.

  • We only write information about relay log positions for recovery if innodb-recovery-update-relay-log is set.

  • Slave_retried_transactions now shows the total count of retried transactions over all replicas.

  • The status variable Com_slave_start is replaced with Com_start_slave.

  • The status variable Com_slave_stop is replaced with Com_stop_slave.

  • FLUSH RELAY LOGS are not replicated anymore. This is not safe as connection names may be different on the replica.

See Also

  • Multi-master ring replication

  • Using multi-source with global transaction id

  • The work in MariaDB is based on the project description at MDEV-253.

  • The original code base comes from Taobao, developed by Peng Lixun. A big thanks to them for this important feature!

This page is licensed: CC BY-SA / Gnu FDL

Multi-Master Ring Replication

Implement multi-master ring replication in MariaDB Server. This section covers configuring a circular replication topology for high availability and load balancing across multiple active master server

The terms master and slave have historically been used in replication, and MariaDB has begun the process of adding primary and replica synonyms. The old terms will continue to be used to maintain backward compatibility - see MDEV-18777 to follow progress on this effort.

What is Multi-Master Ring Replication

Multi-Master "Ring" Replication means that you have two or more masters where each master is replicating its data to another master asynchronously. This is not to be confused with MariaDB Galera Cluster which is a virtually synchronous multi-primary cluster for MariaDB.

The benefit of asynchronous replication compared to Galera Cluster, is that Ring Replication is resilient against bad network connections between the master servers. If a connection goes down, all masters will continue to serve its clients locally and data will automatically be synced when the link is available again.

The following picture shows one of the more advanced Multi-Master setups that is resilient against any master going down but can also handle 'human failures', like an accidental drop table, thanks to the addition of delayed slaves.

One should setup replication on each master like one does in standard MariaDB replication. The replication setup among the masters should be a ring. In other words, each master should replicate to one other master and each master should only have one other master as a slave.

Each master can of course have one or more normal slaves. A master could also be a slave of another master that is not in the ring setup.

All MariaDB servers support Multi-Master Ring Replication. In fact, when MySQL replication was originally designed around the year 2000, it was to be a Multi-Master Ring Replication solution for Yahoo to replicate from the East Coast to the West Coast.

Configuring the Masters

First, follow the instructions in setup replication. The main thing to remember is to use the master_use_gtid=current_pos option for CHANGE MASTER.

The main things that are different for Multi-Master Ring Replication are:

  • Give every master and slave in the replication setup a unique server_id. This can be a number from 1 to 4294967295 or 1-255 if one is using uuid_short(). It is a good practice to ensure that you do not have any servers in your system with the same server_id!

  • Use global transaction id (as described above)

  • Give each master a unique gtid_domain_id. This will allow replication to apply transactions from a different master in parallel independent from other masters.

Add the following into your my.cnf file for all masters and restart the servers.

[mariadb]
# Replace the following with a unique ID. All slaves of this master should have the same
# gtid_domain_id to allow easy failover to a slave if needed
gtid_domain_id=1
#
# Let us assume there will never be more than 10 masters in a Multi-Master ring setup
auto_increment_increment=10
# Set this to a different value 1-10 for each master. Could be the same as gtid_domain_id
# This is to ensure that all masters generate different values for AUTO_INCREMENT keys.
auto_increment_offset=1
#
# The following is needed to ensure the ALTER TABLE on another master will not
# break ring replication
slave_type_conversions=ALL_NON_LOSSY,ALL_LOSSY
#
# We cannot use semi-sync in Ring Replication as the masters need to be resilient against
# bad connections
rpl_semi_sync_master_enabled=0
#
# We have to log updates from other masters to the binary log.
log_slave_updates

Limitations when using Ring Replication

  • MariaDB does not yet support conflict resolution for conflicting changes. It is up to the application to ensure that there is never a conflicting insert/update/delete between the masters. The easiest setup is having each master server work on a different database or table. If not, one must:

    • Ensure you have an id (master-unique-id) for each row that unequally identifies the master who is responsible for this row. This should preferably be short and part of the primary key in each table. A good value for this would be the gtid_domain_id as this is unique for each local cluster.

    • Never insert rows with PRIMARY KEY or UNIQUE KEY values that can be same on another master. This can be avoided by

      • Have the master-unique-id part of all primary and unique keys.

      • In case of AUTO_INCREMENT keys, have a different value for auto_increment_offset on each master.

      • Use uuid_short() to generate unique values, like in create table t1 (a bigint unsigned default(uuid_short()) primary key). Note that if one is using uuid_short() in Multi-Master ring replication, one can only use server_id in the range 1-255!

    • Ensure that UPDATE and DELETE on each master only update rows generated by this master.

  • If several masters are constantly generating and updating rows for common tables, one has to be extra careful with ALTER TABLE to ensure that any change one does will not cause conflicts when the ALTER TABLE is replicated to other servers. In particular one has to ensure that all masters and their slaves are configured with slave_type_conversions=ALL_NON_LOSSY,ALL_LOSSY.

  • The server_id should be unique for each server. One should not change the server_id of an active master, as the ID is used by the master to recognize its own events and stop them from replicating endlessly around the ring (see replicate_same_server_id).

How does Multi-Master Ring Replication work

  • The main difference between Multi-Master Ring Replication and normal replication is that a change done by a master will eventually replicate back to it. When this happens, the master will see that the binary log event has the same server_id as the master has and will ignore the event. This is why it is critical to ensure that all server_id's are unique and that one does not change the server id.

  • When doing ALTER TABLE in a Multi-Master Ring replication setup, you should be aware the while you are running an ALTER TABLE on one master, another master can generate events that uses the old table definition. You should especially take care to not drop columns that could be in use by any application or still available in the upcoming replication stream.

How to resolve things if they go wrong in Multi-Master Ring Replication

When used correctly, Multi-Master Ring Replication is as resilient to errors as normal MariaDB master-slave replication. If the connection goes down, the replication will stop and will automatically continue when the connection resumes.

What to do when one of the masters dies and has to be replaced by a slave.

  • Ensure that the slave is up to date (has digested all relay events).

  • Check if there are any events on the old master that have not been sent to the slave. You can use mariadb-binlog to read the old master binary log files and apply them to the slave.

  • You can now treat the slave as a new master and put it back in the replication ring. The new master will use its replication GTID position to continue replication from the other master in the ring.

If the slave is not up to date and one cannot access any information of the old master, then one can continue the following way:

  • Enable the option --gtid-ignore-duplicates on the servers.

  • Add the slave to the replication ring.

  • The two masters (one of which is the old slave now added to the ring) will each replicate the events they are missing from one another. The --gtid-ignore-duplicates option is needed to allow the two masters in the ring to start replicating from each other when each server is ahead of the other in one domain and behind in another.

Error applying events

As long as each master handles their own set of data, as described above, there should not be any conflicting data coming from the other master.

If there are conflicts, one should resolve them as one resolves issues with normal replication. The most common way to solve issues is to skip the conflicting log events with SET GLOBAL SQL_SLAVE_SKIP_COUNTER.

Handling duplicate key errors and other conflicts

If things are setup correctly as described earlier, one should never get duplicate key errors in Multi-Master ring replication. Any duplicate key error or data mismatch is usually an application error where it inserts/updates or deletes something it should not have the right to do.

To fix this:

  • Use SET GLOBAL SQL_SLAVE_SKIP_COUNTER to skip the error.

  • Use `mariadb-binlog --base64-output=decode-rows --verbose --start-position=

binlog-name` to see what is missing and apply the changes missing on the server (minus the conflict).

Multi-Master Ring Replication through slaves

An alternative setup to use for Multi-master ring replications is to replicate to the other masters through slaves. The following setup shows how this can be done.

Benefits of replication through slaves

  • The slave will never be out of sync compared to other master. This makes failover from master to slave much easier as one does not have to check if slave has all data.

  • Slightly less load on the master

  • One can use semisynchronous-replication between master and slave.

  • Slave configuration is consolidated to one place. That is, if a master's immediate slave and replicating master should share the same configuration (e.g. rewrite rules, filters, etc), only the immediate slave needs them configured, as the replicating master will pull in the effects inherently.

Disadvantages of replication through slaves

  • There will be a slightly longer delay for the data to hit the next master as it has to go trough the slave. This can be notable if there is a very large transaction executed on the master.

  • If the master OR the slave dies, the replication to other masters will stop.

  • A replicating master is subject to the configuration of a slave (e.g. transactions may be incorrectly filtered out).

  • Re-setting replication after failover is a bit more complex.

Setting things up

  • Setup is identical to Multi-Master Ring Replication, except that rpl_semi_sync_master_enabled=0 is not required.

What to do when one of the masters dies and has to be replaced by a slave.

Let assume that master1 in the above picture has failed.

What needs to be done is to replace master1 with slave1 and add a new slave to replace slave1. Here follows a step by step description of how to do this.

The new slave that will be added to replace slave1 place will below be called slave3. The new master will be called master3 (to simplify explanations). Note that in some cases, the failed master can be re-used as the new slave if it did recover properly. If this is the case, reset all replications setups on the failed master.

Note that when one sets up a master->slave replication, all configurations are done only on the slave!

  • Promote slave1 as the new master3. Applications should now be moved to use master3. Note MaxScale can do this step automatically.

  • Setup master3 as a slave of slave2.

    • The ring replication is now active (at least temporarily, until we have slave3 in place).

  • Delete the old replication setup in master3 that pointed to the deleted master1.

  • Prepare the new slave3 (which can take a while if it based on a backup of master3).

  • Setup slave3 as a slave of master3.

  • If delayed slave1 exists, redirect it to be a slave of slave3.

  • Update master2 to be slave of slave3 (from being a slave of master3).

Some other options:

  • For semi-sync setups, the old master1 can be re-used as slave3 if re-started with --init-rpl-role=SLAVE during recovery

  • For non-semi-synchronous setups, one can use option CHANGE MASTER TO MASTER_DEMOTE_TO_SLAVE=1 (requires MariaDB 10.11 or higher).

See also

  • Multi-source replication

This page is licensed: CC BY-SA / Gnu FDL

Delayed Replication

Implement delayed replication in MariaDB Server. This section explains how to configure a time delay for replica application, providing a safety net against accidental data changes or logical errors.

The terms master and slave have historically been used in replication, and MariaDB has begun the process of adding primary and replica synonyms. The old terms will continue to be used to maintain backward compatibility - see MDEV-18777 to follow progress on this effort.

Delayed replication allows specifying that a replica should lag behind the primary by (at least) a specified amount of time (specified in seconds). Before executing an event, the replica will first wait, if necessary, until the given time has passed since the event was created on the primary. The result is that the replica will reflect the state of the primary some time back in the past.

The default is zero, or no delay, and the maximum value is 2147483647, or about 68 years.

Delayed replication is enabled using the MASTER_DELAY option to CHANGE MASTER:

CHANGE MASTER TO master_delay=3600;

A zero delay disables delayed replication. The replica must be stopped when changing the delay value.

New Variables to Measure Replication Lag

MariaDB 10.6 ES and 11.6 introduced new variables to measure replication lag. The variables are recommended to monitor the time difference between the master and slave.

  • Master_last_event_time: The timestamp of the last event read from the master by the IO thread.

  • Slave_last_event_time: The timestamp of the last event committed on the slave (from the master's perspective).

  • Master_Slave_time_diff: The difference in seconds between Master_last_event_time and Slave_last_event_time.

Fields in SHOW SLAVE STATUS are associated with delayed replication

  1. SQL_Delay: This is the value specified by MASTER_DELAY in CHANGE MASTER (or 0 if none).

  2. SQL_Remaining_Delay: When the replica is delaying the execution of an event due to MASTER_DELAY, this is the number of seconds of delay remaining before the event will be applied. Otherwise, the value is NULL.

  3. Seconds_Behind_Master:

  • If replication is not active, the value is NULL.

  • If all events has been executed and slave is now idle (waiting for new events), the value is 0.

  • If using parallel replication:

    • When starting to execute a new delayed event on an idle slave the value is clock_time_on_slave - clock_when_started_execution_of_the_event_on_master - clock_difference_between_master_and_slave

    • When event is committed the value is updated to: clock_time_on_slave - clock_when_ending_execution_of_the_event_on_master - clock_difference_between_master_and_slave

    • As Seconds_Behind_Master value depends on the timestamp of the last executed event, its value can be much bigger than SQL_Delay. For example if SQL_delay is 10 hours and the master sends a new event after not having executed anything for 20 hours, Seconds_behind_master will be 30 hours. When event starts to execute, Seconds_behind_master will still be growing until it commits. Then it will be changed to about 10 hours (more precisely to 10 hours + execution_time_on_slave - execution_time_on_master which corresponds to the above bullet). Since that the value will be growing until next event is executed and Seconds_behind_master is rebased on its completion time again. To expand more on this check a practical example in the comments.

  • If not using parallel replication the value is updated when starting to execute an event. The value is set to: clock_time_on_slave - clock_when_started_execution_of_the_event_on_master - clock_difference_between_master_and_slave. Note that in this case the clock is at start of event, not at end of event like in parallel replication.

  1. Slave_SQL_Running_State: This shows the state of the SQL driver threads, same as in SHOW PROCESSLIST. When the replica is delaying the execution of an event due to MASTER_DELAY, this fields displays: "Waiting until MASTER_DELAY seconds after master executed event".

When using older versions prior to MariaDB 10.2.3, a 3rd party tool called pt-slave-delay can be used. It is part of the Percona Toolkit. Note that pt-slave-delay does not support MariaDB multi-channel replication syntax.

clock_difference_between_master_and_slave

When setting up a master and slaves it is important that the internal clock has the same configuration. In almost all unix systems the internal clock is in UTC by default. On Windows system it may be in local time. This should be changed to be in UTC if one uses MariaDB with replication!

In addition to being in UTC, one should ensure that the clocks are synchronized. If not properly synchronized, there is a gap between the values of the clocks between different machines. MariaDB replication takes this into account by comparing the master and slave clocks when the slave connects to the master. The difference is clock_difference_between_master_and_slave.

This page is licensed: CC BY-SA / Gnu FDL

Parallel Replication

Boost MariaDB Server replication performance with parallel replication. This section explains how to configure replicas to apply events concurrently, reducing lag and improving throughput.

The terms master and slave have historically been used in replication, and MariaDB has begun the process of adding primary and replica synonyms. The old terms will continue to be used to maintain backward compatibility - see MDEV-18777 to follow progress on this effort.

Some writes, replicated from the primary can be executed in parallel (simultaneously) on the replica. Note that for parallel replication to work, both the primary and replica need to be MariaDB 10.0.5 or later.

Parallel Replication Overview

MariaDB replication in general takes place in three parts:

  • Replication events are read from the primary by the IO thread and queued in the relay log.

  • Replication events are fetched one at a time by the SQL thread from the relay log

  • Each event is applied on the replica to replicate all changes done on the primary.

Before MariaDB 10, the third step was also performed by the SQL thread; this meant that only one event could execute at a time, and replication was essentially single-threaded. Since MariaDB 10, the third step can optionally be performed by a pool of separate replication worker threads, and thereby potentially increase replication performance by applying multiple events in parallel.

How to Enable Parallel Replica

To enable, specify slave-parallel-threads=# in your my.cnf file as an argument to mysql. Parallel replication can in addition be disabled on a per-multi-source connection by setting @@connection_name.slave-parallel-mode to "none".

The value (#) of slave_parallel_threads specifies how many threads will be created in a pool of worker threads used to apply events in parallel for all your replicas (this includesmulti-source replication). If the value is zero, then no worker threads are created, and old-style replication is used where events are applied inside the SQL thread. Usually the value, if non-zero, should be at least two times the number of multi-source primary connections used. It makes little sense to use only a single worker thread for one connection; this will incur some overhead in inter-thread communication between the SQL thread and the worker thread, but with just a single worker thread events can not be applied in parallel anyway.

slave-parallel-threads=# is a dynamic variable that can be changed without restarting mysqld. All replicas connections must however be stopped when changing the value.

Configuring the Replica Parallel Mode

Parallel replication can be in-order or out-of-order:

  • In-order executes transactions in parallel, but orders the commit step of the transactions to happen in the exact same order as on the primary. Transactions are only executed in parallel to the extent that this can be automatically verified to be possible without any conflicts. This means that the use of parallelism is completely transparent to the application.

  • Out-of-order can execute and commit transactions in different order on the replica than originally on the primary. This means that the application must be tolerant to seeing updates occur in different order. The application is also responsible for ensuring that there are no conflicts between transactions that are replicated out-of-order. Out-of-order is only used in GTID mode and only when explicitly enabled by the application, using the replication domain that is part of the GTID.

In-Order Parallel Replication

Optimistic Mode of In-Order Parallel Replication

Optimistic mode of in-order parallel replication provides a lot of opportunities for parallel apply on the replica while still preserving exact transaction semantics from the point of view of applications. It is the default mode from MariaDB 10.5.1.

Optimistic mode of in-order parallel replication can be configured by setting the slave_parallel_mode system variable to optimistic on the replica.

Any transactional DML (INSERT/UPDATE/DELETE) is allowed to run in parallel, up to the limit of @@slave_domain_parallel_threads. This may cause conflicts on the replica, eg. if two transactions try to modify the same row. Any such conflict is detected, and the latter of the two transactions is rolled back, allowing the former to proceed. The latter transaction is then re-tried once the former has completed.

The term "optimistic" is used for this mode, because the server optimistically assumes that few conflicts will occur, and that the extra work spent rolling back and retrying conflicting transactions is justified from the gain from running most transactions in parallel.

There are a few heuristics to try to avoid needless conflicts. If a transaction executed a row lock wait on the primary, it will not be run in parallel on the replica. Transactions can also be marked explicitly as potentially conflicting on the primary, by setting the variable@@skip_parallel_replication. More such heuristics may be added in later MariaDB versions. There is a further --slave-parallel-mode called "aggressive", where these heuristics are disabled, allowing even more transactions to be applied in parallel.

Non-transactional DML and DDL is not safe to optimistically apply in parallel, as it cannot be rolled back in case of conflicts. Thus, in optimistic mode, non-transactional (such as MyISAM) updates are not applied in parallel with earlier events (it is however possible to apply a MyISAM update in parallel with a later InnoDB update). DDL statements are not applied in parallel with any other transactions, earlier or later.

The different kind of transactions can be identified in the output ofmariadb-binlog. For example:

#150324 13:06:26 server id 1  end_log_pos 6881 	GTID 0-1-42 ddl
...
#150324 13:06:26 server id 1  end_log_pos 7816 	GTID 0-1-47
...
#150324 13:06:26 server id 1  end_log_pos 8177  GTID 0-1-49 trans
/*!100101 SET @@session.skip_parallel_replication=1*//*!*/;
...
#150324 13:06:26 server id 1  end_log_pos 9836 	GTID 0-1-59 trans waited

GTID 0-1-42 is marked as being DDL. GTID 0-1-47 is marked as being non-transactional DML, while GTID 0-1-49 is transactional DML (seen on the "trans" keyword). GTID 0-1-49 was additionally run with@@skip_parallel_replication set on the primary. GTID 0-1-59 is transactional DML that had a row lock wait when run on the primary (the "waited" keyword).

Aggressive Mode of In-Order Parallel Replication

Aggressive mode of in-order parallel replication is very similar to optimistic mode. The main difference is that the replica does not consider whether transactions conflicted on the primary when deciding whether to apply the transactions in parallel.

Aggressive mode of in-order parallel replication can be configured by setting the slave_parallel_mode system variable to aggressive on the replica.

Conservative Mode of In-Order Parallel Replication

Conservative mode of in-order parallel replication uses the group commit on the primary to discover potential for parallel apply of events on the replica. If two transactions commit together in a group commit on the primary, they are written into the binlog with the same commit id. Such events are certain to not conflict with each other, and they can be scheduled by the parallel replication to run in different worker threads.

Conservative mode of in-order parallel replication is the default mode until MariaDB 10.5.0, but it can also be configured by setting the slave_parallel_mode system variable to conservative on the replica.

Two transactions that were committed separately on the primary can potentially conflict (eg. modify the same row of a table). Thus, the worker that applies the second transaction will not start immediately, but wait until the first transaction begins the commit step; at this point it is safe to start the second transaction, as it can no longer disrupt the execution of the first one.

Here is example output from mariadb-binlog that shows how GTID events are marked with commit id. The GTID 0-1-47 has no commit id, and can not run in parallel. The GTIDs 0-1-48 and 0-1-49 have the same commit id 630, and can thus replicate in parallel with one another on a replica:

#150324 12:54:24 server id 1  end_log_pos 20052 	GTID 0-1-47 trans
...
#150324 12:54:24 server id 1  end_log_pos 20212 	GTID 0-1-48 cid=630 trans
...
#150324 12:54:24 server id 1  end_log_pos 20372 	GTID 0-1-49 cid=630 trans

In either case, when the two transactions reach the point where the low-level commit happens and commit order is determined, the two commits are sequenced to happen in the same order as on the primary, so that operation is transparent to applications.

The opportunities for parallel replication on replicas can be highly increased if more transactions are committed in a group commit on the primary. This can be tuned using the binlog_commit_wait_count andbinlog_commit_wait_usec variables. If for example the application can tolerate up to 50 milliseconds extra delay for transactions on the primary, one can set binlog_commit_wait_usec=50000 andbinlog_commit_wait_count=20 to get up to 20 transactions at a time available for replication in parallel. Care must however be taken to not set binlog_commit_wait_usec too high, as this could cause significant slowdown for applications that run a lot of small transactions serially one after the other.

Note that even if there is no parallelism available from the primary group commit, there is still an opportunity for speedup from in-order parallel replication, since the actual commit steps of different transactions can run in parallel. This can be particularly effective on a replica with binlog enabled (log_slave_updates=1), and more so if replica is configured to be crash-safe (sync_binlog=1 andinnodb_flush_log_at_trx_commit=1), as this makes group commit possible on the replica.

Minimal Mode of In-Order Parallel Replication

Minimal mode of in-order parallel replication onlyallows the commit step of transactions to be applied in parallel; all other steps are applied serially.

Minimal mode of in-order parallel replication can be configured by setting the slave_parallel_mode system variable to minimal on the replica.

Out-of-Order Parallel Replication

Out-of-order parallel replication happens (only) when using GTID mode, when GTIDs with different replication domains are used. The replication domain is set by the DBA/application using the variable gtid_domain_id.

Two transactions having GTIDs with different domain_id are scheduled to different worker threads by parallel replication, and are allowed to execute completely independently from each other. It is the responsibility of the application to only set different domain_ids for transactions that are truly independent, and are guaranteed to not conflict with each other. The application must also be able to work correctly even though the transactions with different domain_id are seen as committing in different order between the replica and the primary, and between different replicas.

Out-of-order parallel replication can potentially give more performance gain than in-order parallel replication, since the application can explicitly give more opportunities for running transactions in parallel than what the server can determine on its own automatically.

One simple but effective usage is to run long-running statements, such as ALTER TABLE, in a separate replication domain. This allows replication of other transactions to proceed uninterrupted:

SET SESSION gtid_domain_id=1
ALTER TABLE t ADD INDEX myidx(b)
SET SESSION gtid_domain_id=0

Normally, a long-running ALTER TABLE or other query will stall all following transactions, causing the replica to become behind the primary as least as long time as it takes to run the long-running query. By using out-of-order parallel replication by setting the replication domain id, this can be avoided. The DBA/application must ensure that no conflicting transactions will be replicated while the ALTER TABLE runs.

Another common opportunity for out-of-order parallel replication comes in connection with multi-source replication. Suppose we have two different primaries M1 and M2, and we are using multi-source replication to have S1 as a replica of both M1 and M2. S1 will apply events received from M1 in parallel with events received from M2. If we now have a third-level replica S2 that replicates from S1 as primary, we want S2 to also be able to apply events that originated on M1 in parallel with events that originated on M2. This can be achieved with out-of-order parallel replication, by settinggtid_domain_id different on M1 and M2.

Note that there are no special restrictions on what operations can be replicated in parallel using out-of-order; such operations can be on the same database/schema or even on the same table. The only restriction is that the operations must not conflict, that is they must be able to be applied in any order and still end up with the same result.

When using out-of-order parallel replication, the current replica position in the primary's binlog becomes multi-dimensional - each replication domain can have reached a different point in the primary binlog at any one time. The current position can be seen from the variablegtid_slave_pos. When the replica is stopped, restarted, or switched to replicate from a different primary using CHANGE MASTER, MariaDB automatically handles restarting each replication domain at the appropriate point in the binlog.

Out-of-order parallel replication is disabled when--slave-parallel-mode=minimal (or none).

Checking Worker Thread Status in SHOW PROCESSLIST

The worker threads will be listed as "system user" in SHOW PROCESSLIST. Their state will show the query they are currently working on, or it can show one of these:

  • "Waiting for work from main SQL threads". This means that the worker thread is idle, no work is available for it at the moment.

  • "Waiting for prior transaction to start commit before starting next transaction". This means that the previous batch of transactions that committed together on the primary primary has to complete first. This worker thread is waiting for that to happen before it can start working on the following batch.

  • "Waiting for prior transaction to commit". This means that the transaction has been executed by the worker thread. In order to ensure in-order commit, the worker thread is waiting to commit until the previous transaction is ready to commit before it.

Expected Performance Gain

Here is an article showing up to ten times improvement when using parallel replication: 18435.html.

Configuring the Maximum Size of the Parallel Replica Queue

The slave_parallel_max_queued system variable can be used to configure the maximum size of the parallel replica queue. This system variable is only meaningful when parallel replication is configured (i.e. when slave_parallel_threads > 0).

When parallel replication is used, the SQL thread will read ahead in the relay logs, queueing events in memory while looking for opportunities for executing events in parallel. The slave_parallel_max_queued system variable sets a limit for how much memory it will use for this.

The configured value of the slave_parallel_max_queued system variable is actually allocated for each worker thread, so the total allocation is actually equivalent to the following:

slave_parallel_max_queued * slave_parallel_threads

If this value is set too high, and the replica is far (eg. gigabytes of binlog) behind the primary, then the SQL thread can quickly read all of that and fill up memory with huge amounts of binlog events faster than the worker threads can consume them.

On the other hand, if set too low, the SQL thread might not have sufficient space for queuing enough events to keep the worker threads busy, which could reduce performance. In this case, the SQL thread will have the thread state that states Waiting for room in worker thread event queue. For example:

+----+-------------+-----------+------+---------+--------+-----------------------------------------------+------------------+----------+
| Id | User        | Host      | db   | Command | Time   | State                                         | Info             | Progress |
+----+-------------+-----------+------+---------+--------+-----------------------------------------------+------------------+----------+
|  3 | system user |           | NULL | Connect |    139 | closing tables                                | NULL             |    0.000 |
|  4 | system user |           | NULL | Connect |    139 | Waiting for work from SQL thread              | NULL             |    0.000 |
|  6 | system user |           | NULL | Connect | 264274 | Waiting for master to send event              | NULL             |    0.000 |
| 10 | root        | localhost | NULL | Sleep   |     43 |                                               | NULL             |    0.000 |
| 21 | system user |           | NULL | Connect |     45 | Waiting for room in worker thread event queue | NULL             |    0.000 |
| 54 | root        | localhost | NULL | Query   |      0 | init                                          | SHOW PROCESSLIST |    0.000 |
+----+-------------+-----------+------+---------+--------+-----------------------------------------------+------------------+----------+

The slave_parallel_max_queued system variable does not define a hard limit, since the binary log events that are currently executing always need to be held in-memory. This means that at least two events per worker thread can always be queued in-memory, regardless of the value of slave_parallel_threads.

Usually, the slave_parallel_threads system variable should be set large enough that the SQL thread is able to read far enough ahead in the binary log to exploit all possible parallelism. In normal operation, the replica will hopefully not be too far behind, so there will not be a need to queue much data in-memory. The slave_parallel_max_queued system variable could be set fairly high (eg. a few hundred kilobytes) to not limit throughtput. It should just be set low enough that total allocation of the parallel replica queue will not cause the server to run out of memory.

Configuration Variable slave_domain_parallel_threads

The pool of replication worker threads is shared among all multi-source primary connections, and among all replication domains that can replicate in parallel using out-of-order.

If one primary connection or replication domain is currently processing a long-running query, it is possible that it will allocate all the worker threads in the pool, only to have them wait for the long-running query to complete, stalling any other primary connection or replication domain, which will have to wait for a worker thread to become free.

This can be avoided by settingslave_domain_parallel_threads to a number that is lower than slave_parallel_threads. When set different from zero, each replication domain in one primary connection can reserve at most that many worker threads at any one time, leaving the rest (up to the value ofslave_parallel_threads) free for other primary connections or replication domains to use in parallel.

The slave_domain_parallel_threads variable is dynamic and can be changed without restarting the server; all replicas must be stopped while changing it, though.

Implementation Details

The implementation is described in MDEV-4506.

See Also

  • Better Parallel Replication for MariaDB and MySQL (MariaDB.com blog)

  • Evaluating MariaDB & MySQL Parallel Replication Part 2: Slave Group Commit (MariaDB.com blog)

This page is licensed: CC BY-SA / Gnu FDL

Semisynchronous Replication

Implement semi-synchronous replication in MariaDB Server. How to ensure data durability by requiring at least one replica to acknowledge receipt of transactions before the master commits.

Description

Standard MariaDB replication is asynchronous, but MariaDB also provides a semisynchronous replication option. The feature is built into the server and is always available. In versions prior to MariaDB 10.3, it was a separate plugin that needed to be installed.

With regular asynchronous replication, replicas request events from the primary's binary log whenever the replicas are ready. The primary does not wait for a replica to confirm that an event has been received.

With fully synchronous replication, all replicas are required to respond that they have received the events. See Galera Cluster.

Semisynchronous replication waits for just one replica to acknowledge that it has received and logged the events.

Semisynchronous replication therefore comes with some negative performance impact, but increased data integrity. Since the delay is based on the roundtrip time to the replica and back, this delay is minimized for servers in close proximity over fast networks.

Semisynchronous replication is built into the server. See MDEV-13073 for more information.

Enabling Semisynchronous Replication

Semisynchronous replication can be enabled by setting the relevant system variables on the primary and the replica.

If a server needs to be able to switch between acting as a primary and a replica, then you can enable both the primary and replica system variables on the server. For example, you might need to do this if MariaDB MaxScale is being used to enable auto-failover or switchover with MariaDB Monitor.

Enabling Semisynchronous Replication on the Primary

Semisynchronous replication can be enabled on the primary by setting the rpl_semi_sync_master_enabled system variable to ON. It can be set dynamically with SET GLOBAL. For example:

SET GLOBAL rpl_semi_sync_master_enabled=ON;

It can also be set in a server option group in an option file prior to starting up the server. For example:

[mariadb]
...
rpl_semi_sync_master_enabled=ON

Enabling Semisynchronous Replication on the Replica

Semisynchronous replication can be enabled on the replica by setting the rpl_semi_sync_slave_enabled system variable to ON. It can be set dynamically with SET GLOBAL. For example:

SET GLOBAL rpl_semi_sync_slave_enabled=ON;

It can also be set in a server option group in an option file prior to starting up the server. For example:

[mariadb]
...
rpl_semi_sync_slave_enabled=ON

When switching between semisynchronous replication and asynchronous replication on a replica with replica IO threads already running, the replica I/O thread will need to be restarted. For example:

STOP SLAVE IO_THREAD;
START SLAVE IO_THREAD;

If this is not done, then the replica IO thread will continue to use the previous setting.

Configuring the Primary Timeout

In semisynchronous replication, only after the events have been written to the relay log and flushed does the replica acknowledge receipt of a transaction's events. If the replica does not acknowledge the transaction before a certain amount of time has passed, then a timeout occurs and the primary switches to asynchronous replication. This will be reflected in the primary's error log with messages like the following:

[Warning] Timeout waiting for reply of binlog (file: mariadb-1-bin.000002, pos: 538), semi-sync up to file , position 0.
[Note] Semi-sync replication switched OFF.

When this occurs, the Rpl_semi_sync_master_status status variable will be switched to OFF.

When at least one semisynchronous replica catches up, semisynchronous replication is resumed. This will be reflected in the primary's error log with messages like the following:

[Note] Semi-sync replication switched ON with replica (server_id: 184137206) at (mariadb-1-bin.000002, 215076)

When this occurs, the Rpl_semi_sync_master_status status variable will be switched to ON.

The number of times that semisynchronous replication has been switched off can be checked by looking at the value of the Rpl_semi_sync_master_no_times status variable.

If you see a lot of timeouts like this in your environment, then you may want to change the timeout period. The timeout period can be changed by setting the rpl_semi_sync_master_timeout system variable. It can be set dynamically with SET GLOBAL. For example:

SET GLOBAL rpl_semi_sync_master_timeout=20000;

It can also be set in a server option group in an option file prior to starting up the server. For example:

[mariadb]
...
rpl_semi_sync_master_timeout=20000

To determine a good value for the rpl_semi_sync_master_timeout system variable, you may want to look at the values of the Rpl_semi_sync_master_net_avg_wait_time and Rpl_semi_sync_master_tx_avg_wait_time status variables.

Configuring the Primary Wait Point

In semisynchronous replication, there are two potential points at which the primary can wait for the replica acknowledge the receipt of a transaction's events. These two wait points have different advantages and disadvantages.

The wait point is configured by the rpl_semi_sync_master_wait_point system variable. The supported values are:

  • AFTER_SYNC

  • AFTER_COMMIT

It can be set dynamically with SET GLOBAL. For example:

SET GLOBAL rpl_semi_sync_master_wait_point='AFTER_SYNC';

It can also be set in a server option group in an option file prior to starting up the server. For example:

[mariadb]
...
rpl_semi_sync_master_wait_point=AFTER_SYNC

When this variable is set to AFTER_SYNC, the primary performs the following steps:

  1. Prepares the transaction in the storage engine.

  2. Syncs the transaction to the binary log.

  3. Waits for acknowledgement from the replica.

  4. Commits the transaction to the storage engine.

  5. Returns an acknowledgement to the client.

The effects of the AFTER_SYNC wait point are:

  • All clients see the same data on the primary at the same time; after acknowledgement by the replica and after being committed to the storage engine on the primary.

  • If the primary crashes, then failover should be lossless, because all transactions committed on the primary would have been replicated to the replica.

  • However, if the primary crashes, then its binary log may also contain events for transactions that were prepared by the storage engine and written to the binary log, but that were never actually committed by the storage engine. As part of the server's automatic crash recovery process, the server may recover these prepared transactions when the server is restarted. This could cause the "old" crashed primary to become inconsistent with its former replicas when they have been reconfigured to replace the old primary with a new one. The old primary in such a scenario can be re-introduced only as a semisync replica. The server post-crash recovery of the server configured with rpl_semi_sync_slave_enabled = ON ensures through MDEV-21117 that the server will not have extra transactions. The reconfigured as semisync replica server's binlog gets truncated to discard transactions proven not to be committed, in any of their branches if they are multi-engine. Truncation does not occur though when there exists a non-transactional group of events beyond the truncation position in which case recovery reports an error. When the semisync replica recovery can't be carried out, the crashed primary may need to be rebuilt.

When this variable is set to AFTER_COMMIT, the primary performs the following steps:

  1. Prepares the transaction in the storage engine.

  2. Syncs the transaction to the binary log.

  3. Commits the transaction to the storage engine.

  4. Waits for acknowledgement from the replica.

  5. Returns an acknowledgement to the client.

The effects of the AFTER_COMMIT wait point are:

  • Other clients may see the committed transaction before the committing client.

  • If the primary crashes, then failover may involve some data loss, because the primary may have committed transactions that had not yet been acknowledged by the replicas.

Versions

Version
Status
Introduced

N/A

N/A

MariaDB 10.3.3 (feature is built-in, no longer available as a separate plugin)

1.0

Stable

MariaDB 10.1.13

1.0

Gamma

MariaDB 10.0.13

1.0

Unknown

MariaDB 10.0.11

1.0

N/A

MariaDB 5.5

System Variables

rpl_semi_sync_master_enabled

  • Description: Set to ON to enable semi-synchronous replication primary. Disabled by default.

  • Commandline: --rpl-semi-sync-master-enabled[={0|1}]

  • Scope: Global

  • Dynamic: Yes

  • Data Type: boolean

  • Default Value: OFF

rpl_semi_sync_master_timeout

  • Description: The timeout value, in milliseconds, for semi-synchronous replication in the primary. If this timeout is exceeded in waiting on a commit for acknowledgement from a replica, the primary will revert to asynchronous replication.

    • When a timeout occurs, the Rpl_semi_sync_master_status status variable will also be switched to OFF.

    • See Configuring the Primary Timeout for more information.

  • Commandline: --rpl-semi-sync-master-timeout[=#]

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 10000 (10 seconds)

  • Range: 0 to 18446744073709551615

rpl_semi_sync_master_trace_level

  • Description: The tracing level for semi-sync replication. Four levels are defined:

    • 1: General level, including for example time function failures.

    • 16: More detailed level, with more verbose information.

    • 32: Net wait level, including more information about network waits.

    • 64: Function level, including information about function entries and exits.

  • Commandline: --rpl-semi-sync-master-trace-level[=#]

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 32

  • Range: 0 to 18446744073709551615

rpl_semi_sync_master_wait_no_slave

  • Description: If set to ON, the default, the replica count (recorded by Rpl_semi_sync_master_clients) may drop to zero, and the primary will still wait for the timeout period. If set to OFF, the primary will revert to asynchronous replication as soon as the replica count drops to zero.

  • Commandline: --rpl-semi-sync-master-wait-no-slave[={0|1}]

  • Scope: Global

  • Dynamic: Yes

  • Data Type: boolean

  • Default Value: ON

rpl_semi_sync_master_wait_point

  • Description: Whether the transaction should wait for semi-sync acknowledgement after having synced the binlog (AFTER_SYNC), or after having committed in storage engine (AFTER_COMMIT, the default).

    • When this variable is set to AFTER_SYNC, the primary performs the following steps:

      1. Prepares the transaction in the storage engine.

      2. Syncs the transaction to the binary log.

      3. Waits for acknowledgement from the replica.

      4. Commits the transaction to the storage engine.

      5. Returns an acknowledgement to the client.

    • When this variable is set to AFTER_COMMIT, the primary performs the following steps:

      1. Prepares the transaction in the storage engine.

      2. Syncs the transaction to the binary log.

      3. Commits the transaction to the storage engine.

      4. Waits for acknowledgement from the replica.

      5. Returns an acknowledgement to the client.

    • In MariaDB 10.1.2 and before, this system variable does not exist. However, in those versions, the primary waits for the acknowledgement from replicas at a point that is equivalent to AFTER_COMMIT.

    • See Configuring the Primary Wait Point for more information.

  • Commandline: --rpl-semi-sync-master-wait-point=value

  • Scope: Global

  • Dynamic: Yes

  • Data Type: enum

  • Default Value: AFTER_COMMIT

  • Valid Values: AFTER_SYNC, AFTER_COMMIT

rpl_semi_sync_master_wait_for_slave_count

  • Description: The number of replicas that need to acknowledge that they have received a transaction before the transaction can complete on the primary.

  • Commandline: --rpl-semi-sync-master-wait-for-slave-count=#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 1

  • Range: 0 to 65535

  • Introduced: MariaDB 12.1

rpl_semi_sync_slave_delay_master

  • Description: Only write primary info file when ack is needed.

  • Commandline: --rpl-semi-sync-slave-delay-master[={0|1}]

  • Scope: Global

  • Dynamic: Yes

  • Data Type: boolean

  • Default Value: OFF

rpl_semi_sync_slave_enabled

  • Description: Set to ON to enable semi-synchronous replication replica. Disabled by default.

  • Commandline: --rpl-semi-sync-slave-enabled[={0|1}]

  • Scope: Global

  • Dynamic: Yes

  • Data Type: boolean

  • Default Value: OFF

rpl_semi_sync_slave_kill_conn_timeout

  • Description: Timeout for the mysql connection used to kill the replica io_thread's connection on primary. This timeout comes into play when stop slave is executed.

  • Commandline: --rpl-semi-sync-slave-kill-conn-timeout[={0|1}]

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 5

  • Range: 0 to 4294967295

rpl_semi_sync_slave_trace_level

  • Description: The tracing level for semi-sync replication. The levels are the same as for rpl_semi_sync_master_trace_level.

  • Commandline: --rpl-semi-sync-slave-trace_level[=#]

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 32

  • Range: 0 to 18446744073709551615

Options

init-rpl-rol

  • From MariaDB 10.6.19, MariaDB 10.11.9, MariaDB 11.1.6, MariaDB 11.2.5, MariaDB 11.4.3 and MariaDB 11.5.2, changes the condition for semi-sync recovery to truncate the binlog to instead use this option, when set to SLAVE. This avoids a possible error state where the replica’s state is ahead of the primary’s. See -init-rpl-role.

rpl-semi-sync_master

  • Description: Controls how the server should treat the plugin when the server starts up.

    • Valid values are:

      • OFF - Disables the plugin without removing it from the mysql.plugins table.

      • ON - Enables the plugin. If the plugin cannot be initialized, then the server will still continue starting up, but the plugin will be disabled.

      • FORCE - Enables the plugin. If the plugin cannot be initialized, then the server will fail to start with an error.

      • FORCE_PLUS_PERMANENT - Enables the plugin. If the plugin cannot be initialized, then the server will fail to start with an error. In addition, the plugin cannot be uninstalled with UNINSTALL SONAME or UNINSTALL PLUGIN while the server is running.

    • See Plugin Overview: Configuring Plugin Activation at Server Startup for more information.

  • Commandline: --rpl-semi-sync-master=value

  • Data Type: enumerated

  • Default Value: ON

  • Valid Values: OFF, ON, FORCE, FORCE_PLUS_PERMANENT

  • Removed: MariaDB 10.3.3

rpl-semi-sync_slave

  • Description: Controls how the server should treat the plugin when the server starts up.

    • Valid values are:

      • OFF - Disables the plugin without removing it from the mysql.plugins table.

      • ON - Enables the plugin. If the plugin cannot be initialized, then the server will still continue starting up, but the plugin will be disabled.

      • FORCE - Enables the plugin. If the plugin cannot be initialized, then the server will fail to start with an error.

      • FORCE_PLUS_PERMANENT - Enables the plugin. If the plugin cannot be initialized, then the server will fail to start with an error. In addition, the plugin cannot be uninstalled with UNINSTALL SONAME or UNINSTALL PLUGIN while the server is running.

    • See Plugin Overview: Configuring Plugin Activation at Server Startup for more information.

  • Commandline: --rpl-semi-sync-slave=value

  • Data Type: enumerated

  • Default Value: ON

  • Valid Values: OFF, ON, FORCE, FORCE_PLUS_PERMANENT

  • Removed: MariaDB 10.3.3

Status Variables

For a list of status variables added when the plugin is installed, see Semisynchronous Replication Plugin Status Variables.

This page is licensed: CC BY-SA / Gnu FDL

Row-based Replication With No Primary Key

Learn about row-based replication in MariaDB Server without a primary key. This section discusses the implications and challenges of replicating data when tables lack a unique identifier.

The terms master and slave have historically been used in replication, and MariaDB has begun the process of adding primary and replica synonyms. The old terms will continue to be used to maintain backward compatibility - see MDEV-18777 to follow progress on this effort.

MariaDB improves on row-based replication (see binary log formats) of tables which have no primary key but do have some other index. This is based in part on the original Percona patch "row_based_replication_without_primary_key.patch", with some additional fixes and enhancements.

When row-based replication is used with UPDATE or DELETE, the slave needs to locate each replicated row based on the value in columns. If the table contains at least one index, an index lookup will be used (otherwise a table scan is needed for each row, which is extremely inefficient for all but the smallest table and generally to be avoided).

In MariaDB, the slave will try to choose a good index among any available:

  • The primary key is used, if there is one.

  • Else, the first unique index without NULL-able columns is used, if there is one.

  • Else, a choice is made among any normal indexes on the table (e.g. aFULLTEXT index is not considered).

The choice of which of several non-unique indexes to use is based on the cardinality of indexes; the one that is most selective (has the smallest average number of rows per distinct tuple of column values) is preferred. Note that for this choice to be effective, for most storage engines (like MyISAM, InnoDB) it is necessary to make sure ANALYZE TABLE has been run on the slave, otherwise statistics about index cardinality will not be available. In the absence of index cardinality, the first unique index will be chosen, if any, else the first non-unique index.

Prior to MariaDB 5.3, the slave would always choose the first index without considering cardinality. The slave could even choose an unusable index (like FULLTEXT) if no other index was available (MySQL Bug #58997), causing row-based replication to break in this case; this was also fixed in MariaDB 5.3.

See Also

  • What is MariaDB 5.3

This page is licensed: CC BY-SA / Gnu FDL

Unsafe Statements for Statement-Based Replication

Identify unsafe statements for statement-based replication in MariaDB Server. This section details SQL commands that can cause inconsistencies, guiding you toward safer replication practices.

A safe statement is generally deterministic; in other words the statement will always produce the same result. For example, an INSERT statement producing a random number will most likely produce a different result on the primary than on the replica, and so cannot be replicated safely.

When an unsafe statement is run, the current binary logging format determines how the server responds.

  • If the binary logging format is statement-based, unsafe statements generate a warning and are logged normally.

  • If the binary logging format is mixed, unsafe statements are logged using the row-based format, while safe statements use the statement-based format.

  • If the binary logging format is row-based, all statements are logged normally, and the distinction between safe and unsafe is not made.

MariaDB tries to detect unsafe statements. When an unsafe statement is issued, a warning similar to the following is produced:

Note (Code 1592): Unsafe statement written to the binary log using statement format since 
  BINLOG_FORMAT = STATEMENT. The statement is unsafe because it uses a LIMIT clause. This 
  is unsafe because the set of rows included cannot be predicted.

MariaDB also issues this warning for some classes of statements that are safe.

Unsafe Statements

The following statements are regarded as unsafe:

  • INSERT ... ON DUPLICATE KEY UPDATE statements using tables with multiple primary or unique keys, as the order that the keys are checked in, and which affect the rows chosen to update, is not deterministic. The warning about this was removed, because we always check keys in the same order on the primary and replica if the primary and replica are using the same storage engine.

  • INSERT-DELAYED. These statements are inserted in an indeterminate order.

  • INSERT ... SELECT for a table that has an AUTO_INCREMENT column.

  • INSERTs on tables with a composite primary key that has an AUTO_INCREMENT column that isn't the first column of the composite key.

  • When a table has an AUTO_INCREMENT column and a trigger or stored procedure executes an UPDATE statement against the table.

  • UPDATE statements that use LIMIT, since the order of the returned rows is unspecified. This applies even to statements using an ORDER BY clause, which are deterministic (a known bug). However, LIMIT 0 is an exception to this rule (see MDEV-6170), and these statements are safe for replication.

  • When using a user-defined function.

  • Statements using any of the following functions, which can return different results on the replica:

    • CURRENT_ROLE()

    • CURRENT_USER()

    • FOUND_ROWS()

    • GET_LOCK()

    • IS_FREE_LOCK()

    • IS_USED_LOCK()

    • JSON_TABLE()

    • LOAD_FILE()

    • MASTER_POS_WAIT()

    • RAND()

    • RANDOM_BYTES()

    • RELEASE_ALL_LOCKS()

    • RELEASE_LOCK()

    • ROW_COUNT()

    • SESSION_USER()

    • SLEEP()

    • SYSDATE()

    • SYSTEM_USER()

    • USER()

    • UUID()

    • UUID_SHORT().

  • Statements which refer to log tables, since these may differ across servers.

  • Statements which refer to self-logging tables. Statements following a read or write to a self-logging table within a transaction are also considered unsafe.

  • Statements which refer to system variables (there are a few exceptions).

  • LOAD DATA INFILE statements.

  • Non-transactional reads or writes that execute after transactional reads within a transaction.

  • If row-based logging is used for a statement, and the session executing the statement has any temporary tables, row-based logging is used for the remaining statements until the temporary table is dropped. This is because temporary tables can't use row-based logging, so if it is used due to one of the above conditions, all subsequent statements using that table are unsafe. The server deals with this situation by treating all statements in the session as unsafe for statement-based logging until the temporary table is dropped.

Safe Statements

The following statements are not deterministic, but are considered safe for binary logging and replication:

  • CONNECTION_ID()

  • CURDATE()

  • CURRENT_DATE()

  • CURRENT_TIME()

  • CURRENT_TIMESTAMP()

  • CURTIME()

  • LAST_INSERT_ID()

  • LOCALTIME()

  • LOCALTIMESTAMP()

  • NOW()

  • UNIX_TIMESTAMP()

  • UTC_DATE()

  • UTC_TIME()

  • UTC_TIMESTAMP()

Isolation Levels

Even when using safe statements, not all transaction isolation levels are safe with statement-based or mixed binary logging. While the REPEATABLE READ and SERIALIZABLE isolation levels can be used with both statement- and row-based replication, the READ COMMITTED and READ UNCOMMITTED isolation levels only support row-based replication, as with them isolation between transactions is not guaranteed at all, and different transaction orders on a replica, or when doing point-in-time recovery, would lead to different results than on the original master.

This restriction does not apply if only non-transactional storage engines are used.

See Also

  • Replication and Foreign Keys

This page is licensed: CC BY-SA / Gnu FDL

Replication Filters

Use replication filters to control what data is replicated. This section explains how to include or exclude specific databases or tables, enhancing flexibility and resource management.

The terms master and slave have historically been used in replication, and MariaDB has begun the process of adding primary and replica synonyms. The old terms will continue to be used to maintain backward compatibility - see MDEV-18777 to follow progress on this effort.

Replication filters allow users to configure replicas to intentionally skip certain events.

Binary Log Filters for Replication Primaries

MariaDB provides options that can be used on a replication primary to restrict local changes to specific databases from getting written to the binary log, which also determines whether any replicas replicate those changes.

Binary Log Filter Options

The following options are available, and they are evaluated in the order that they are listed below. If there are conflicting settings, binlog_do_db prevails. Before MariaDB 11.2.0, they are only available as options; from MariaDB 11.2.0 they are also available as system variables.

binlog_do_db

The binlog_do_db option allows you to configure a replication primary to write statements and transactions affecting databases that match a specified name into its binary log. Since the filtered statements or transactions will not be present in the binary log, its replicas will not be able to replicate them.

This option will not work with cross-database updates with statement-based logging. See the Statement-Based Logging section for more information.

This option can not be set dynamically.

When setting it on the command-line or in a server option group in an option file, the option does not accept a comma-separated list. If you would like to specify multiple filters, then you need to specify the option multiple times. For example:

[mariadb]
...
binlog_do_db=db1
binlog_do_db=db2

This will tell the primary to do the following:

  • Write statements and transactions affecting the database named db1 into the binary log.

  • Write statements and transactions affecting the database named db2 into the binary log.

  • Don't write statements and transactions affecting any other databases into the binary log.

binlog_ignore_db

The binlog_ignore_db option allows you to configure a replication primary to not write statements and transactions affecting databases that match a specified name into its binary log. Since the filtered statements or transactions will not be present in the binary log, its replicas will not be able to replicate them.

This option will not work with cross-database updates with statement-based logging. See the Statement-Based Logging section for more information.

This option can not be set dynamically.

When setting it on the command-line or in a server option group in an option file, the option does not accept a comma-separated list. If you would like to specify multiple filters, then you need to specify the option multiple times. For example:

[mariadb]
...
binlog_ignore_db=db1
binlog_ignore_db=db2

This will tell the primary to do the following:

  • Don't write statements and transactions affecting the database named db1 into the binary log.

  • Don't write statements and transactions affecting the database named db2 into the binary log.

  • Write statements and transactions affecting any other databases into the binary log.

The binlog_ignore_db option is effectively ignored if the binlog_do_db option is set, so those two options should not be set together.

Replication Filters for Replicas

MariaDB provides options and system variables that can be used on used on a replicas to filter events replicated in the binary log.

Replication Filter Options

The following options and system variables are available, and they are evaluated in the order that they are listed below. If there are conflicting settings, the respective replicate_do_ prevails.

replicate_rewrite_db

The replicate_rewrite_db option (and, from MariaDB 10.11, system variable), allows you to configure a replica to rewrite database names. It uses the format primary_database->replica_database. If a replica encounters a binary log event in which the default database (i.e. the one selected by the USE statement) is primary_database, then the replica will apply the event in replica_database instead.

This option will not work with cross-database updates with statement-based logging. See the Statement-Based Logging section for more information.

This option only affects statements that involve tables. This option does not affect statements involving the database itself, such as CREATE DATABASE, ALTER DATABASE, and DROP DATABASE.

This option's rewrites are evaluated before any other replication filters configured by the replicate_* system variables.

Statements that use table names qualified with database names do not work with other replication filters such as replicate_do_table.

Until MariaDB 10.11, this option could not be set dynamically.

When setting it on the command-line or in a server option group in an option file, the option does not accept a comma-separated list. If you would like to specify multiple filters, then you need to specify the option multiple times. For example:

[mariadb]
...
replicate_rewrite_db=db1->db3
replicate_rewrite_db=db2->db4

This will tell the replica to do the following:

  • If a binary log event is encountered in which the default database was db1, then apply the event in db3 instead.

  • If a binary log event is encountered in which the default database was db2, then apply the event in db4 instead.

See Configuring Replication Filter Options with Multi-Source Replication for how to configure this system variable with multi-source replication.

replicate_do_db

The replicate_do_db system variable allows you to configure a replica to apply statements and transactions affecting databases that match a specified name.

This system variable will not work with cross-database updates with statement-based logging or when using mixed-based logging and the statement is logged statement based. For statement-based replication, only the default database (that is, the one selected by USE) is considered, not any explicitly mentioned tables in the query. See the Statement-Based Logging section for more information.

When setting it dynamically with SET GLOBAL, the system variable accepts a comma-separated list of filters.

When setting it dynamically, it is not possible to specify database names that contain commas. If you need to specify database names that contain commas, then you will need to specify them by either providing the command-line options or configuring them in a server option group in an option file when the server is started.

When setting it dynamically, the replica threads must be stopped. For example:

STOP SLAVE;
SET GLOBAL replicate_do_db='db1,db2';
START SLAVE;

When setting it on the command-line or in a server option group in an option file, the system variable does not accept a comma-separated list. If you would like to specify multiple filters, then you need to specify the system variable multiple times. For example:

[mariadb]
...
replicate_do_db=db1
replicate_do_db=db2

This will tell the replica to do the following:

  • Replicate statements and transactions affecting the database named db1.

  • Replicate statements and transactions affecting the database named db2.

  • Ignore statements and transactions affecting any other databases.

See Configuring Replication Filter Options with Multi-Source Replication for how to configure this system variable with multi-source replication.

replicate_ignore_db

The replicate_ignore_db system variable allows you to configure a replica to ignore statements and transactions affecting databases that match a specified name.

This system variable will not work with cross-database updates with statement-based logging or when using mixed-based logging and the statement is logged statement based. For statement-based replication, only the default database (that is, the one selected by USE) is considered, not any explicitly mentioned tables in the query. See the Statement-Based Logging section for more information.

When setting it dynamically with SET GLOBAL, the system variable accepts a comma-separated list of filters.

When setting it dynamically, it is not possible to specify database names that contain commas. If you need to specify names or patterns that contain commas, then you will need to specify them by either providing the command-line options or configuring them in a server option group in an option file when the server is started.

When setting it dynamically, the replica threads must be stopped. For example:

STOP SLAVE;
SET GLOBAL replicate_ignore_db='db1,db2';
START SLAVE;

When setting it on the command-line or in a server option group in an option file, the system variable does not accept a comma-separated list. If you would like to specify multiple filters, then you need to specify the system variable multiple times. For example:

[mariadb]
...
replicate_ignore_db=db1
replicate_ignore_db=db2

This will tell the replica to do the following:

  • Ignore statements and transactions affecting databases named db1.

  • Ignore statements and transactions affecting databases named db2.

  • Replicate statements and transactions affecting any other databases.

The replicate_ignore_db system variable is effectively ignored if the replicate_do_db system variable is set, so those two system variables should not be set together.

See Configuring Replication Filter Options with Multi-Source Replication for how to configure this system variable with multi-source replication.

replicate_do_table

The replicate_do_table system variable allows you to configure a replica to apply statements and transactions that affect tables that match a specified name. The table name is specified in the format: dbname.tablename.

This system variable will not work with cross-database updates with statement-based logging. See the Statement-Based Logging section for more information.

This option only affects statements that involve tables. This option does not affect statements involving the database itself, such as CREATE DATABASE, ALTER DATABASE, and DROP DATABASE.

When setting it dynamically with SET GLOBAL, the system variable accepts a comma-separated list of filters.

When setting it dynamically, it is not possible to specify database or table names or patterns that contain commas. If you need to specify database or table names that contain commas, then you will need to specify them by either providing the command-line options or configuring them in a server option group in an option file when the server is started.

When setting it dynamically, the replica threads must be stopped. For example:

STOP SLAVE;
SET GLOBAL replicate_do_table='db1.tab,db2.tab';
START SLAVE;

When setting it on the command-line or in a server option group in an option file, the system variable does not accept a comma-separated list. If you would like to specify multiple filters, then you need to specify the system variable multiple times. For example:

[mariadb]
...
replicate_do_table=db1.tab
replicate_do_table=db2.tab

This will tell the replica to do the following:

  • Replicate statements and transactions affecting tables in databases named db1 and which are named tab.

  • Replicate statements and transactions affecting tables in databases named db2 and which are named tab.

  • Ignore statements and transactions affecting any other tables.

See Configuring Replication Filter Options with Multi-Source Replication for how to configure this system variable with multi-source replication.

replicate_ignore_table

The replicate_ignore_table system variable allows you to configure a replica to ignore statements and transactions that affect tables that match a specified name. The table name is specified in the format: dbname.tablename.

This system variable will not work with cross-database updates with statement-based logging. See the Statement-Based Logging section for more information.

When setting it dynamically with SET GLOBAL, the system variable accepts a comma-separated list of filters.

When setting it dynamically, it is not possible to specify database or table names that contain commas. If you need to specify database or table names that contain commas, then you will need to specify them by either providing the command-line options or configuring them in a server option group in an option file when the server is started.

When setting it dynamically, the replica threads must be stopped. For example:

STOP SLAVE;
SET GLOBAL replicate_ignore_table='db1.tab,db2.tab';
START SLAVE;

When setting it on the command-line or in a server option group in an option file, the system variable does not accept a comma-separated list. If you would like to specify multiple filters, then you need to specify the system variable multiple times. For example:

[mariadb]
...
replicate_ignore_table=db1.tab
replicate_ignore_table=db2.tab

This will tell the replica to do the following:

  • Ignore statements and transactions affecting tables in databases named db1 and which are named tab.

  • Ignore statements and transactions affecting tables in databases named db2 and which are named tab.

  • Replicate statements and transactions affecting any other tables.

The replicate_ignore_table system variable is effectively ignored if either the replicate_do_table system variable or the replicate_wild_do_table system variable is set, so the replicate_ignore_table system variable should not be used with those two system variables.

See Configuring Replication Filter Options with Multi-Source Replication for how to configure this system variable with multi-source replication.

replicate_wild_do_table

The replicate_wild_do_table system variable allows you to configure a replica to apply statements and transactions that affect tables that match a specified wildcard pattern.

The wildcard pattern uses the same semantics as the LIKE operator. This means that the following characters have a special meaning:

  • _ - The _ character matches any single character.

  • % - The % character matches zero or more characters.

  • \ - The \ character is used to escape the other special characters in cases where you need the literal character.

This system variable will work with cross-database updates with statement-based logging. See the Statement-Based Logging section for more information.

The system variable does filter databases, tables, views and triggers.

The system variable does not filter stored procedures, stored functions, and events. The replicate_do_db system variable will need to be used to filter those.

If the table name pattern for a filter is just specified as %, then all tables in the database will be matched. In this case, the filter will also affect certain database-level statements, such as CREATE DATABASE, ALTER DATABASE and DROP DATABASE.

When setting it dynamically with SET GLOBAL, the system variable accepts a comma-separated list of filters.

When setting it dynamically, it is not possible to specify database or table names or patterns that contain commas. If you need to specify database or table names or patterns that contain commas, then you will need to specify them by either providing the command-line options or configuring them in a server option group in an option file when the server is started.

When setting it dynamically, the replica threads must be stopped. For example:

STOP SLAVE;
SET GLOBAL replicate_wild_do_table='db%.tab%,app1.%';
START SLAVE;

When setting it on the command-line or in a server option group in an option file, the system variable does not accept a comma-separated list. If you would like to specify multiple filters, then you need to specify the system variable multiple times. For example:

[mariadb]
...
replicate_wild_do_table=db%.tab%
replicate_wild_do_table=app1.%

This will tell the replica to do the following:

  • Replicate statements and transactions affecting tables in databases that start with db and whose table names start with tab.

  • Replicate statements and transactions affecting the database named app1.

  • Ignore statements and transactions affecting any other tables and databases.

See Configuring Replication Filter Options with Multi-Source Replication for how to configure this system variable with multi-source replication.

replicate_wild_ignore_table

The replicate_wild_ignore_table system variable allows you to configure a replica to ignore statements and transactions that affect tables that match a specified wildcard pattern.

The wildcard pattern uses the same semantics as the LIKE operator. This means that the following characters have a special meaning:

  • _ - The _ character matches any single character.

  • % - The % character matches zero or more characters.

  • \ - The \ character is used to escape the other special characters in cases where you need the literal character.

This system variable will work with cross-database updates with statement-based logging. See the Statement-Based Logging section for more information.

The system variable does filter databases, tables, views and triggers.

The system variable does not filter stored procedures, stored functions, and events. The replicate_ignore_db system variable will need to be used to filter those.

If the table name pattern for a filter is just specified as %, then all tables in the database will be matched. In this case, the filter will also affect certain database-level statements, such as CREATE DATABASE, ALTER DATABASE and DROP DATABASE.

When setting it dynamically with SET GLOBAL, the system variable accepts a comma-separated list of filters.

When setting it dynamically, it is not possible to specify database or table names or patterns that contain commas. If you need to specify database or table names or patterns that contain commas, then you will need to specify them by either providing the command-line options or configuring them in a server option group in an option file when the server is started.

When setting it dynamically, the replica threads must be stopped. For example:

STOP SLAVE;
SET GLOBAL replicate_wild_ignore_table='db%.tab%,app1.%';
START SLAVE;

When setting it on the command-line or in a server option group in an option file, the system variable does not accept a comma-separated list. If you would like to specify multiple filters, then you need to specify the system variable multiple times. For example:

[mariadb]
...
replicate_wild_ignore_table=db%.tab%
replicate_wild_ignore_table=app1.%

This will tell the replica to do the following:

  • Ignore statements and transactions affecting tables in databases that start with db and whose table names start with tab.

  • Ignore statements and transactions affecting all the tables in the database named app1.

  • Replicate statements and transactions affecting any other tables and databases.

The replicate_ignore_table system variable is effectively ignored if either the replicate_do_table system variable or the replicate_wild_do_table system variable is set, so the replicate_ignore_table system variable should not be used with those two system variables.

See Configuring Replication Filter Options with Multi-Source Replication for how to configure this system variable with multi-source replication.

Configuring Replication Filter Options with Multi-Source Replication

How you configure replication filters with multi-source replication depends on whether you are configuring them dynamically or whether you are configuring them in a server option group in an option file.

Setting Replication Filter Options Dynamically with Multi-Source Replication

The usage of dynamic replication filters changes somewhat when multi-source replication is in use. By default, the variables are addressed to the default connection, so in a multi-source environment, the required connection needs to be specified. There are two ways to do this.

Prefixing the Replication Filter Option with the Connection Name

One way to change a replication filter for a multi-source connection is to explicitly specify the name when changing the filter. For example:

STOP SLAVE 'gandalf';
SET GLOBAL gandalf.replicate_do_table='database1.table1,database1.table2,database1.table3';
START SLAVE 'gandalf';

Changing the Default Connection

Alternatively, the default connection can be changed by setting the default_master_connection system variable, and then the replication filter can be changed in the usual fashion. For example:

SET default_master_connection = 'gandalf';
STOP SLAVE; 
SET GLOBAL replicate_do_table='database1.table1,database1.table2,database1.table3';
START SLAVE;

Setting Replication Filter Options in Option Files with Multi-Source Replication

If you are using multi-source replication and if you would like to make this filter persist server restarts by adding it to a server option group in an option file, then the option file can also include the connection name that each filter would apply to. For example:

[mariadb]
...
gandalf.replicate_do_db=database1
saruman.replicate_do_db=database2

CHANGE MASTER Options

The CHANGE MASTER statement has a few options that can be used to filter certain types of binary log events.

IGNORE_SERVER_IDS

The IGNORE_SERVER_IDS option for CHANGE MASTER can be used to configure a replica to ignore binary log events that originated from certain servers. Filtered binary log events will not get logged to the replica’s relay log, and they will not be applied by the replica.

DO_DOMAIN_IDS

The DO_DOMAIN_IDS option for CHANGE MASTER can be used to configure a replica to only apply binary log events if the transaction's GTID is in a specific gtid_domain_id value. Filtered binary log events will not get logged to the replica’s relay log, and they will not be applied by the replica.

IGNORE_DOMAIN_IDS

The IGNORE_DOMAIN_IDS option for CHANGE MASTER can be used to configure a replica to ignore binary log events if the transaction's GTID is in a specific gtid_domain_id value. Filtered binary log events will not get logged to the replica’s relay log, and they will not be applied by the replica.

Replication Filters and Binary Log Formats

The way that a replication filter is interpreted can depend on the binary log format.

Statement-Based Logging

When an event is logged in its statement-based format, many replication filters that affect a database will test the filter against the default database (i.e. the one selected by the USE statement). This applies to the following replication filters:

  • binlog_do_db

  • binlog_ignore_db

  • replicate_rewrite_db

  • replicate_do_db

  • replicate_ignore_db

When an event is logged in its statement-based format, many replication filters that affect a table will test the filter against the table in the default database (i.e. the one selected by the USE statement). This applies to the following replication filters:

  • replicate_do_table

  • replicate_ignore_table

This means that cross-database updates not work with replication filters and statement-based binary logging. For example, if replicate_do_table=db2.tab were set, then the following would not replicate with statement-based binary logging:

USE db1;
INSERT INTO db2.tab VALUES (1);

If you need to be able to support cross-database updates with replication filters and statement-based binary logging, then you should use the following replication filters:

  • replicate_wild_do_table

  • replicate_wild_ignore_table

Row-Based Logging

When an event is logged in its row-based format, many replication filters that affect a database will test the filter against the database that is actually affected by the event.

Similarly, when an event is logged in its row-based format, many replication filters that affect a table will test the filter against the table in the database that is actually affected by the event.

This means that cross-database updates work with replication filters and statement-based binary logging.

Keep in mind that DDL statements are always logged to the binary log in statement-based format, even when the binlog_format system variable is set to ROW. This means that the notes mentioned in Statement-Based Logging always apply to DDL.

Replication Filters and Galera Cluster

When using Galera cluster, replication filters should be used with caution. See Configuring MariaDB Galera Cluster: Replication Filters for more details.

See Also

  • Dynamic replication filters — our wheel will be square!

This page is licensed: CC BY-SA / Gnu FDL

Replication Threads

Understand replication threads in MariaDB Server. This section explains the I/O and SQL threads, their roles in the replication process, and how they impact performance and consistency.

The terms master and slave have historically been used in replication, and MariaDB has begun the process of adding primary and replica synonyms. The old terms will continue to be used to maintain backward compatibility - see MDEV-18777 to follow progress on this effort.

MariaDB's replication implementation requires several types of threads.

Threads on the Primary

The primary usually only has one type of replication-related thread: the binary log dump thread.

If semisynchronous replication is enabled, then the primary also has an ACK receiver thread.

Binary Log Dump Thread

The binary log dump thread runs on the primary and dumps the binary log to the replica. This thread can be identified by running the SHOW PROCESSLIST statement and finding the thread where the thread command is "Binlog Dump".

The primary creates a separate binary log dump thread for each replica connected to the primary. You can identify which replicas are connected to the primary by executing the SHOW SLAVE HOSTS statement.

Binary Log Dump Threads and the Shutdown Process

When a primary server is shutdown and it goes through the normal shutdown process, the primary kills client threads in random order. By default, the primary also considers its binary log dump threads to be regular client threads. As a consequence, the binary log dump threads can be killed while client threads still exist, and this means that data can be written on the primary during a normal shutdown that won't be replicated. This is true even if semi-synchronous replication is being used. Data is not lost, it is stored in the primary server's binary log. The replicas on reconnection, after the primary server restarts, will resume at the exact position they where killed off during the primary shutdown. No data is lost.

In MariaDB 10.4 and later, this problem can be solved by shutting down the server using either the mariadb-admin utility or the SHUTDOWN command, and providing a special option.

For example, this problem can be solved by shutting down the server with the mariadb-admin utility and by providing the --wait-for-all-slaves option to the utility and by executing the shutdown command with the utility:

mariadb-admin --wait-for-all-slaves shutdown

Or this problem can be solved by shutting down the server with the SHUTDOWN command and by providing the WAIT FOR ALL SLAVES option to the command:

SHUTDOWN WAIT FOR ALL SLAVES;

When one of these special options is provided, the server only kills its binary log dump threads after all client threads have been killed, and it only completes the shutdown after the last binary log has been sent to all connected replicas.

In MariaDB 10.4 and later, it is still not possible to enable this behavior by default. This means that this behavior is currently inaccessible when shutting down the server using tools like systemd or sysVinit.

In MariaDB 10.3 and before, it is recommended to manually switchover replicas to a new primary before shutting down the old primary.

ACK Receiver Thread

When semisynchronous replication is enabled, semisynchronous replicas send acknowledgements (ACKs) to their primary to confirm that they have received some transaction. The primary creates an ACK receiver thread to receive these ACKs.

Threads on the Replica

The replica has three types of replication-related threads: the replica I/O thread, the replica SQL thread, and worker threads, which are only applicable when parallel replication is in use.

When multi-source replication is in use, each independent replication connection has its own replica threads of each type.

Replica I/O Thread

The replica's I/O thread receives the binary log events from the primary and writes them to its relay log.

Binary Log Position

The binary log position of the replica's I/O thread can be checked by executing the SHOW SLAVE STATUS statement. It will be shown as the Master_Log_File and Read_Master_Log_Pos columns.

The binary log position of the replica's I/O thread can be set by setting the MASTER_LOG_FILE and MASTER_LOG_POS options with the CHANGE MASTER statement.

The binary log position of the replica's I/O thread and the values of most other CHANGE MASTER options are written to either the default master.info file or the file that is configured by the master_info_file option. The replica's I/O thread keeps this binary log position updated as it downloads events only when the MASTER_USE_GTID option is set to NO. Otherwise the file is not updated on a per event basis. See CHANGE MASTER TO: Option Persistence for more information.

Replica SQL Thread

The replica's SQL thread reads events from the relay log. What it does with them depends on whether parallel replication is in use. If parallel replication is not in use, then the SQL thread applies the events to its local copy of the data. If parallel replication is in use, then the SQL thread hands off the events to its worker threads to apply in parallel.

Relay Log Position

The relay log position of the replica's SQL thread can be checked by executing the SHOW SLAVE STATUS statement. It will be shown as the Relay_Log_File and Relay_Log_Pos columns.

The relay log position of the replica's SQL thread can be set by setting the RELAY_LOG_FILE and RELAY_LOG_POS options with the CHANGE MASTER statement.

The relay log position of the replica's SQL thread is written to either the default relay-log.info file or the file that is configured by the relay_log_info_file system variable. The replica's SQL thread keeps this relay log position updated as it applies events. See CHANGE MASTER TO: Option Persistence for more information.

Binary Log Position

The corresponding binary log position of the current relay log position of the replica's SQL thread can be checked by executing the SHOW SLAVE STATUS statement. It will be shown as the Relay_Master_Log_File and Exec_Master_Log_Pos columns.

GTID Position

If the replica is replicating binary log events that contain GTIDs, then the replica's's SQL thread will write every GTID that it applies to the mysql.gtid_slave_pos table. This GTID can be inspected and modified through the gtid_slave_pos system variable.

If the replica has the log_slave_updates system variable enabled and if the replica has the binary log enabled, then every write by the replica's SQL thread will also go into the replica's binary log. This means that GTIDs of replicated transactions would be reflected in the value of the gtid_binlog_pos system variable.

See CHANGE MASTER TO: GTID Persistence for more information.

Worker Threads

When parallel replication is in use, then the SQL thread hands off the events to its worker threads to apply in parallel.

This page is licensed: CC BY-SA / Gnu FDL

Replication and Foreign Keys

Understand how foreign keys interact with replication, detailing the implications of foreign key constraints on data consistency & provides best practices for managing them in replicated environments.

The terms master and slave have historically been used in replication, and MariaDB has begun the process of adding primary and replica synonyms. The old terms will continue to be used to maintain backward compatibility - see MDEV-18777 to follow progress on this effort.

Replication is based upon the binary log. However, cascading deletes or updates based on foreign key relations are an internal mechanism, and are not written to the binary log.

Because of this, an identical statement run on the master and the slave may result in different outcomes if the foreign key relations are not identical on both master and slave This could be the case if the storage engine on one supports cascading deletes (e.g. InnoDB) and the storage engine on the other does not (e.g. MyISAM), or the one has specified a foreign key relation, and the other hasn't.

Take the following example:

CREATE TABLE employees (
    x INT PRIMARY KEY,
    name VARCHAR(10)
) ENGINE = InnoDB;

CREATE TABLE children (
    y INT PRIMARY KEY,
    f INT,
    name VARCHAR(10),
    FOREIGN KEY fk (f) REFERENCES employees (x)
        ON DELETE CASCADE
) ENGINE = InnoDB;

The slave, however, has been set up without InnoDB support, and defaults to MyISAM, so the foreign key restrictions are not in place.

INSERT INTO employees VALUES (1, 'Yaser'), (2, 'Prune');

INSERT INTO children VALUES (1, 1, 'Haruna'), (2, 1, 'Hera'), (3, 2, 'Eva');

At this point, the slave and the master are in sync:

SELECT * FROM employees;
+---+-------+
| x | name  |
+---+-------+
| 1 | Yaser |
| 2 | Prune |
+---+-------+
2 rows in set (0.00 sec)

SELECT * FROM children;
+---+------+--------+
| y | f    | name   |
+---+------+--------+
| 1 |    1 | Haruna |
| 2 |    1 | Hera   |
| 3 |    2 | Eva    |
+---+------+--------+

However, after:

DELETE FROM employees WHERE x=1;

there are different outcomes on the slave and the master.

On the master, the cascading deletes have taken effect:

SELECT * FROM children;
+---+------+------+
| y | f    | name |
+---+------+------+
| 3 |    2 | Eva  |
+---+------+------+

On the slave, the cascading deletes did not take effect:

SELECT * FROM children;
+---+------+--------+
| y | f    | name   |
+---+------+--------+
| 1 |    1 | Haruna |
| 2 |    1 | Hera   |
| 3 |    2 | Eva    |
+---+------+--------+

This page is licensed: CC BY-SA / Gnu FDL

Replication and Binary Log System Variables

Explore replication and binary log system variables in MariaDB Server. This section details essential configuration parameters for managing replication behavior & binary logging for data consistency.

The terms master and slave have historically been used in replication, and MariaDB has begun the process of adding primary and replica synonyms. The old terms will continue to be used to maintain backward compatibility - see MDEV-18777 to follow progress on this effort.

This page lists system variables that are related to binary logging and replication.

See Server System Variables for a complete list of system variables and instructions on setting them, as well as System variables for global transaction ID.

Also see mariadbd replication options for related options that are not system variables (such as binlog_do_db and binlog_ignore_db).

See also the Full list of MariaDB options, system and status variables.

auto_increment_increment

  • Description: The increment for all AUTO_INCREMENT values on the server, by default 1. Intended for use in primary-to-primary replication.

  • Commandline: --auto-increment-increment[=#]

  • Scope: Global, Session

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 1

  • Range: 1 to 65535

auto_increment_offset

  • Description: The offset for all AUTO_INCREMENT values on the server, by default 1. Intended for use in primary-to-primary replication. Should be not be larger than auto_increment_increment. See AUTO_INCREMENT#Replication.

  • Commandline: --auto-increment-offset[=#]

  • Scope: Global, Session

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 1

  • Range: 1 to 65535

binlog_alter_two_phase

  • Description: When set, split ALTER at binary logging into two statements: START ALTER and COMMIT/ROLLBACK ALTER. The ON setting is recommended for long-running ALTER-table so it could start on replica before its actual execution on primary.

  • Commandline: --binlog-alter-two-phase[={0|1}]

  • Scope: Global, Session

  • Dynamic: Yes

  • Data Type: boolean

  • Default Value: OFF

  • Introduced: MariaDB 10.8.1

binlog_annotate_row_events

  • Description: This option tells the primary to write annotate_rows_events to the binary log.

  • Commandline: --binlog-annotate-row-events[={0|1}]

  • Scope: Global, Session

  • Dynamic: Yes

  • Data Type: boolean

  • Default Value:

    • ON (>= MariaDB 10.2.4)

    • OFF (<= MariaDB 10.2.3)

binlog_cache_size

  • Description: If the binary log is active, this variable determines the size in bytes, per-connection, of the cache holding a record of binary log changes during a transaction. A separate variable, binlog_stmt_cache_size, sets the upper limit for the statement cache. The binlog_cache_disk_use and binlog_cache_use server status variables will indicate whether this variable needs to be increased (you want a low ratio of binlog_cache_disk_use to binlog_cache_use).

  • Commandline: --binlog-cache-size=#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 32768

  • Range - 32 bit: 4096 to 4294967295

  • Range - 64 bit: 4096 to 18446744073709547520

binlog_checksum

  • Description: Specifies the type of BINLOG_CHECKSUM_ALG for log events in the binary log.

  • Commandline:

    • --binlog-checksum=name

    • --binlog-checksum=[0|1]

  • Scope: Global

  • Dynamic: Yes

  • Data Type: string

  • Default Value:

    • CRC32 (>= MariaDB 10.2.1)

    • NONE (<= MariaDB 10.2.0)

  • Valid Values: NONE (0), CRC32 (1)

binlog_commit_wait_count

  • Description: Configures the behavior of group commit for the binary log, which can help increase transaction throughput and is used to enable conservative mode of in-order parallel replication. With group commit for the binary log, the server can delay flushing a committed transaction into binary log until the given number of transactions are ready to be flushed as a group. The delay will however not be longer than the value set by binlog_commit_wait_usec. The default value of 0 means that no delay is introduced. Setting this value can reduce I/O on the binary log and give an increased opportunity for parallel apply on the replica when conservative mode of in-order parallel replication is enabled, but too high a value will decrease the transaction throughput. By monitoring the status variable binlog_group_commit_trigger_count (>=MariaDB 10.1.5) it is possible to see how often this is occurring.

  • Starting with MariaDB 10.0.18 and MariaDB 10.1.4: If the server detects that one of the committing transactions T1 holds an InnoDB row lock that another transaction T2 is waiting for, then the commit will complete immediately without further delay. This helps avoid losing throughput when many transactions need conflicting locks. This often makes it safe to use this option without losing throughput on a replica with conservative mode of in-order parallel replication, provided the value ofslave_parallel_threads is sufficiently high.

  • Commandline: --binlog-commit-wait-count=#]

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 0

  • Range: 0 to 18446744073709551615

binlog_commit_wait_usec

  • Description: Configures the behavior of group commit for the binary log, which can help increase transaction throughput and is used to enable conservative mode of in-order parallel replication. With group commit for the binary log, the server can delay flushing a committed transaction into binary log until the transaction has waited the configured number of microseconds. By monitoring the status variable binlog_group_commit_trigger_timeout (>=MariaDB 10.1.5) it is possible to see how often group commits are made due to binlog_commit_wait_usec. As soon as the number of pending commits reaches binlog_commit_wait_count, the wait will be terminated, though. Thus, this setting only takes effect if binlog_commit_wait_count is non-zero.

  • Commandline: --binlog-commit-wait-usec#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 100000

  • Range: 0 to 18446744073709551615

binlog_direct_non_transactional_updates

  • Description: Replication inconsistencies can occur due when a transaction updates both transactional and non-transactional tables and the updates to the non-transactional tables are visible before being written to the binary log. This is because, to preserve causality, the non-transactional statements are written to the transaction cache, which is only flushed on commit. Setting binlog_direct_non_transactional_updates to 1 (0 is default) will cause non-transactional tables to be written straight to the binary log, rather than the transaction cache. This setting has no effect when row-based binary logging is used, as it requires statement-based logging. See binlog_format. Use with care, and only in situations where no dependencies exist between the non-transactional and transactional tables, for example INSERTing into a non-transactional table based upon the results of a SELECT from a transactional table.

  • Commandline: --binlog-direct-non-transactional-updates[=value]

  • Scope: Global, Session

  • Dynamic: Yes

  • Data Type: boolean

  • Default Value: OFF (0)

binlog_do_db

  • Description: This option allows you to configure a replication primary to write statements and transactions affecting databases that match a specified name into its binary log. Since the filtered statements or transactions will not be present in the binary log, its replicas will not be able to replicate them.

    • This option will not work with cross-database updates with statement-based logging. See the Statement-Based Logging section for more information.

    • Until MariaDB 11.2.0, only available as an option, not a system variable. This option can not be set dynamically.

    • When setting it on the command-line or in a server option group in an option file, the option does not accept a comma-separated list. If you would like to specify multiple filters, then you need to specify the option multiple times.

    • See Replication Filters for more information.

  • Commandline: --binlog-do-db=#

  • Scope: Global

  • Dynamic: No

  • Data Type: string

  • Default Value: NULL

  • Introduced: MariaDB 11.2.0 (as a system variable)

binlog_expire_logs_seconds

  • Description: If non-zero, binary logs will be purged after binlog_expire_logs_seconds seconds. Possible purges happen at startup and at binary log rotation. From MariaDB 10.6.1, binlog_expire_logs_seconds and expire_logs_days are forms of aliases, such that changes to one automatically reflect in the other.

  • Commandline: --binlog-expire-logs-seconds=#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 0

  • Range: 0 to 8553600

  • Introduced: MariaDB 10.6.1

binlog_file_cache_size

  • Description: Size of in-memory cache that is allocated when reading binary log and relay log files.

  • Commandline: --binlog-file-cache-size=#

  • Scope: Global, Session

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 16384

  • Range: 8192 to 18446744073709551615

  • Introduced: MariaDB 10.3.3

binlog_format

  • Description: Determines whether replication is row-based, statement-based or mixed. Statement-based was the default until MariaDB 10.2.3. Be careful of changing the binary log format when a replication environment is already running. See Binary Log Formats. Starting from MariaDB 10.0.22 a replica will apply any events it gets from the primary, regardless of the binary log format. binlog_format only applies to normal (not replicated) updates.

  • Commandline: --binlog-format=format

  • Scope: Global, Session

  • Dynamic: Yes

  • Data Type: enumeration

  • Default Value:

    • MIXED (>= MariaDB 10.2.4)

    • STATEMENT (<= MariaDB 10.2.3)

  • Valid Values: ROW, STATEMENT or MIXED

binlog_gtid_index

  • Description: Enable the creation of a GTID index for every binlog file, and the use of such index for speeding up GTID lookup in the binlog. See Binlog indexing.

  • Commandline: --binlog-gtid-index{=0|1}

  • Scope: Global

  • Dynamic: Yes

  • Data Type: boolean

  • Default Value: ON

  • Introduced: MariaDB 11.4

binlog_gtid_index_page_size

  • Description: Page size to use for the binlog GTID index. See Binlog indexing.

  • Commandline: --binlog-gtid-index-page-size=#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 4096

  • Range: 64 to 16777216

  • Introduced: MariaDB 11.4

binlog_gtid_index_span_min

  • Description: Control sparseness of the binlog GTID index. If set to N, at most one index record will be added for every N bytes of binlog file written, to reduce the size of the index. Normally does not need tuning. See Binlog indexing.

  • Commandline: --binlog-gtid-index-span-min=#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 65536

  • Range: 1 to 1073741824

  • Introduced: MariaDB 11.4

binlog_ignore_db

  • Description: This option allows you to configure a replication primary to not write statements and transactions affecting databases that match a specified name into its binary log. Since the filtered statements or transactions will not be present in the binary log, its replicas will not be able to replicate them.

    • This option will not work with cross-database updates with statement-based logging. See the Statement-Based Logging section for more information.

    • Until MariaDB 11.2.0, only available as an option, not a system variable. This option can not be set dynamically.

    • When setting it on the command-line or in a server option group in an option file, the option does not accept a comma-separated list. If you would like to specify multiple filters, then you need to specify the option multiple times.

    • See Replication Filters for more information.

  • Commandline: --binlog-ignore-db=name

  • Scope: Global

  • Dynamic: No

  • Data Type: string

  • Default Value: NULL

  • Introduced: MariaDB 11.2.0

binlog_large_commit_threshold

  • Description: Increases transaction concurrency for large transactions (i.e. those with sizes larger than this value) by using the large transaction's cache file as a new binary log, and rotating the active binary log to the large transaction's cache file at commit time. This avoids the default commit logic that copies the transaction cache data to the end of the active binary log file while holding a lock that prevents other transactions from binlogging.

  • Commandline: --binlog-large-commit-threshold=val

  • Scope: Global

  • Dynamic: Yes

  • Data Type: bigint unsigned

  • Default Value: 134217728

  • Range: 10485760 to 18446744073709551615

  • Introduced: MariaDB 11.7

binlog_legacy_event_pos

  • Description: Fill in the end_log_pos field of all events in the binlog, even when doing so costs performance. Can be used in case some old application needs it for backwards compatibility. Setting this option can hurt binlog scalability.

  • Commandline: --binlog-legacy-event-pos{=0|1}

  • Scope: Global

  • Dynamic: Yes

  • Data Type: boolean

  • Default Value: OFF

  • Introduced: MariaDB 11.4

binlog_optimize_thread_scheduling

  • Description: Run fast part of group commit in a single thread, to optimize kernel thread scheduling. On by default. Disable to run each transaction in group commit in its own thread, which can be slower at very high concurrency. This option is mostly for testing one algorithm versus another, and it should not normally be necessary to change it. Deprecated in MariaDB 11.7, as the option was initially added to provide a safe alternative for the newly added binlog group commit logic, such that when 0, it would disable a leader thread from performing the binlog write for all transactions that are a part of the group commit. Problems related to the binlog group commit optimization are expected to be addressed by now, so the option has been deprecated and will be removed in future.

  • Commandline: --binlog-optimize-thread-scheduling or --skip-binlog-optimize-thread-scheduling

  • Scope: Global

  • Dynamic: No

  • Data Type: boolean

  • Default Value: ON

  • Deprecated: MariaDB 11.7

binlog_row_event_max_size

  • Description: The maximum size of a row-based binary log event in bytes. Rows will be grouped into events smaller than this size if possible. The value has to be a multiple of 256. Until MariaDB 11.2.0, only available as an option, not a system variable.

  • Commandline: --binlog-row-event-max-size=val

  • Scope: Global, Session

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 8192

  • Range: 256 to 4294967040 (in multiples of 256)

  • Introduced: MariaDB 11.2.0

binlog_row_image

  • Description: Controls the logging format in row-based replication. In row-based replication (the variable has no effect with statement-based replication), each row change event contains an image for matching against when choosing the row to be updated, and another image containing the changes. Before the introduction of this variable, all columns were logged for both of these images. In certain circumstances, this is not necessary, and memory, disk and network resources can be saved by partial logging. Note that to safely change this setting from the default, the table being replicated to must contain identical primary key definitions, and columns must be present, in the same order, and use the same data types as the original table. If these conditions are not met, matches may not be correctly determined and updates and deletes may diverge on the replica, with no warnings or errors returned.

    • FULL: All columns in the before and after image are logged. This is the default, and the only behavior in earlier versions.

    • NOBLOB: mariadbd avoids logging blob and text columns whenever possible (eg, blob column was not changed or is not part of primary key).

    • MINIMAL: A PK equivalent (PK columns or full row if there is no PK in the table) is logged in the before image, and only changed columns are logged in the after image.

    • FULL_NODUP: All columns are logged in the before image, but only changed columns or all columns of inserted record are logged in the after image. This is essentially the same as FULL, but takes less space. From MariaDB 11.4.

  • Commandline: --binlog-row-image=value

  • Scope: Global, Session

  • Dynamic: Yes

  • Data Type: enum

  • Default Value: FULL

  • Valid Values:

    • <= MariaDB 11.3: FULL, NOBLOB or MINIMAL

    • >=MariaDB 11.4: FULL, NOBLOB, MINIMAL or FULL_NODUP

binlog_row_metadata

  • Description: Controls the format used for binlog metadata logging.

    • NO_LOG: No metadata is logged (default).

    • MINIMAL: Only metadata required by a replica is logged.

    • FULL: All metadata is logged.

  • Commandline: --binlog-row-metadata=value

  • Scope: Global

  • Dynamic: Yes

  • Data Type: enum

  • Default Value: NO_LOG

  • Valid Values: NO_LOG, MINIMAL, FULL

  • Introduced: MariaDB 10.5.0

binlog_space_limit

  • Description: Alias for max_binlog_total_size.

  • Introduced: MariaDB 11.4

binlog_stmt_cache_size

  • Description: If the binary log is active, this variable determines the size in bytes of the cache holding a record of binary log changes outside of a transaction. The variable binlog_cache_size, determines the cache size for binary log statements inside a transaction. The binlog_stmt_cache_disk_use and binlog_stmt_cache_use server status variables will indicate whether this variable needs to be increased (you want a low ratio of binlog_stmt_cache_disk_use to binlog_stmt_cache_use).

  • Commandline: --binlog-stmt-cache-size=#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 32768

  • Range - 32 bit: 4096 to 4294967295

  • Range - 64 bit: 4096 to 18446744073709547520

default_master_connection

  • Description: In multi-source replication, specifies which connection will be used for commands and variables if you don't specify a connection.

  • Commandline: None

  • Scope: Session

  • Dynamic: Yes

  • Data Type: string

  • Default Value: '' (empty string)

encrypt_binlog

  • Description: Encrypt binary logs (including relay logs). See Data at Rest Encryption and Encrypting Binary Logs.

  • Commandline: --encrypt-binlog[={0|1}]

  • Scope: Global

  • Dynamic: No

  • Data Type: boolean

  • Default Value: OFF

expire_logs_days

  • Description: Number of days after which the binary log can be automatically removed. By default 0, or no automatic removal. When using replication, should always be set higher than the maximum lag by any replica. Removals take place when the server starts up, when the binary log is flushed, when the next binary log is created after the previous one reaches the maximum size, or when running PURGE BINARY LOGS. Units are whole days (integer) until MariaDB 10.6.0, or 1/1000000 precision (double) from MariaDB 10.6.1.Starting from MariaDB 10.6.1, expire_logs_days and binlog_expire_logs_seconds are forms of aliases, such that changes to one automatically reflect in the other.

  • Commandline: --expire-logs-days=#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 0.000000 (>= MariaDB 10.6.1), 0 (<= MariaDB 10.6.0)

  • Range: 0 to 99

init_slave

  • Description: Similar to init_connect, but the string contains one or more SQL statements, separated by semicolons, that will be executed by a replica server each time the SQL thread starts. These statements are only executed after the acknowledgement is sent to the replica and START SLAVE completes.

  • Commandline: --init-slave=name

  • Scope: Global

  • Dynamic: Yes

  • Data Type: string

  • Related variables: init_connect

log_bin

  • Description: Whether binary logging is enabled or not. If the --log-bin option is used, log_bin will be set to ON, otherwise it will be OFF. If no name option is given for --log-bin, datadir/'log-basename'-bin or 'datadir'/mysql-bin will be used (the latter if --log-basename is not specified). We strongly recommend you use either --log-basename or specify a filename to ensure that replication doesn't stop if the real hostname of the computer changes. The name option can optionally include an absolute path. If no path is specified, the log will be written to the data directory. The name can optionally include the file extension; it will be stripped and only the file basename will be used.

  • Commandline: --log-bin[=name]

  • Scope: Global

  • Dynamic: No

  • Data Type: boolean

  • Default Value: OFF

  • Related variables: sql_log_bin

log_bin_basename

  • Description: The full path of the binary log file names, excluding the extension. Its value is derived from the rules specified in log_bin system variable. This is a read-only variable only, there is no corresponding configuration file setting or command line option by the same name, use log_bin to set the basename path instead.

  • Commandline: No commandline option

  • Scope: Global

  • Dynamic: No

  • Data Type: string

  • Default Value: None

  • Dynamic: No

log_bin_compress

  • Description: Whether or not the binary log can be compressed. 0 (the default) means no compression. See Compressing Events to Reduce Size of the Binary Log.

  • Commandline: --log-bin-compress

  • Scope: Global

  • Dynamic: Yes

  • Data Type: boolean

  • Default Value: OFF

log_bin_compress_min_len

  • Description: Minimum length of sql statement (in statement mode) or record (in row mode) that can be compressed. See Compressing Events to Reduce Size of the Binary Log.

  • Commandline: --log-bin-compress-min-len

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 256

  • Range: 10 to 1024

log_bin_index

  • Description: File that holds the names for last binlog files. If --log-basename is also set, log_bin_index should be placed after in the config files. Later settings override earlier settings, so log-basename will override any earlier log file name settings.

  • Commandline: --log-bin-index=name

  • Scope: Global

  • Dynamic: No

  • Data Type: string

  • Default Value: None

log_bin_trust_function_creators

  • Description: Functions and triggers can be dangerous when used with replication. Certain types of functions and triggers may have unintended consequences when the statements are applied on a replica. For that reason, there are some restrictions on the creation of functions and triggers when the binary log is enabled by default, such as:

    • When log_bin_trust_function_creators is OFF and log_bin is ON, CREATE FUNCTION and ALTER FUNCTION statements will trigger an error if the function is defined with any of the NOT DETERMINISTIC, CONTAINS SQL or MODIFIES SQL DATA characteristics.

    • This means that when log_bin_trust_function_creators is OFF and log_bin is ON, CREATE FUNCTION and ALTER FUNCTION statements will only succeed if the function is defined with any of the DETERMINISTIC, NO SQL, or READS SQL DATA characteristics.

    • When log_bin_trust_function_creators is OFF and log_bin is ON, the SUPER privilege is also required to execute the following statements:

      • CREATE FUNCTION

      • CREATE TRIGGER

      • DROP TRIGGER

    • Setting log_bin_trust_function_creators to ON removes these requirements around functions characteristics and the SUPER privileges.

    • See Binary Logging of Stored Routines for more information.

  • Commandline: --log-bin-trust-function-creators[={0|1}]

  • Scope: Global

  • Dynamic: Yes

  • Data Type: boolean

  • Default Value: OFF

log_slow_slave_statements

  • Description: Log slow statements executed by replica thread to the slow log if it is open. Before MariaDB 10.1.13, this was only available as a mariadbd option, not a server variable.

  • Commandline: --log-slow-slave-statements

  • Scope: Global

  • Dynamic: Yes

  • Data Type: boolean

  • Default Value:

    • ON (>= MariaDB 10.2.4)

    • OFF (<= MariaDB 10.2.3)

log_slave_updates

  • Description: If set to 0, the default, updates on a replica received from a primary during replication are not logged in the replica's binary log. If set to 1, they are. The replica's binary log needs to be enabled for this to have an effect. Set to 1 if you want to daisy-chain the replicas.

  • Commandline: --log-slave-updates

  • Scope: Global

  • Dynamic: No

  • Data Type: boolean

  • Default Value: OFF

master_verify_checksum

  • Description: Verify binlog checksums when reading events from the binlog on the primary.

  • Commandline: --master-verify-checksum=[0|1]

  • Scope: Global

  • Access Type: Can be changed dynamically

  • Data Type: bool

  • Default Value: OFF (0)

max_binlog_cache_size

  • Description: Restricts the size in bytes used to cache a multi-transactional query. If more bytes are required, a Multi-statement transaction required more than 'max_binlog_cache_size' bytes of storage error is generated. If the value is changed, current sessions are unaffected, only sessions started subsequently. See max_binlog_stmt_cache_size and binlog_cache_size.

  • Commandline: --max-binlog-cache-size=#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 18446744073709547520

  • Range: 4096 to 18446744073709547520

max_binlog_size

  • Description: If the binary log exceeds this size in bytes after a write, the server rotates it by closing it and opening a new binary log. Single transactions will always be stored in the same binary log, so the server will wait for open transactions to complete before rotating. This figure also applies to the size of relay logs if max_relay_log_size is set to zero.

  • Commandline: --max-binlog-size=#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 1073741824 (1GB)

  • Range: 4096 to 1073741824 (4KB to 1GB)

max_binlog_stmt_cache_size

  • Description: Restricts the size used to cache non-transactional statements. See max_binlog_cache_size and binlog_stmt_cache_size.

  • Commandline: --max-binlog-stmt-cache-size=#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 18446744073709547520 (64 bit), 4294963200 (32 bit)

  • Range: 4096 to 18446744073709547520

max_binlog_total_size

  • Description: Maximum space in bytes to use for all binary logs. Extra logs are deleted on server start, log rotation, FLUSH LOGS or when writing to binlog. Default is 0, which means no size restrictions. See also slave_connections_needed_for_purge.

  • Commandline: --max-binlog-size=#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 0

  • Range: 0 to 18446744073709551615

  • Introduced: MariaDB 11.4

max_relay_log_size

  • Description: Replica will rotate its relay log if it exceeds this size after a write. If set to 0, the max_binlog_size setting is used instead. Previously global only, since the implementation of multi-source replication, it can be set per session as well.

  • Commandline: --max-relay-log-size=#

  • Scope: Global, Session

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 0

  • Range: 0, or 4096 to 1073741824 (4KB to 1GB)

read_binlog_speed_limit

  • Description: Used to restrict the speed at which a replica can read the binlog from the primary. This can be used to reduce the load on a primary if many replicas need to download large amounts of old binlog files at the same time. The network traffic will be restricted to the specified number of kilobytes per second.

  • Commandline: --read-binlog-speed-limit=#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 0 (no limit)

  • Range: 0 to 18446744073709551615

relay_log

  • Description: Relay log basename. If not set, the basename of the files will be hostname-relay-bin, or derived from --log-basename. If --log-basename is also set, relay_log should be placed after in the config files. Later settings override earlier settings, so log-basename will override any earlier log file name settings.

  • Commandline: --relay-log=file_name

  • Scope: Global

  • Dynamic: No

  • Data Type: filename

  • Default Value: '' (none)

relay_log_basename

  • Description: The full path of the relay log file names, excluding the extension. Its value is derived from the relay-log variable value.

  • Commandline: No commandline option

  • Scope: Global

  • Dynamic: No

  • Data Type: string

  • Default Value: None

  • Dynamic: No

relay_log_index

  • Description: Name and location of the relay log index file, the file that keeps a list of the last relay logs. Defaults to hostname-relay-bin.index, or derived from --log-basename. If --log-basename is also set, relay_log_index should be placed after in the config files. Later settings override earlier settings, so log-basename will override any earlier log file name settings.

  • Commandline: --relay-log-index=name

  • Scope: Global

  • Dynamic: No

  • Data Type: string

  • Default Value: None

relay_log_info_file

  • Description: Name and location of the file where the RELAY_LOG_FILE and RELAY_LOG_POS options (i.e. the relay log position) for the CHANGE MASTER statement are written. The replica's SQL thread keeps this relay log position updated as it applies events.

    • See CHANGE MASTER TO: Option Persistence for more information.

  • Commandline: --relay-log-info-file=file_name

  • Scope: Global

  • Dynamic: No

  • Data Type: string

  • Default Value: relay-log.info

relay_log_purge

  • Description: If set to 1 (the default), relay logs will be purged as soon as they are no longer necessary.

  • Commandline: --relay-log-purge={0|1}

  • Scope: Global

  • Dynamic: Yes

  • Data Type: boolean

  • Default Value: ON

  • Note: In MySQL and in MariaDB before version 10.0.8 this variable was silently changed if you did CHANGE MASTER.

relay_log_recovery

  • Description: If set to 1 (0 is default), on startup the replica will drop all relay logs that haven't yet been processed, and retrieve relay logs from the primary. Can be useful after the replica has crashed to prevent the processing of corrupt relay logs. relay_log_recovery should always be set together with relay_log_purge. Setting relay-log-recovery=1 with relay-log-purge=0 can cause the relay log to be read from files that were not purged, leading to data inconsistencies.

  • Commandline: --relay-log-recovery

  • Scope: Global

  • Dynamic: Yes

  • Data Type: boolean

  • Default Value: OFF

relay_log_space_limit

  • Description: Specifies the maximum space to be used for the relay logs. The IO thread will stop until the SQL thread has cleared the backlog. By default 0, or no limit.

  • Commandline: --relay-log-space-limit=#

  • Scope: Global

  • Dynamic: No

  • Data Type: numeric

  • Default Value: 0

  • Range - 32 bit: 0 to 4294967295

  • Range - 64 bit: 0 to 18446744073709547520

replicate_annotate_row_events

  • Description: Tells the replica to reproduce annotate_rows_events received from the primary in its own binary log. This option is sensible only when used in tandem with the log_slave_updates option.

  • Commandline: --replicate-annotate-row-events

  • Scope: Global

  • Dynamic: No

  • Data Type: boolean

  • Default Value:

    • ON (>= MariaDB 10.2.4)

    • OFF (<= MariaDB 10.2.3)

replicate_do_db

  • Description: This system variable allows you to configure a replica to apply statements and transactions affecting databases that match a specified name.

    • This system variable will not work with cross-database updates with statement-based logging. See the Statement-Based Logging section for more information.

    • When setting it dynamically with SET GLOBAL, the system variable accepts a comma-separated list of filters.

    • When setting it on the command-line or in a server option group in an option file, the system variable does not accept a comma-separated list. If you would like to specify multiple filters, then you need to specify the system variable multiple times.

    • See Replication Filters for more information.

  • Commandline: --replicate-do-db=name

  • Scope: Global

  • Dynamic: Yes

  • Data Type: string

  • Default Value: '' (empty)

replicate_do_table

  • Description: This system variable allows you to configure a replica to apply statements and transactions that affect tables that match a specified name. The table name is specified in the format: dbname.tablename.

    • This system variable will not work with cross-database updates with statement-based logging. See the Statement-Based Logging section for more information.

    • When setting it dynamically with SET GLOBAL, the system variable accepts a comma-separated list of filters.

    • When setting it on the command-line or in a server option group in an option file, the system variable does not accept a comma-separated list. If you would like to specify multiple filters, then you need to specify the system variable multiple times.

    • See Replication Filters for more information.

  • Commandline: --replicate-do-table=name

  • Scope: Global

  • Dynamic: Yes

  • Data Type: string

  • Default Value: '' (empty)

replicate_events_marked_for_skip

  • Description: Tells the replica whether to replicate events that are marked with the @@skip_replication flag. See Selectively skipping replication of binlog events for more information.

  • Commandline: --replicate-events-marked-for-skip

  • Scope: Global

  • Dynamic: Yes

  • Data Type: enumeration

  • Default Value: replicate

  • Valid Values: REPLICATE, FILTER_ON_SLAVE, FILTER_ON_MASTER

replicate_ignore_db

  • Description: This system variable allows you to configure a replica to ignore statements and transactions affecting databases that match a specified name.

    • This system variable will not work with cross-database updates with statement-based logging. See the Statement-Based Logging section for more information.

    • When setting it dynamically with SET GLOBAL, the system variable accepts a comma-separated list of filters.

    • When setting it on the command-line or in a server option group in an option file, the system variable does not accept a comma-separated list. If you would like to specify multiple filters, then you need to specify the system variable multiple times.

    • See Replication Filters for more information.

  • Commandline: --replicate-ignore-db=name

  • Scope: Global

  • Dynamic: Yes

  • Data Type: string

  • Default Value: '' (empty)

replicate_ignore_table

  • Description: This system variable allows you to configure a replica to ignore statements and transactions that affect tables that match a specified name. The table name is specified in the format: dbname.tablename.

    • This system variable will not work with cross-database updates with statement-based logging. See the Statement-Based Logging section for more information.

    • When setting it dynamically with SET GLOBAL, the system variable accepts a comma-separated list of filters.

    • When setting it on the command-line or in a server option group in an option file, the system variable does not accept a comma-separated list. If you would like to specify multiple filters, then you need to specify the system variable multiple times.

    • See Replication Filters for more information.

  • Commandline: --replicate-ignore-table=name

  • Scope: Global

  • Dynamic: Yes

  • Data Type: string

  • Default Value: '' (empty)

replicate_rewrite_db

  • Description: This option allows you to configure a replica to rewrite database names. It uses the format primary_database->replica_database. If a replica encounters a binary log event in which the default database (i.e. the one selected by the USE statement) is primary_database, then the replica will apply the event in replica_database instead.

    • This option will not work with cross-database updates with statement-based logging. See the Statement-Based Logging section for more information.

    • This option only affects statements that involve tables. This option does not affect statements involving the database itself, such as CREATE DATABASE, ALTER DATABASE, and DROP DATABASE.

    • When setting it on the command-line or in a server option group in an option file, the option does not accept a comma-separated list. If you would like to specify multiple filters, then you need to specify the option multiple times.

    • See Replication Filters for more information.

    • Before MariaDB 10.11, replicate_rewrite_db was not available as a system variable, only as a mariadbd option, and could not be set dynamically. From MariaDB 10.11 it is available as a dynamic system variable

  • Commandline: --replicate-rewrite-db=primary_database->replica_database

  • Scope: Global

  • Dynamic: Yes

  • Data Type: string

  • Default Value: '' (empty)

  • Introduced: MariaDB 10.11.0

replicate_wild_do_table

  • Description: This system variable allows you to configure a replica to apply statements and transactions that affect tables that match a specified wildcard pattern. The wildcard pattern uses the same semantics as the LIKE operator.

    • This system variable will work with cross-database updates with statement-based logging. See the Statement-Based Logging section for more information.

    • When setting it dynamically with SET GLOBAL, the system variable accepts a comma-separated list of filters.

    • When setting it on the command-line or in a server option group in an option file, the system variable does not accept a comma-separated list. If you would like to specify multiple filters, then you need to specify the system variable multiple times.

    • See Replication Filters for more information.

  • Commandline: --replicate-wild-do-table=name

  • Scope: Global

  • Dynamic: Yes

  • Data Type: string

  • Default Value: '' (empty)

replicate_wild_ignore_table

  • Description: This system variable allows you to configure a replica to ignore statements and transactions that affect tables that match a specified wildcard pattern. The wildcard pattern uses the same semantics as the LIKE operator.

    • This system variable will work with cross-database updates with statement-based logging. See the Statement-Based Logging section for more information.

    • When setting it dynamically with SET GLOBAL, the system variable accepts a comma-separated list of filters.

    • When setting it on the command-line or in a server option group in an option file, the system variable does not accept a comma-separated list. If you would like to specify multiple filters, then you need to specify the system variable multiple times.

    • See Replication Filters for more information.

  • Commandline: --replicate-wild-ignore-table=name

  • Scope: Global

  • Dynamic: Yes

  • Data Type: string

  • Default Value: '' (empty)

report_host

  • Description: The host name or IP address the replica reports to the primary when it registers. If left unset, the replica will not register itself. Reported by SHOW SLAVE HOSTS. Note that it is not sufficient for the primary to simply read the IP of the replica from the socket once the replica connects. Due to NAT and other routing issues, that IP may not be valid for connecting to the replica from the primary or other hosts.

  • Commandline: --report-host=host_name

  • Scope: Global

  • Dynamic: No

  • Data Type: string

report_password

  • Description: Replica password reported to the primary when it registers. Reported by SHOW SLAVE HOSTS if --show-slave-auth-info is set. This password has no connection with user privileges or with the replication user account password.

  • Commandline: --report-password=password

  • Scope: Global

  • Dynamic: No

  • Data Type: string

report_port

  • Description: The commandline option sets the TCP/IP port for connecting to the replica that will be reported to the replicating primary during the replica's registration. Viewing the variable will show this value.

  • Commandline: --report-port=#

  • Scope: Global

  • Dynamic: No

  • Data Type: numeric

  • Default Value: 0

  • Range: 0 to 65535

report_user

  • Description: Replica's account user name reported to the primary when it registers. Reported by SHOW SLAVE HOSTS if --show-slave-auth-info is set. This username has no connection with user privileges or with the replication user account.

  • Commandline: --report-user=name

  • Scope: Global

  • Dynamic: No

  • Data Type: string

server_id

  • Description: This system variable is used with MariaDB replication to identify unique primary and replica servers in a topology. This system variable is also used with the binary log to determine which server a specific transaction originated on.

    • When MariaDB replication is used with standalone MariaDB Server, each server in the replication topology must have a unique server_id value.

    • When MariaDB replication is used with MariaDB Galera Cluster, see Using MariaDB Replication with MariaDB Galera Cluster: Setting server_id on Cluster Nodes for more information on how to set the server_id values.

    • In MariaDB 10.2.1 and below, the default server_id value is 0. If a replica's server_id value is 0, then all primary's will refuse its connection attempts. If a primary's server_id value is 0, then it will refuse all replica connection attempts.

  • Commandline: --server-id =#

  • Scope: Global, Session

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 1

  • Range: 1 to 4294967295

skip_parallel_replication

  • Description: If set when a transaction is written to the binlog, parallel apply of that transaction will be avoided on a replica where slave_parallel_mode is not aggressive. Can be used to avoid unnecessary rollback and retry for transactions that are likely to cause a conflict if replicated in parallel. See parallel replication.

  • Commandline: None

  • Scope: Session

  • Dynamic: Yes

  • Data Type: boolean

  • Default Value: OFF

skip_replication

  • Description: Changes are logged into the binary log with the @@skip_replication flag set. Such events will not be replicated by replica that run with --replicate-events-marked-for-skip set different from its default of REPLICATE. See Selectively skipping replication of binlog events for more information.

  • Commandline: None

  • Scope: Session

  • Dynamic: Yes

  • Data Type: boolean

  • Default Value: OFF

slave_abort_blocking_timeout

  • Description: Maximum time a replica DDL will wait for a blocking SELECT or other user query until that query will be aborted. The argument will be treated as a decimal value with nanosecond precision. The variable is intended to solve a problem where a long-running SELECT on a replica causes DDL to wait for that SELECT to complete, potentially causing massive replica lag.

  • Commandline: --slave-abort-blocking-timeout=num

  • Scope: Global

  • Dynamic: Yes

  • Data Type: double

  • Default Value: 31536000.000000

  • Range: 0 to 31536000

  • Introduced: MariaDB 11.7

slave_compressed_protocol

  • Description: If set to 1 (0 is the default), will use compression for the replica/primary protocol if both primary and replica support this.

  • Commandline: --slave-compressed-protocol

  • Scope: Global

  • Dynamic: Yes

  • Data Type: boolean

  • Default Value: 0

slave_connections_needed_for_purge

  • Description: Minimum number of connected replicas required for automatic binary log purge with max_binlog_total_size, binlog_expire_logs_seconds or expire_logs_days. Change of the value triggers an attempt to purging, though without binlog rotation, with the purged set of files satisfying the above two parameters and the value that is set itself.

  • Commandline: --slave-connections-needed-for-purge=#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 1; 0 on Galera cluster nodes.

  • Range: 0 to 18446744073709551615

  • Introduced: MariaDB 11.4

slave_ddl_exec_mode

  • Description: Modes for how replication of DDL events should be executed. Legal values are STRICT and IDEMPOTENT (default). In IDEMPOTENT mode, the replica will not stop for failed DDL operations that would not cause a difference between the primary and the replica. In particular CREATE TABLE is treated as CREATE OR REPLACE TABLE and DROP TABLE is treated as DROP TABLE IF EXISTS.

  • Commandline: --slave-ddl-exec-mode=name

  • Scope: Global

  • Dynamic: Yes

  • Data Type: enumeration

  • Default Value: IDEMPOTENT

  • Valid Values: IDEMPOTENT, STRICT

slave_domain_parallel_threads

  • Description: When set to a non-zero value, each replication domain in one primary connection can reserve at most that many worker threads at any one time, leaving the rest (up to the value ofslave_parallel_threads) free for other primary connections or replication domains to use in parallel. See Parallel Replication for details.

  • Commandline: --slave-domain-parallel-threads=#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 0

  • Valid Values: 0 to 16383

slave_exec_mode

  • Description: Determines the mode used for replication error checking and conflict resolution. STRICT mode is the default, and catches all errors and conflicts. IDEMPOTENT mode suppresses duplicate key or no key errors, which can be useful in certain replication scenarios, such as when there are multiple primaries, or circular replication.

  • Scope: Global

  • Dynamic: Yes

  • Data Type: enumeration

  • Default Value: IDEMPOTENT (NDB), STRICT (All)

  • Valid Values: IDEMPOTENT, STRICT

slave_load_tmpdir

  • Description: Directory where the replica stores temporary files for replicating LOAD DATA INFILE statements. If not set, the replica will use tmpdir. Should be set to a disk-based directory that will survive restarts, or else replication may fail.

  • Commandline: --slave-load-tmpdir=path

  • Scope: Global

  • Dynamic: No

  • Data Type: file name

  • Default Value: /tmp

slave_max_allowed_packet

  • Description: Maximum packet size in bytes for replica SQL and I/O threads. This value overrides max_allowed_packet for replication purposes. Set in multiples of 1024 (the minimum) up to 1GB

  • Commandline: --slave-max-allowed-packet=#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 1073741824

  • Range: 1024 to 1073741824

slave_max_statement_time

  • Description: A query that has taken more than this in seconds to run on the replica will be aborted. The argument will be treated as a decimal value with microsecond precision. A value of 0 (default) means no timeout.

  • Commandline: --slave-max-statement-time=#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 0.000000

  • Range: 0 to 31536000

  • Introduced: MariaDB 10.10

slave_net_timeout

  • Description: Time in seconds for the replica to wait for more data from the primary before considering the connection broken, after which it will abort the read and attempt to reconnect. The retry interval is determined by the MASTER_CONNECT_RETRY open for the CHANGE MASTER statement, while the maximum number of reconnection attempts is set by the master-retry-count option. The first reconnect attempt takes place immediately.

  • Commandline: --slave-net-timeout=#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value:

    • 60 (1 minute)

  • Range: 1 to 31536000

slave_parallel_max_queued

  • Description: When parallel_replication is used, the SQL thread will read ahead in the relay logs, queueing events in memory while looking for opportunities for executing events in parallel. This system variable sets a limit for how much memory it will use for this.

    • The configured value of this system variable is actually allocated for each worker thread, so the total allocation is actually equivalent to the following:

      • slave_parallel_max_queued * slave_parallel_threads

    • This system variable is only meaningful when parallel replication is configured (i.e. when slave_parallel_threads > 0).

    • See Parallel Replication: Configuring the Maximum Size of the Parallel Slave Queue for more information.

  • Commandline: --slave-parallel-max-queued=#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 131072

  • Range: 0 to 2147483647

slave_parallel_mode

  • Description: Controls what transactions are applied in parallel when using parallel replication.

    • optimistic: tries to apply most transactional DML in parallel, and handles any conflicts with rollback and retry. See optimistic mode.

    • conservative: limits parallelism in an effort to avoid any conflicts. See conservative mode.

    • aggressive: tries to maximize the parallelism, possibly at the cost of increased conflict rate.

    • minimal: only parallelizes the commit steps of transactions.

    • none disables parallel apply completely.

  • Commandline: None

  • Scope: Global

  • Dynamic: Yes

  • Data Type: enum

  • Default Value: optimistic (>= MariaDB 10.5.1), conservative (<= MariaDB 10.5.0)

  • Valid Values: conservative, optimistic, none, aggressive and minimal

slave_parallel_threads

  • Description: This system variable is used to configure parallel replication.

    • If this system variable is set to a value greater than 0, then its value will determine how many replica worker threads will be created to apply binary log events in parallel.

    • If this system variable is set to 0 (which is the default value), then no replica worker threads will be created. Instead, when replication is enabled, binary log events are applied by the replica's SQL thread.

    • The replica threads must be stopped in order to change this option's value dynamically.

    • Events that were logged with GTIDs with different gtid_domain_id values can be applied in parallel in an out-of-order manner. Each gtid_domain_id can use the number of threads configured by slave_domain_parallel_threads.

    • Events that were group-committed on the primary can be applied in parallel in an in-order manner, and the specific behavior can be configured by setting slave_parallel_mode.

  • Commandline: --slave-parallel-threads=#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 0

  • Range: 0 to 16383

slave_parallel_workers

  • Description: Alias for slave_parallel_threads.

  • Commandline: --slave-parallel-workers=#

slave_run_triggers_for_rbr

  • Description: See Running triggers on the slave for Row-based events for a description and use-case for this setting.

  • Commandline: --slave-run-triggers-for-rbr=value

  • Scope: Global

  • Dynamic: Yes

  • Data Type: enum

  • Default Value: NO

  • Valid Values: NO, YES, LOGGING, or ENFORCE (>= MariaDB 10.5.2)

slave_skip_errors

  • Description: When an error occurs on the replica, replication usually halts. This option permits a list of error codes to ignore, and for which replication will continue. This option should never be needed in normal use, and careless use could lead to replica that are out of sync with primary's. Error codes are in the format of the number from the replica error log. Using all as an option permits the replica the keep replicating no matter what error it encounters, an option you would never normally need in production and which could rapidly lead to data inconsistencies. A count of these is kept in slave_skipped_errors.

  • Commandline: --slave-skip-errors=[error_code1,error_code2,...|all|ddl_exist_errors]

  • Scope: Global

  • Dynamic: No

  • Data Type: string

  • Default Value: OFF

  • Valid Values: [list of error codes], ALL, OFF

slave_sql_verify_checksum

  • Description: Verify binlog checksums when the replica SQL thread reads events from the relay log.

  • Commandline: --slave-sql-verify-checksum=[0|1]

  • Scope: Global

  • Access Type: Can be changed dynamically

  • Data Type: bool

  • Default Value: ON (1)

slave_transaction_retries

  • Description: Number of times a replication replica retries to execute an SQL thread after it fails due to InnDB deadlock or by exceeding the transaction execution time limit. If after this number of tries the SQL thread has still failed to execute, the replica will stop with an error. See also the innodb_lock_wait_timeout system variable.

  • Commandline: --slave-transaction-retries=#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 10

  • Range - 32 bit: 0 to 4294967295

  • Range - 64 bit: 0 to 18446744073709547520

slave_transaction_retry_errors

  • Description: When an error occurs during a transaction on the replica, replication usually halts. By default, transactions that caused a deadlock or elapsed lock wait timeout will be retried. One can add other errors to the list of errors that should be retried by adding a comma-separated list of error numbers to this variable. This is particularly useful in some Spider setups. Some recommended errors to retry for Spider are 1020, 1158, 1159, 1160, 1161, 1429, 2013, 12701 (these are in the default value in recent versions).

  • Commandline: --slave-transaction_retry-errors=[error_code1,error_code2,...]

  • Scope: Global

  • Dynamic: No

  • Data Type: string

  • Default Value:

    • 1158,1159,1160,1161,1205,1213,1020,1429,2013,12701 (>= MariaDB 10.6.18, MariaDB 10.11.8, MariaDB 11.0.6, MariaDB 11.1.5, MariaDB 11.2.4, MariaDB 11.4.2)

    • 1158,1159,1160,1161,1205,1213,1429,2013,12701 (>= MariaDB 10.4.5)

  • Valid Values: comma-separated list of error codes

  • Introduced: MariaDB 10.3.3

slave_transaction_retry_interval

  • Description: Interval in seconds for the replica SQL thread to retry a failed transaction due to a deadlock, elapsed lock wait timeout or an error listed in slave_transaction_retry_errors. The interval is calculated as max(slave_transaction_retry_interval, min(retry_count, 5)).

  • Commandline: --slave-transaction-retry-interval=#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 0

  • Range: 0 to 3600

  • Introduced: MariaDB 10.3.3

slave_type_conversions

  • Description: Determines the type conversion mode on the replica when using row-based replication, including replications in MariaDB Galera cluster. Multiple options can be set, delimited by commas. If left empty, the default, type conversions are disallowed. The variable is dynamic and a change in its value takes effect immediately. This variable tells the server what to do if the table definition is different between the primary and replica (for example a column is 'int' on the primary and 'bigint' on the replica).

    • ALL_NON_LOSSY means that all safe conversions (no data loss) are allowed.

    • ALL_LOSSY means that all lossy conversions are allowed (for example 'bigint' to 'int'). This, however, does not imply that safe conversions (non-lossy) are allowed as well. In order to allow all conversions, one needs to allow both lossy as well as non-lossy conversions by setting this variable to ALL_NON_LOSSY,ALL_LOSSY.

    • Empty (default) means that the server should give an error and replication should stop if the table definition is different between the primary and replica.

  • Commandline: --slave-type-conversions=set

  • Scope: Global

  • Dynamic: Yes

  • Data Type: set

  • Default Value: Empty variable

  • Valid Values: ALL_LOSSY, ALL_NON_LOSSY, empty

sql_log_bin

  • Description: If set to 0 (1 is the default), no logging to the binary log is done for the client. Only clients with the SUPER privilege can update this variable. Does not affect the replication of events in a Galera cluster. Note that sql_log_bin has no effect if log_bin is not set.

  • Scope: Session

  • Dynamic: Yes

  • Data Type: boolean

  • Default Value: 1

  • Related variables: log_bin

sql_slave_skip_counter

  • Description: Number of events that a replica skips from the primary. If this would cause the replica to begin in the middle of an event group, the replica will instead begin from the beginning of the next event group. See SET GLOBAL sql_slave_skip_counter.

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 0

sync_binlog

  • Description: MariaDB will synchronize its binary log file to disk after this many events. The default is 0, in which case the operating system handles flushing the file to disk. 1 is the safest, but slowest, choice, since the file is flushed after each write. If autocommit is enabled, there is one write per statement, otherwise there's one write per transaction. If the disk has cache backed by battery, synchronization will be fast and a more conservative number can be chosen.

  • Commandline: --sync-binlog=#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 0

  • Range: 0 to 4294967295

sync_master_info

  • Description: A replication replica will synchronize its master.info file to disk after this many events. If set to 0, the operating system handles flushing the file to disk.

  • Commandline: --sync-master-info=#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 10000

sync_relay_log

  • Description: The MariaDB server will synchronize its relay log to disk after this many writes to the log. The default until MariaDB 10.1.7 was 0, in which case the operating system handles flushing the file to disk. 1 is the safest, but slowest, choice, since the file is flushed after each write. If autocommit is enabled, there is one write per statement, otherwise there's one write per transaction. If the disk has cache backed by battery, synchronization will be fast and a more conservative number can be chosen.

  • Commandline: --sync-relay-log=#

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 10000

sync_relay_log_info

  • Description: A replication replica will synchronize its relay-log.info file to disk after this many transactions. The default until MariaDB 10.1.7 was 0, in which case the operating system handles flushing the file to disk. 1 is the most secure choice, because at most one event could be lost in the event of a crash, but it's also the slowest.

  • Commandline: --sync-relay-log-info=#

  • Scope: Global,

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 10000

  • Range: 0 to 4294967295

This page is licensed: CC BY-SA / Gnu FDL

Replication and Binary Log Status Variables

Monitor replication & binary log status with status variables. This section details key metrics for observing replication health & binary log activity, aiding in troubleshooting & performance tuning.

The terms master and slave have historically been used in replication, and MariaDB has begun the process of adding primary and replica synonyms. The old terms will continue to be used to maintain backward compatibility - see MDEV-18777 to follow progress on this effort.

The following status variables are useful in binary logging and replication. See Server Status Variables for a complete list of status variables that can be viewed with SHOW STATUS.

See also the Full list of MariaDB options, system and status variables.

Binlog_bytes_written

  • Description: The number of bytes written to the binary log.

  • Scope: Global

  • Data Type: numeric

Binlog_cache_disk_use

  • Description: Number of transactions which used a temporary disk cache because they could not fit in the regular binary log cache, being larger than binlog_cache_size. The global value can be flushed by FLUSH STATUS.

  • Scope: Global

  • Data Type: numeric

Binlog_cache_use

  • Description: Number of transaction which used the regular binary log cache, being smaller than binlog_cache_size. The global value can be flushed by FLUSH STATUS.

  • Scope: Global

  • Data Type: numeric

Binlog_commits

  • Description: Total number of transactions committed to the binary log.

  • Scope: Global

  • Data Type: numeric

Binlog_disk_use

  • Description: If max-binlog-total_size is not set to zero, shows the space usage of the binary log in bytes.

  • Scope: Global

  • Data Type: numeric

  • Introduced: MariaDB 11.4

Binlog_group_commit_trigger_count

  • Description: Total number of group commits triggered because of the number of binary log commits in the group reached the limit set by the variable binlog_commit_wait_count. See Group commit for the binary log.

  • Scope: Global

  • Data Type: numeric

Binlog_group_commit_trigger_lock_wait

  • Description: Total number of group commits triggered because a binary log commit was being delayed because of a lock wait where the lock was held by a prior binary log commit. When this happens the later binary log commit is placed in the next group commit. See Group commit for the binary log.

  • Scope: Global

  • Data Type: numeric

Binlog_group_commit_trigger_timeout

  • Description: Total number of group commits triggered because of the time since the first binary log commit reached the limit set by the variable binlog_commit_wait_usec. See Group commit for the binary log.

  • Scope: Global

  • Data Type: numeric

Binlog_group_commits

  • Description: Total number of group commits done to the binary log. See Group commit for the binary log.

  • Scope: Global

  • Data Type: numeric

Binlog_gtid_index_hit

  • Description: Incremented for each successful lookup in a GTID index.

  • Scope: Global

  • Data Type: numeric

  • Introduced: MariaDB 11.4

Binlog_gtid_index_miss

  • Description: Incremented when a GTID index lookup is not possible, which indicates that the index file is missing (eg. binlog written by old server version without GTID index support), or corrupt.

  • Scope: Global

  • Data Type: numeric

  • Introduced: MariaDB 11.4

Binlog_snapshot_file

  • Description: The binary log file. Unlike SHOW MASTER STATUS, can be queried in a transactionally consistent way, irrespective of which other transactions have been committed since the snapshot was taken. See Enhancements for START TRANSACTION WITH CONSISTENT SNAPSHOT.

  • Scope: Global

  • Data Type: string

Binlog_snapshot_position

  • Description: The binary log position. Unlike SHOW MASTER STATUS, can be queried in a transactionally consistent way, irrespective of which other transactions have been committed since the snapshot was taken. See Enhancements for START TRANSACTION WITH CONSISTENT SNAPSHOT.

  • Scope: Global

  • Data Type: numeric

Binlog_stmt_cache_disk_use

  • Description: Number of non-transaction statements which used a temporary disk cache because they could not fit in the regular binary log cache, being larger than binlog_stmt_cache_size. The global value can be flushed by FLUSH STATUS.

  • Scope: Global

  • Data Type: numeric

Binlog_stmt_cache_use

  • Description: Number of non-transaction statement which used the regular binary log cache, being smaller than binlog_stmt_cache_size. The global value can be flushed by FLUSH STATUS.

  • Scope: Global

  • Data Type: numeric

Com_change_master

  • Description: Number of CHANGE MASTER TO statements executed.

  • Scope: Global, Session

  • Data Type: numeric

Com_show_binlog_status

  • Description:

  • Scope: Global, Session

  • Data Type: numeric

  • Introduced: MariaDB 10.5.2

Com_show_master_status

  • Description: Number of SHOW MASTER STATUS commands executed.

  • Scope: Global, Session

  • Data Type: numeric

  • Removed: MariaDB 10.5.2

Com_show_new_master

  • Description:

  • Scope: Global, Session

  • Data Type: numeric

  • Removed: MariaDB 5.5

Com_show_slave_hosts

  • Description: Number of SHOW SLAVE HOSTS commands executed.

  • Scope: Global, Session

  • Data Type: numeric

Com_show_slave_status

  • Description: Number of SHOW SLAVE STATUS commands executed.

  • Scope: Global, Session

  • Data Type: numeric

Com_slave_start

  • Description: Number of START SLAVE commands executed. Removed in MariaDB 10.0, see Com_start_slave.

  • Scope: Global, Session

  • Data Type: numeric

  • Removed: MariaDB 10.0

Com_slave_stop

  • Description: Number of STOP SLAVE commands executed. Removed in MariaDB 10.0, see Com_stop_slave.

  • Scope: Global, Session

  • Data Type: numeric

  • Removed: MariaDB 10.0

Com_start_all_slaves

  • Description: Number of START ALL SLAVES commands executed.

  • Scope: Global, Session

  • Data Type: numeric

Com_start_slave

  • Description: Number of START SLAVE commands executed. Replaces the old Com_slave_start.

  • Scope: Global, Session

  • Data Type: numeric

Com_stop_all_slaves

  • Description: Number of STOP ALL SLAVES commands executed.

  • Scope: Global, Session

  • Data Type: numeric

Com_stop_slave

  • Description: Number of STOP SLAVE commands executed. Replaces the old Com_slave_stop.

  • Scope: Global, Session

  • Data Type: numeric

Master_gtid_wait_count

  • Description: Number of times MASTER_GTID_WAIT called.

  • Scope: Global, Session

  • Data Type: numeric

Master_gtid_wait_time

  • Description: Total number of time spent in MASTER_GTID_WAIT.

  • Scope: Global, Session

  • Data Type: numeric

Master_gtid_wait_timeouts

  • Description: Number of timeouts occurring in MASTER_GTID_WAIT.

  • Scope: Global, Session

  • Data Type: numeric

Rpl_status

  • Description: For showing the status of fail-safe replication. Removed in MySQL 5.6, still present in MariaDB 10.0.

Rpl_transactions_multi_engine

  • Description: Number of replicated transactions that involved changes in multiple (transactional) storage engines, before considering the update of mysql.gtid_slave_pos. These are transactions that were already cross-engine, independent of the GTID position update introduced by replication. The global value can be flushed by FLUSH STATUS.

  • Scope: Global

  • Data Type: numeric

  • Introduced: MariaDB 10.3.1

Slave_connections

  • Description: Number of REGISTER_SLAVE attempts. In practice the number of times slaves has tried to connect to the master.

  • Scope: Global

  • Data Type: numeric

Slave_heartbeat_period

  • Description: Time in seconds that a heartbeat packet is requested from the master by a slave.

  • Scope: Global

  • Data Type: numeric

Slave_open_temp_tables

  • Description: Number of temporary tables the slave has open.

  • Scope: Global

  • Data Type: numeric

Slave_received_heartbeats

  • Description: Number of heartbeats the slave has received from the master.

  • Scope: Global

  • Data Type: numeric

Slave_retried_transactions

  • Description: Number of times the slave has retried transactions since the server started. The global value can be flushed by FLUSH STATUS.

  • Scope: Global

  • Data Type: numeric

Slave_running

  • Description: Whether the default connection slave is running (both I/O and SQL threads are running) or not.

  • Scope: Global

  • Data Type: numeric

Slave_skipped_errors

  • Description: The number of times a slave has skipped errors defined by slave-skip-errors.

  • Scope: Global

  • Data Type: numeric

Slaves_connected

  • Description: Number of slaves connected.

  • Scope: Global

  • Data Type: numeric

Slaves_running

  • Description: Number of slave SQL threads running.

  • Scope: Global

  • Data Type: numeric

Transactions_gtid_foreign_engine

  • Description: Number of replicated transactions where the update of the gtid_slave_pos table had to choose a storage engine that did not otherwise participate in the transaction. This can indicate that setting gtid_pos_auto_engines might be useful. The global value can be flushed by FLUSH STATUS.

  • Scope: Global

  • Data Type: numeric

  • Introduced: MariaDB 10.3.1

Transactions_multi_engine

  • Description: Number of transactions that changed data in multiple (transactional) storage engines. If this is significantly larger than Rpl_transactions_multi_engine, it indicates that setting gtid_pos_auto_engines could reduce the need for cross-engine transactions. The global value can be flushed by FLUSH STATUS.

  • Scope: Global

  • Data Type: numeric

  • Introduced: MariaDB 10.3.1

This page is licensed: CC BY-SA / Gnu FDL

Binlog Event Checksum Interoperability

Understand binlog event checksum interoperability in replication. This section explains how checksums ensure data integrity during replication and their compatibility across different server versions.

The terms master and slave have historically been used in replication, and MariaDB has begun the process of adding primary and replica synonyms. The old terms will continue to be used to maintain backward compatibility - see MDEV-18777 to follow progress on this effort.

The introduction of checksums on binlog events changes the format that events are stored in binary log files and sent over the network to replicas. This raises the question on what happens when replicating between different versions of the server, where one server is a newer version that has the binlog checksum feature implemented, while the other server is an older version that does not know about binlog checksums.

When checksums are disabled on the primary (or the primary has the old version with no checksums implemented), there is no problem. In this case the binlog format is backwards compatible, and replication works fine.

When the primary is a newer version with checksums enabled in the binlog, but the replica is an old version that does not understand checksums, replication will fail. The primary will disconnect the replica with an error, and also log a warning in its own error log. This prevents sending events to the replica that it will be unable to interpret correctly, but means that binlog checksums can not be used with older replicas. (With the recommended upgrade path, where replicas are upgraded before primaries, this is not a problem of course).

Replicating from a new MySQL primary with checksums enabled to a new MariaDB which also understands checksums works, and the MariaDB replica will verify checksums on replicated events.

There is however a problem when a newer MySQL replica replicates against a newer MariaDB primary with checksums enabled. The replica server looks at the primary server version to know whether events include checksums or not, and MySQL has not yet been updated to learn that MariaDB does this already from version 5.3.0 (as of the time of writing, MySQL 5.6.2). Thus, if MariaDB at least version 5.3.0 but less that 5.6.1 is used as a primary with binlog checksums enabled, a MySQL replica will interpret the received events incorrectly as it does not realise the last part of the events is the checksum. So replication will fail with an error about corrupt events or even silent corruption of replicated data in unlucky cases. This requires changes to the MySQL server to fix.

Here is a summary table of the status of replication between different combination of primary and replica servers and checksum enabled/disabled:

  • OLD: MySQL <5.6.1 or MariaDB < 5.3.0 with no checksum capabilities

  • NEW-MARIA: MariaDB >= 5.3.0 with checksum capabilities

  • NEW-MYSQL: MySQL >= 5.6.1 with checksum capabilities

Primary mariadb-lbinlog
Replica / enabled?
Checksums
Status

OLD

OLD

-

Ok

OLD

NEW-MARIA

-

Ok

OLD

MYSQL

-

Ok

NEW-MARIA

OLD

No

Ok

NEW-MARIA

OLD

Yes

Primary will refuse with error

NEW-MARIA

NEW-MARIA

Yes/No

Ok

NEW-MARIA

NEW-MYSQL

No

Ok

NEW-MARIA

NEW-MYSQL

Yes

Fail. Requires changes in MySQL, otherwise it will not realise MariaDB < 5.6.1 does checksums and will be confused.

NEW-MYSQL

OLD

No

Ok

NEW-MYSQL

OLD

Yes

Primary will refuse with error

NEW-MYSQL

NEW-MARIA

Yes/No

Ok

NEW-MYSQL

NEW-MYSQL

Yes/No

Ok

Checksums and mariadb-binlog

When using the mariadb-binlog client program, there are similar issues.

A version of mariadb-binlog which understands checksums can read binlog files from either old or new servers, with or without checksums enabled.

An old version of mariadb-binlog can read binlog files produced by a new server version if checksums were disabled when the log was produced. Old versions of mariadb-binlog reading a new binlog file containing checksums will be confused, and output will be garbled, with the added checksums being interpreted as extra garbage at the end of query strings and similar entries. No error will be reported in this case, just wrong output.

A version of mysqlbinlog (the MySQL equivalent to mariadb-binlog and the old MariaDB name for the binary) from MySQL >= 5.6.1 will have similar problems as a replica until this is fixed in MySQL. When reading a binlog file with checksums produced by MariaDB >= 5.3.0 but < 5.6.1, it will not realise that checksums are included, and will produce garbled output just like an old version ofmysqlbinlog. The MariaDB version of mariadb-binlog can read binlog files produced by either MySQL or MariaDB just fine.

See Also

  • Binlog Event Checksums

This page is licensed: CC BY-SA / Gnu FDL

Binlog Event Checksums

Understand binlog event checksums in MariaDB Server. This section explains how checksums ensure data integrity and detect corruption in the binary log during replication processes.

The terms master and slave have historically been used in replication, and MariaDB has begun the process of adding primary and replica synonyms. The old terms will continue to be used to maintain backward compatibility - see MDEV-18777 to follow progress on this effort.

MariaDB includes a feature to include a checksum in binary log events.

Checksums are enabled with the binlog_checksum option. Until MariaDB 10.2.1, this was disabled by default. From MariaDB 10.2.1, the option is set to CRC32.

The variable can be changed dynamically without restarting the server. Setting the variable in any way (even to the existing value) forces a rotation of thebinary log (the intention is to avoid having a single binlog where some events are checksummed and others are not).

When checksums are enabled, replicas will check events received over the network for checksum errors, and will stop with an error if a corrupt event is detected.

In addition, the server can be configured to verify checksums in two other places.

One is when reading events from the binlog on the primary, for example when sending events to a replica or for something like SHOW BINLOG EVENTS. This is controlled by option master_verify_checksum, and is thus used to detect file system corruption of the binlog files.

The other is when the replica SQL thread reads events from the relay log. This is controlled by the slave_sql_verify_checksum option, and is used to detect file system corruption of replica relay log files.

MariaDB starting with 11.4

From MariaDB 11.4, binlog checksums are computed when writing events into the statement or transaction caches, where before this was done when the caches were copied to the real binlog file. This moves the checksum computation outside of holding LOCK_log, improving scalability. See MDEV-31273.

master_verify_checksum

  • Description: Verify binlog checksums when reading events from the binlog on the primary.

  • Commandline: --master_verify_checksum=[0|1]

  • Scope: Global

  • Access Type: Can be changed dynamically

  • Data Type: bool

  • Default Value: OFF (0)

slave_sql_verify_checksum

  • Description: Verify binlog checksums when the replica SQL thread reads events from the relay log.

  • Commandline: --slave_sql_verify_checksum=[0|1]

  • Scope: Global

  • Access Type: Can be changed dynamically

  • Data Type: bool

  • Default Value: ON (1)

The mariadb-binlog client program by default does not verify checksums when reading a binlog file, however it can be instructed to do so with the option verify-binlog-checksum:

  • Variable Name: verify-binlog-checksum

  • Data Type: bool

  • Default Value: OFF

See Also

  • Binlog Event Checksum Interoperability

  • What is MariaDB 5.3

This page is licensed: CC BY-SA / Gnu FDL

Changing a Replica to Become the Primary

Learn how to transition a replica to become the primary server in MariaDB. This section provides the steps and considerations for promoting a replica to handle write operations and maintain HA.

The terms master and slave have historically been used in replication, and MariaDB has begun the process of adding primary and replica synonyms. The old terms will continue to be used to maintain backward compatibility - see MDEV-18777 to follow progress on this effort.

This article describes how to change a replica to become a primary and optionally to set the old primary as a replica for the new primary.

A typical scenario of when this is useful is if you have set up a new version of MariaDB as a replica, for example for testing, and want to upgrade your primary to the new version.

In MariaDB replication, a replica should be of a version same or newer than the primary. Because of this, one should first upgrades all replicas to the latest version before changing a replica to be a primary. In some cases one can have a replica to be of an older version than the primary, as long as one doesn't execute on the primary any SQL commands that the replica doesn't understand. This is however not guaranteed between all major MariaDB versions.

Note that in the examples below, [connection_name] is used as the name of the connection. If you are not using named connections you can ignore this.

Stopping the Original Master.

First one needs to take down the original primary in such a way that the replica has all information on the primary.

If you are using Semisynchronous Replication you can just stop the server with the SHUTDOWN command as the replicas should be automatically up to date.

If you are using MariaDB MaxScale proxy, then you can use MaxScale to handle the whole process of taking down the primary and replacing it with one of the replicas.

If neither of the above is true, you have to do this step manually:

Manually Take Down the Primary

First we have to set the primary to read only to ensure that there are no new updates on the primary:

FLUSH TABLES WITH READ LOCK;

Note that you should not disconnect this session as otherwise the read lock will disappear and you have to start from the beginning.

Then you should check the current position of the primary:

SHOW MASTER STATUS;
+--------------------+----------+--------------+------------------+
| File               | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| mariadb-bin.000003 |      343 |              |                  |
+--------------------+----------+--------------+------------------+
SELECT @@global.gtid_binlog_pos;
+--------------------------+
| @@global.gtid_binlog_pos |
+--------------------------+
| 0-1-2                    |
+--------------------------+

And wait until you have the same position on the replica: (The following should be expected on the replica)

SHOW SLAVE [connection_name] STATUS;
+-------------------+-------------------+
Master_Log_File     | narttu-bin.000003 +
Read_Master_Log_Pos | 343               +
Exec_Master_Log_Pos | 343               +
...
Gtid_IO_Pos          0-1-2              +
+-------------------+-------------------+

The most important information to watch are Master_Log_File andExec_Master_Log_Pos as when this matches the primary, it signals that all transactions have been committed on the replica.

Note that Gtid_IO_Pos on replica can contain many different positions separated with ',' if the replica has been connected to many different primaries. What is important is that all the sequences that are on the primary is also on the replica.

When replica is up to date, you can then take the PRIMARY down. This should be on the same connection where you executed FLUSH TABLES WITH READ LOCK.

SHUTDOWN;

Preparing the Replica to be a Primary

Stop all old connections to the old primary(s) and reset read only mode, if you had it enabled. You also want to save the values ofSHOW MASTER STATUS and gtid_binlog_pos, as you may need these to setup new replicas.

STOP ALL SLAVES;
RESET SLAVE ALL;
SHOW MASTER STATUS;
SELECT @@global.gtid_binlog_pos;
SET @@global.read_only=0;

Reconnect Other Replicas to the New Primary

On the other replicas you have point them to the new primary (the replica you promoted to a primary).

STOP SLAVE [connection_name];
CHANGE MASTER [connection_name] TO 
 MASTER_HOST="new_master_name",
 MASTER_PORT=3306, 
 MASTER_USER='root', 
 MASTER_USE_GTID=current_pos,
 MASTER_LOG_FILE="XXX", 
 MASTER_LOG_POS=XXX;
START SLAVE;

The XXX values for MASTER_LOG_FILE and MASTER_LOG_POS should be the values you got from the SHOW MASTER STATUS command you did when you finished setting up the replica.

Changing the Old Primary to be a Replica

Now you can upgrade the old primary to a newer version of MariaDB and then follow the same procedure to connect it as a replica.

When starting the original primary, it's good to start the mysqld executable with the --with-skip-slave-start and --read-only options to ensure that no old replica configurations could cause any conflicts.

For the same reason it's also good to execute the following commands on the old primary (same as for other replicas, but with some extra security). The read_only option below is there to ensure that old applications doesn't by accident try to update the old primary by mistake. It only affects normal connections to the replica, not changes from the new primary.

SET @@global.read_only=1;
STOP ALL SLAVES;
RESET MASTER;
RESET SLAVE ALL;
CHANGE MASTER [connection_name] TO 
 MASTER_HOST="new_master_name",
 MASTER_PORT=3306, 
 MASTER_USER='root', 
 MASTER_USE_GTID=current_pos,
 MASTER_LOG_FILE="XXX", 
 MASTER_LOG_POS=XXX;
START SLAVE;

Moving Applications to Use New Primary

You should now point your applications to use the new primary. If you are using the MariaDB MaxScale proxy, then you don't have to do this step as MaxScale will take care of sending write request to the new primary.

See Also

  • CHANGE MASTER TO command

  • MaxScale Blog about using Switchover to swap a primary and replica

  • Percona blog about how to upgrade replica to primary

This page is licensed: CC BY-SA / Gnu FDL

Replication When the Primary and Replica Have Different Table Definitions

Learn about replication challenges when primary & replica servers have differing table definitions. The section explores strategies to manage inconsistencies & ensure data integrity in such scenarios.

The terms master and slave have historically been used in replication, and MariaDB has begun the process of adding primary and replica synonyms. The old terms will continue to be used to maintain backward compatibility - see MDEV-18777 to follow progress on this effort.

While replication is usually meant to take place between primaries and replicas with the same table definitions and this is recommended, in certain cases replication can still take place even if the definitions are identical.

Tables on the replica and the primary do not need to have the same definition in order for replication to take place. There can be differing numbers of columns, or differing data definitions and, in certain cases, replication can still proceed.

Different Column Definitions - Attribute Promotion and Demotion

It is possible in some cases to replicate to a replica that has a column of a different type on the replica and the primary. This process is called attribute promotion (to a larger type) or attribute demotion (to a smaller type).

The conditions differ depending on whether statement-based or row-based replication is used.

Statement-Based Replication

When using statement-based replication, generally, if a statement can run successfully on the replica, it will be replicated. If a column definition is the same or a larger type on the replica than on the primary, it can replicate successfully. For example a column defined as VARCHAR(10) will successfully be replicated on a replica with a definition of VARCHAR(12).

Replicating to a replica where the column is defined as smaller than on the primary can also work. For example, given the following table definitions:

Master:

DESC r;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | tinyint(4)  | YES  |     | NULL    |       |
| v     | varchar(10) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+

Slave

DESC r;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | tinyint(4)  | YES  |     | NULL    |       |
| v     | varchar(8) | YES   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+

the statement

INSERT INTO r VALUES (6,'hi');

would successfully replicate because the value inserted into the v field can successfully be inserted on both the primary and the smaller replica equivalent.

However, the following statement would fail:

INSERT INTO r VALUES (7,'abcdefghi')

In this case, the value fits in the primary definition, but is too long for the replica field, and so replication will fail.

SHOW SLAVE STATUS\G
*************************** 1. row ***************************
...
Slave_IO_Running: Yes
Slave_SQL_Running: No
...
Last_Errno: 1406
Last_Error: Error 'Data too long for column 'v' at row 1' on query. 
   Default database: 'test'. Query: 'INSERT INTO r VALUES (7,'abcdefghi')'
...

Row-Based Replication

When using row-based replication, the value of the slave_type_conversions variable is important. The default value of this variable is empty, in which case MariaDB will not perform attribute promotion or demotion. If the column definitions do not match, replication will stop. If set to ALL_NON_LOSSY, safe replication is permitted. If set to ALL_LOSSY as well, replication will be permitted even if data loss takes place.

For example:

Master:

DESC r;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | smallint(6) | YES  |     | NULL    |       |
| v     | varchar(10) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+

Slave:

SHOW VARIABLES LIKE 'slave_ty%';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| slave_type_conversions |       |
+------------------------+-------+

 DESC r;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id    | tinyint(4) | YES  |     | NULL    |       |
| v     | varchar(1) | YES  |     | NULL    |       |
+-------+------------+------+-----+---------+-------+

The following query will fail:

INSERT INTO r VALUES (3,'c');
SHOW SLAVE STATUS\G;
...
Slave_IO_Running: Yes
Slave_SQL_Running: No
...
Last_Errno: 1677
Last_Error: Column 0 of table 'test.r' cannot be converted from 
  type 'smallint' to type 'tinyint(4)'
...

By changing the value of the slave_type_conversions, replication can proceed:

SET GLOBAL slave_type_conversions='ALL_NON_LOSSY,ALL_LOSSY';

START SLAVE;
SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
...
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
...

Supported Conversions

  • Between TINYINT, SMALLINT, MEDIUMINT, INT and BIGINT. If lossy conversion is supported, the value from the primary will be converted to the maximum or minimum permitted on the replica, which non-lossy conversions require the replica column to be large enough. For example, SMALLINT UNSIGNED can be converted to MEDIUMINT, but not SMALLINT SIGNED.

Different Number or Order of Columns

Replication can also take place when the primary and replica have a different number of columns if the following criteria are met:

  • columns must be in the same order on the primary and replica

  • common columns must be defined with the same data type

  • extra columns must be defined after the common columns

Row-Based

The following example replicates incorrectly (replication proceeds, but the data is corrupted), as the columns are not in the same order.

Master:

CREATE OR REPLACE TABLE r (i1 INT, i2 INT);

Slave:

ALTER TABLE r ADD i3 INT AFTER i1;

Master:

INSERT INTO r (i1,i2) VALUES (1,1);

SELECT * FROM r;
+------+------+
| i1   | i2   |
+------+------+
|    1 |    1 |
+------+------+

Slave:

SELECT * FROM r;
+------+------+------+
| i1   | i3   | i2   |
+------+------+------+
|    1 |    1 | NULL |
+------+------+------+

Statement-Based

Using statement-based replication, the same example may work, even though the columns are not in the same order.

Master:

CREATE OR REPLACE TABLE r (i1 INT, i2 INT);

Slave:

ALTER TABLE r ADD i3 INT AFTER i1;

Master:

INSERT INTO r (i1,i2) VALUES (1,1);

SELECT * FROM r;
+------+------+
| i1   | i2   |
+------+------+
|    1 |    1 |
+------+------+

Slave:

SELECT * FROM r;
+------+------+------+
| i1   | i3   | i2   |
+------+------+------+
|    1 | NULL |    1 |
+------+------+------+

This page is licensed: CC BY-SA / Gnu FDL

Enhancements for START TRANSACTION WITH CONSISTENT SNAPSHOT

Explore enhancements for START TRANSACTION WITH CONSISTENT SNAPSHOT. This section details how these improvements aid in achieving consistent backups and replication in highly active environments.

With the introduction of group commit, MariaDB also introduced an enhanced storage engine API for COMMIT that allows engines to coordinate commit ordering and visibility with each other and with the binary log.

With these improvements, the START TRANSACTION WITH CONSISTENT SNAPSHOT statement was enhanced to ensure consistency between storage engines that support the new API. At the time of writing, the supporting engines are XtraDB and PBXT. In addition, the binary log, while not a storage engine as such, also supports the new API and can provide a binlog position consistent with storage engine transaction snapshots.

This means that with transaction isolation level at least REPEATABLE READ, theSTART TRANSACTION WITH CONSISTENT SNAPSHOT statement can be used to ensure that queries will see a transaction-consistent view of the database also between storage engines. It is then not possible for a query to see the changes from some transaction T in XtraDB tables without also seeing the changes T makes to PBXT tables. (Within a single transactional storage engine like XtraDB or PBXT, consistency is always guaranteed even without using START TRANSACTION WITH CONSISTENT SNAPSHOT).

For example, suppose the following two transactions run in parallel:

Transaction T1:

BEGIN;
    SET @t = NOW();
    UPDATE xtradb_table SET a= @t WHERE id = 5;
    UPDATE pbxt_table SET b= @t WHERE id = 5;
    COMMIT;

Transaction T2:

SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
    START TRANSACTION WITH CONSISTENT SNAPSHOT;
    SELECT t1.a, t2.b
      FROM xtradb_table t1 INNER JOIN pbxt_table t2 ON t1.id=t2.id
     WHERE t1.id = 5;

Then transaction T2 will always see the same value for xtradb_table.a andpbxt_table.b.

(In MariaDB 5.2 and earlier, and MySQL at least up to 5.5, START TRANSACTION WITH CONSISTENT SNAPSHOT did not give any guarantees of consistency between different storage engines. So it is possible, even with a "consistent" snapshot, to see the changes in a transaction only to InnoDB/XtraDB tables, not PBXT tables, for example.)

Status Variables

Another use for these enhancements is to obtain a binary log position that is consistent with a particular transactional state of the storage engine(s) in the database. This is done with two status variables for the binary log: binlog_snapshot_file and binlog_snapshot_position

These variables give the binary log file and position like SHOW MASTER STATUS does. But they can be queried in a transactionally consistent way. After starting a transaction using START TRANSACTION WITH CONSISTENT SNAPSHOT, the two variables will give the binlog position corresponding to the state of the database of the consistent snapshot so taken, irrespectively of which other transactions have been committed since the snapshot was taken (SHOW MASTER STATUS always shows the position of the last committed transaction). This works for MVCC storage engines that implement the commit ordering part of the storage engine API, which at the time of writing is XtraDB and PBXT.

This is useful to obtain a logical dump/backup with a matching binlog position that can be used to provision a new slave with the original server as the master. First START TRANSACTION WITH CONSISTENT SNAPSHOT is executed. Then a consistent state of the database is obtained with queries, and the matching binlog position is obtained with SHOW STATUS LIKE 'binlog_snapshot%'. When this is loaded on a new slave server, the binlog position is used in a CHANGE MASTER TO statement to set the slave replicating from the correct position.

With the variables binlog_snapshot_file and binlog_snapshot_position, such provisioning can be done fully non-blocking on the master. Without them, it is necessary to get the binlog position under FLUSH TABLES WITH READ LOCK; this can potentially stall the server for a long time, as it blocks new queries until all updates that have already started have finished.

mariadb-dump

The mariadb-dump program was extended to use these status variables. This means that a backup suitable for provisioning a replica can be obtained as normal like this:

mariadb-dump --single-transaction --master-data ...

The dump will be fully non-blocking if both the mariadb-dump program and the queried server include the necessary feature (eg. both are from MariaDB 5.2-rpl, 5.3, or higher). In other cases, it will fall back to the old blocking method using FLUSH TABLES WITH READ LOCK.

For more information on the design and implementation of this feature, see MWL#136.

See Also

  • START TRANSACTION

  • What is MariaDB 5.3

  • MyRocks and START TRANSACTION WITH CONSISTENT SNAPSHOT

This page is licensed: CC BY-SA / Gnu FDL

Restricting Speed of Reading Binlog from Primary by a Replica

Control binlog reading speed from a primary by a replica. This section explains how to configure replicas to regulate the rate at which they consume binary log events, optimizing network usage.

When a replica starts after being stopped for some time, or a new replica starts that was created from a backup from some time back, a lot of old binlog events may need to be downloaded from the primary. If this happens from many replicas simultaneously, it can put a lot of load on the primary.

The read_binlog_speed_limit option can be used to reduce such load, by limiting the speed at which events are downloaded. The limit is given as maximum kilobytes per second to download on one replica connection.

With this option set, the replication I/O thread will limit the rate of download. Since the I/O thread is often much faster to download events than the SQL thread is at applying them, an appropriate value forread_binlog_speed_limit may reduce load spikes on the primary without much limit in the speed of the replica.

The option read_binlog_speed_limit is available starting from MariaDB 10.2.3.

read_binlog_speed_limit

  • Description: Maximum speed(KB/s) to read binlog from primary.

  • Commandline: --read-binlog-speed-limit[=#]

  • Scope: Global

  • Dynamic: Yes

  • Data Type: numeric

  • Default Value: 0

  • Range: 0 to 4294967295

  • Introduced: MariaDB 10.2.3

This page is licensed: CC BY-SA / Gnu FDL

Running Triggers on the Replica for Row-based Events

Learn about running triggers on MariaDB replicas for row-based events. This section explains how to configure triggers to execute on replicated data, enabling custom logic and data consistency.

The terms master and slave have historically been used in replication, and MariaDB has begun the process of adding primary and replica synonyms. The old terms will continue to be used to maintain backward compatibility - see MDEV-18777 to follow progress on this effort.

MariaDB can force the replica thread to run triggers for row-based binlog events.

The setting is controlled by the slave_run_triggers_for_rbr global variable. It can be also specified as a command-line option or in my.cnf.

Possible values are:

Value
Meaning

NO (Default)

Don't invoke triggers for row-based events

YES

Invoke triggers for row-based events, don't log their effect into the binary log

LOGGING

Invoke triggers for row-based events, and log their effect into the binary log

ENFORCE

From MariaDB 10.5.2 only. Triggers will always be run on the replica, even if there are triggers on the master. ENFORCE implies LOGGING.

Note that if you just want to use triggers together with replication, you most likely don't need this option. Read below for details.

When to Use slave_run_triggers_for_rbr

Background

Normally, MariaDB's replication system can replicate trigger actions automatically.

  • When one uses statement-based replication, the binary log contains SQL statements. Replica server(s) execute the SQL statements. Triggers are run on the master and on each replica, independently.

  • When one uses row-based replication, the binary log contains row changes. It will have both the changes made by the statement itself, and the changes made by the triggers that were invoked by the statement. Replica server(s) do not need to run triggers for row changes they are applying.

Target Usecase

One may want to have a setup where a replica has triggers that are not present on the master (Suppose the replica needs to update summary tables or perform some other ETL-like process).

If one uses statement-based replication, they can just create the required triggers on the replica. The replica will run the statements from the binary log, which will cause the triggers to be invoked.

However, there are cases where you have to use row-based replication. It could be because the master runs non-deterministic statements, or the master could be a node in a Galera cluster. In that case, you would want row-based events to invoke triggers on the replica. This is what the slave_run_triggers_for_rbr option is for. Setting the option to YES will cause the SQL replica thread to invoke triggers for row-based events; setting it to LOGGING will also cause the changes made by the triggers to be written into the binary log.

The following triggers are invoked:

  • Update_row_event runs an UPDATE trigger

  • Delete_row_event runs a DELETE trigger

  • Write_row_event runs an INSERT trigger. Additionally it runs a DELETE trigger if there was a conflicting row that had to be deleted.

Preventing Multiple Trigger Invocations

There is a basic protection against triggers being invoked both on the master and replica. If the master modifies a table that has triggers, it will produce row-based binlog events with the "triggers were invoked for this event" flag. The replica will not invoke any triggers for flagged events.

See Also

  • Task in Jira, MDEV-5095.

This page is licensed: CC BY-SA / Gnu FDL

Selectively Skipping Replication of Binlog Events

Selectively skip replication of binlog events in MariaDB Server. This section details how to bypass specific events, enabling granular control over replication and aiding in troubleshooting.

The terms master and slave have historically been used in replication, and MariaDB has begun the process of adding primary and replica synonyms. The old terms will continue to be used to maintain backward compatibility - see MDEV-18777 to follow progress on this effort.

Normally, all changes that are logged as events in the binary log are also replicated to all replicas (though still subject to filtering byreplicate-do-db, replicate-ignore-db, and similar options). However, sometimes it may be desirable to have certain events be logged into the binlog, but not be replicated to all or a subset of replicas, where the distinction between events that should be replicated or not is under the control of the application making the changes.

This could be useful if an application does some replication external to the server outside of the built-in replication, or if it has some data that should not be replicated for whatever reason.

This is possible with the following system variables.

Primary Session Variable: skip_replication

When the skip_replication variable is set to true, changes are logged into the binary log with the flag @@skip_replication set. Such events will not be replicated by replicas that run with--replicate-events-marked-for-skip set different from its default of REPLICATE.

Variable Name
Scope
Access Type
Data Type
Default Value

Variable Name

skip_replication

Scope

Session only

Access Type

Dynamic

Data Type

bool

Default Value

OFF

The skip_replication option only has effect if binary logging is enabled and sql_log_bin is true.

Attempting to change @@skip_replication in the middle of a transaction will fail; this is to avoid getting half of a transaction replicated while the other half is not replicated. Be sure to end any current transaction withCOMMIT/ROLLBACK before changing the variable.

Replica Option: --replicate-events-marked-for-skip

The replicate_events_marked_for_skip option tells the replica whether to replicate events that are marked with the @@skip_replication flag. Default is REPLICATE, to ensure that all changes are replicated to the replica. If set to FILTER_ON_SLAVE, events so marked will be skipped on the replica and not replicated. If set toFILTER_ON_MASTER, the filtering will be done on the primary, saving on network bandwidth as the events will not be received by the replica at all.

Variable Name
Scope
Access Type
Data Type
Default Value

Variable Name

replicate_events_marked_for_skip

Scope

Global

Access Type

Dynamic

Data Type

enum: REPLICATE

FILTER_ON_SLAVE

FILTER_ON_MASTER

Default Value

REPLICATE

Note: replicate_events_marked_for_skip is a dynamic variable (it can be changed without restarting the server), however the replica threads must be stopped when it is changed, otherwise an error will be thrown.

When events are filtered due to @@skip_replication, the filtering happens on the primary side; in other words, the event is never sent to the replica. If many events are filtered like this, a replica can sit a long time without receiving any events from the primary. This is not a problem in itself, but must be kept in mind when inquiring on the replica about events that are filtered. For example START SLAVE UNTIL <some position> will stop when the first event that is not filtered is encountered at the given position or beyond. If the event at the given position is filtered, then the replica thread will only stop when the next non-filtered event is encountered. In effect, if an event is filtered, to the replica it appears that it was never written to the binlog on the primary.

Note that when events are filtered for a replica, the data in the database will be different on the replica and on the primary. It is the responsibility of the application to replicate the data outside of the built-in replication or otherwise ensure consistency of operation. If this is not done, it is possible for replication to encounter, for example,UNIQUE contraint violations or other problems which will cause replication to stop and require manual intervention to fix.

The session variable @@skip_replication can be changed without requiring special privileges. This makes it possible for normal applications to control it without requiring SUPER privileges. But it must be kept in mind when using replicas with --replicate-events-marked-for-skip set different from REPLICATE, as it allows any connection to do changes that are not replicated.

skip_replication and sql_log_bin

@@sql_log_bin and @@skip_replication are somewhat related, as they can both be used to prevent a change on the primary from being replicated to the replica. The difference is that with @@skip_replication, changes are still written into the binlog, and replication of the events is only skipped on replicas that explicitly are configured to do so, with--replicate-events-marked-for-skip different fromREPLICATE. With @@sql_log_bin, events are not logged into the binlog, and so are not replicated by any replica.

skip_replication and the Binlog

When events in the binlog are marked with the @@skip_replication flag, the flag will be preserved if the events are dumped by the mariadb-binlog program and re-applied against a server with themariadb client program. Similarly, theBINLOG statement will preserve the flag from the event being replayed. And a replica which runs with--log-slave-updates and does not filter events (--replicate-events-marked-for-skip=REPLICATE) will also preserve the flag in the events logged into the binlog on the replica.

See Also

  • Using SQL_SLAVE_SKIP_COUNTER - How to skip a number of events on the replica

This page is licensed: CC BY-SA / Gnu FDL

Obsolete Replication Information

This section contains obsolete information regarding MariaDB Server standard replication. While outdated, it may offer historical context or insights for specific compatibility or migration scenarios.

LOAD DATA FROM MASTER (removed)

Syntax

LOAD DATA FROM MASTER

Description

This feature has been removed from recent versions of MariaDB.

Since the current implementation of LOAD DATA FROM MASTER and LOAD TABLE FROM MASTER is very limited, these statements are deprecated in versions 4.1 of MySQL and above. We will introduce a more advanced technique (called "online backup") in a future version. That technique will have the additional advantage of working with more storage engines.

For MySQL 5.1 and earlier, the recommended alternative solution to using LOAD DATA FROM MASTER orLOAD TABLE FROM MASTER is using mysqldump or mysqlhotcopy. The latter requires Perl and two Perl modules (DBI and DBD:mysql) and works forMyISAM and ARCHIVE tables only. With mysqldump, you can create SQL dumps on the master and pipe (or copy) these to a mysql client on the slave. This has the advantage of working for all storage engines, but can be quite slow, since it works using SELECT.

This statement takes a snapshot of the master and copies it to the slave. It updates the values of MASTER_LOG_FILE andMASTER_LOG_POS so that the slave starts replicating from the correct position. Any table and database exclusion rules specified with the--replicate-*-do-* and--replicate-*-ignore-* options are honored.--replicate-rewrite-db is not taken into account because a user could use this option to set up a non-unique mapping such as--replicate-rewrite-db="db1->db3" and--replicate-rewrite-db="db2->db3", which would confuse the slave when loading tables from the master.

Use of this statement is subject to the following conditions:

  • It works only for MyISAM tables. Attempting to load a non-MyISAM table results in the following error:ERROR 1189 (08S01): Net error reading from master

  • It acquires a global read lock on the master while taking the snapshot, which prevents updates on the master during the load operation.

If you are loading large tables, you might have to increase the values ofnet_read_timeout and net_write_timeout on both the master and slave servers. See Server System Variables.

Note that LOAD DATA FROM MASTER does not copy any tables from the mysql database. This makes it easy to have different users and privileges on the master and the slave.

To use LOAD DATA FROM MASTER, the replication account that is used to connect to the master must have the RELOAD andSUPER privileges on the master and theSELECT privilege for all master tables you want to load. All master tables for which the user does not have theSELECT privilege are ignored byLOAD DATA FROM MASTER. This is because the master hides them from the user: LOAD DATA FROM MASTER callsSHOW DATABASES to know the master databases to load, butSHOW DATABASES returns only databases for which the user has some privilege. On the slave side, the user that issues LOAD DATA FROM MASTER must have privileges for dropping and creating the databases and tables that are copied.

This page is licensed: GPLv2, originally from fill_help_tables.sql

LOAD TABLE FROM MASTER (removed)

Syntax

LOAD TABLE tbl_name FROM MASTER

Description

This feature has been removed from recent versions of MariaDB.

Since the current implementation of LOAD DATA FROM MASTER and LOAD TABLE FROM MASTER is very limited, these statements are deprecated in versions 4.1 of MySQL and above. We will introduce a more advanced technique (called "online backup") in a future version. That technique will have the additional advantage of working with more storage engines.

For MariaDB and MySQL 5.1 and earlier, the recommended alternative solution to using LOAD DATA FROM MASTER orLOAD TABLE FROM MASTER is using mysqldump or mysqlhotcopy. The latter requires Perl and two Perl modules (DBI and DBD:mysql) and works forMyISAM and ARCHIVE tables only. With mysqldump, you can create SQL dumps on the master and pipe (or copy) these to a mysql client on the slave. This has the advantage of working for all storage engines, but can be quite slow, since it works using SELECT.

Transfers a copy of the table from the master to the slave. This statement is implemented mainly debugging LOAD DATA FROM MASTER operations. To use LOAD TABLE, the account used for connecting to the master server must have the RELOAD andSUPER privileges on the master and theSELECT privilege for the master table to load. On the slave side, the user that issues LOAD TABLE FROM MASTER must have privileges for dropping and creating the table.

The conditions for LOAD DATA FROM MASTER apply here as well. For example, LOAD TABLE FROM MASTER works only for MyISAM tables. The timeout notes for LOAD DATA FROM MASTER apply as well.

This page is licensed: GPLv2, originally from fill_help_tables.sql

MariaDB 5.2 Replication Feature Preview

Note: This page is obsolete. The information is old, outdated, or otherwise currently incorrect. We are keeping the page for historical reasons only. Do not rely on the information in this article.

This page describes a "feature preview release" which previewed some replication-related features which are included in MariaDB 5.3. If you would like to try out the features mentioned here, it is recommended that you use MariaDB 5.3 (download MariaDB 5.3 here) instead of the actual release described below. Likewise, the code is available in the MariaDB 5.3 tree on Launchpad.

About this release

There has been quite a lot of interest in these features, and providing this feature preview release allows the developers to get more and earlier feedback, as well as allowing more users an early opportunity to evaluate the new features.

This feature preview release is based on MariaDB 5.2, adding a number of fairly isolated features that are considered complete and fairly well-tested. It is however not a stable or GA release, nor is it planned to be so.

The stable release including these features will be MariaDB 5.3. That being said, we greatly welcome any feedback / bug reports, and will strive to fix any issues found and we will update the feature preview until MariaDB 5.3 stable is ready.

Download/Installation

These packages are generated the same way as "official" MariaDB releases. Please see the main download pages for more detailed instructions on installation etc.

The instructions below use the mirrorftp.osuosl.org, but any of the MariaDB mirrors can be used by replacing the appropriate part of the URLs. See themain download page for what mirrors are available.

Debian/Ubuntu

For Debian and Ubuntu, it is highly recommended to install from the repositories, using apt-get, aptitude, or other favorite package managers.

First import the public key with which the repositories are signed, so that apt can verify the integrity of the packages it downloads. For example like this:

wget -O- http://ftp.osuosl.org/pub/mariadb/PublicKey | sudo apt-key add -

Now add the appropriate repository. An easy way is to create a file calledmariadb-5.2-rpl.list in /etc/apt/sources.list.d/ with contents like this for Debian:

deb http://ftp.osuosl.org/pub/mariadb/mariadb-5.2-rpl/debian squeeze main
deb-src http://ftp.osuosl.org/pub/mariadb/mariadb-5.2-rpl/debian squeeze main

Or this for Ubuntu:

deb http://ftp.osuosl.org/pub/mariadb/mariadb-5.2-rpl/ubuntu maverick main
deb-src http://ftp.osuosl.org/pub/mariadb/mariadb-5.2-rpl/ubuntu maverick main

Replace "squeeze" or "maverick" in the examples above with the appropriate distribution name. Supported are "lenny" and "squeeze" for Debian, and "hardy", "jaunty", "karmic", "lucid", and "maverick" for Ubuntu.

Now run

sudo apt-get update

The packages can now be installed with your package manager of choice, for example:

sudo apt-get install mariadb-server-5.2

(To manually download and install packages, browse the directories below- the .debs are indebian/pool/ and ubuntu/pool/, respectively.)

Generic Linux binary tarball

Generic linux binary tarballs can be downloaded here:

  • i386 (32-bit):

  • amd64 (64-bit):

Centos 5 RPMs

  • i386 (32-bit):

  • amd64 (64-bit):

Windows (32-bit)

Source tarball

Launchpad bzr branch:

  • lp:~maria-captains/maria/mariadb-5.2-rpl

New Features in the MariaDB 5.2 replication feature preview

Here is a summary of the new features included in this preview release. The headings link to more detailed information.

Group commit for the binary log

This preview release implements group commit which works when using XtraDB with the binary log enabled. (In previous MariaDB releases, and all MySQL releases at the time of writing, group commit works in InnoDB/XtraDB when the binary log is disabled, but stops working when the binary log is enabled).

Enhancements for START TRANSACTION WITH CONSISTENT SNAPSHOT

START TRANSACTION WITH CONSISTENT SNAPSHOT now also works with the binary log. This means it is possible to obtain the binlog position corresponding to a transactional snapshot of the database without blocking any other queries. This is used by mysqldump --single-transaction --master-data to do a fully non-blocking backup which can be used to provision a new slave.

START TRANSACTION WITH CONSISTENT SNAPSHOT now also works consistently between transactions involving more than one storage engine (currently XTraDB and PBXT support this).

Annotation of row-based replication events with the original SQL statement

When using row-based replication, the binary log does not contain SQL statements, only discrete single-row insert/update/delete events. This can make it harder to read mysqlbinlog output and understand where in an application a given event may have originated, complicating analysis and debugging.

This feature adds an option to include the original SQL statement as a comment in the binary log (and shown in mysqlbinlog output) for row-based replication events.

Row-based replication for tables with no primary key

This feature can improve the performance of row-based replication on tables that do not have a primary key (or other unique key), but which do have another index that can help locate rows to update or delete. With this feature, index cardinality information from ANALYZE TABLE is considered when selecting the index to use (before this feature is implemented, the first index was selected unconditionally).

PBXT consistent commit ordering

This feature implements the new commit ordering storage engine API in PBXT. With this feature, it is possible to use START TRANSACTION WITH CONSISTENT SNAPSHOT and get consistency among transactions which involve both XtraDB and InnoDB. (Without this feature, there is no such consistency guarantee. For example, even after running START TRANSACTION WITH CONSISTENT SNAPSHOT it was still possible for the InnoDB/XtraDB part of some transaction T to be visible and the PBXT part of the same transaction T to not be visible.)

Miscellaneous

  • This preview also includes a small change to make mysqlbinlog omit redundant use statements around BEGIN, SAVEPOINT, COMMIT, and ROLLBACK events when reading MySQL 5.0 binlogs.

  • The preview included a feature--innodb-release-locks-early. However we decided to omit this feature from future MariaDB releases because of a fundamental design bug, lp:798213.

This page is licensed: CC BY-SA / Gnu FDL

XtraDB option --innodb-release-locks-early

The --innodb-release-locks-early feature (MWL#163) was included in the 5.2 replication preview. However, it was omitted from MariaDB 5.3 due to the buglp:798213.

This page is licensed: CC BY-SA / Gnu FDL