Getting replication working involves steps on both the master server/s and steps on the slave server/s.
MariaDB 10.0 introduced replication with global transaction IDs. These have a number of benefits, and it is generally recommended to use this feature from MariaDB 10.0. The following instructions describe the old-style replication first.
- Включите binary logging. Смотрите Activating the Binary Log и Binary log formats для получения деталей.
- Задайте для master уникальный server_id. Также у всех slave должен быть указан server_id. Для этого введите число от 1 до 232-1, это число должно быть уникальным для каждого сервера группы репликации.
- 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).
Добавьте в файл my.cnf:
[mariadb] log-bin server_id=1 log-basename=master1
Выполните в командной строке SQL:
GRANT REPLICATION SLAVE ON *.* TO replication_user;
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.
- Give the slave a unique server_id. All servers, whether masters or slaves, 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 | +--------------------+----------+--------------+------------------+ | mariadb-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.
- Once the data has been copied, you can release the lock on the master by running UNLOCK TABLES.
- 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='mariadb-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
- Теперь запустите slave с помощью комманды START SLAVE:
Replicating from MySQL master to MariaDB slave
- Replicating from MySQL 5.5 to MariaDB 5.5+ should just work.
- 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 starting from MariaDB 10.0.22 and MariaDB 10.1.8. In this case MariaDB will remove the MySQL GTIDs and other unneeded events and instead adds its own GTIDs.
Use global transaction id (GTID)
MariaDB starting with 10.0
Note that MariaDB 10.0 introduced global transaction IDs (GTIDs) for replication. It is generally recommended to use (GTIDs) from MariaDB 10.0, as this 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 = current_pos
Смотрите полное описание Global Transaction ID.