Setting Up MariaDB Master-Slave Replication

Master-slave replication ensures high availability and distributes read queries efficiently. Let’s dive into setting up MariaDB master-slave replication step-by-step.

Prerequisites:

  • Two servers: a master (master_server_ip) and a slave (slave_server_ip).
  • MariaDB installed on both servers.

1. Configuring the Master Server:

a. Adjusting the MariaDB Configuration: Open the MariaDB configuration file, often found at /etc/mysql/mariadb.conf.d/50-server.cnf:

[mysqld]
log-bin=/var/log/mysql/mysql-bin.log
server-id=1

b. Restart MariaDB:

sudo systemctl restart mariadb

c. Create Replication User:

On the master, you’ll need a user that the slave will use to connect:

CREATE USER 'replicator_user'@'%' IDENTIFIED BY 'your-password';
GRANT REPLICATION SLAVE ON *.* TO 'replicator_user'@'%';

d. Obtain Binary Log Coordinates:

You’ll need these when setting up the slave:

SHOW MASTER STATUS;

2. Configuring the Slave Server:

a. Adjusting the MariaDB Configuration:

Open the MariaDB configuration file:

[mysqld]
server-id = 2
relay-log = /var/log/mysql/mysql-relay-bin.log
log_bin = /var/log/mysql/mysql-bin.log
binlog_format = mixed
read-only = 1

b. Restart MariaDB:

sudo systemctl restart mariadb

c. Set Up Master Connection Details on Slave:

Replace the placeholders with actual data:

CHANGE MASTER TO
MASTER_HOST='master_server_ip',
MASTER_USER='replicator_user',
MASTER_PASSWORD='your-password',
MASTER_LOG_FILE='file_from_master_status',
MASTER_LOG_POS=position_from_master_status;

d. Start Slave Processes:

START SLAVE;

e. Monitor Replication Status:

To ensure everything’s running smoothly:

SHOW SLAVE STATUS\G;

Ensure that Slave_IO_Running and Slave_SQL_Running are both set to Yes.

3. Final Notes:

Keep in mind that:

  • It’s important to frequently monitor the replication status.
  • If there’s an issue, the SHOW SLAVE STATUS command will typically provide info on what’s wrong.
  • Adjust firewalls and networking as needed to allow the servers to communicate.

That wraps up the MariaDB Master-Slave replication setup. Following these steps should help ensure a smooth replication process between your master and slave servers.

Leave a Reply

Your email address will not be published. Required fields are marked *