Files
my-docs/Databases/Mariadb/Replication.md
2025-07-28 20:26:44 +03:30

2.1 KiB

📘 MariaDB Replication Setup Guide

This guide walks you through setting up Master-Slave Replication using MariaDB.


🛠️ Step 1: Install MariaDB

Install MariaDB on both the master and slave servers.

sudo apt update
sudo apt install mariadb-server

⚙️ Step 2: Configure the Master Server

Edit the MariaDB configuration file:

sudo vim /etc/mysql/mariadb.conf.d/50-server.cnf

Add or modify the following under the [mariadb] section:

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

🔁 Also, change the bind-address to allow external connections:

bind-address = 0.0.0.0

🔐 Step 3: Create Replication User

Access the MariaDB shell:

sudo mysql

Then run:

CREATE USER 'radin'@'%' IDENTIFIED BY '123';
GRANT REPLICATION SLAVE ON *.* TO 'radin'@'%';
FLUSH PRIVILEGES;

📈 Step 4: Check Master Status

Still inside the MariaDB shell, run:

FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;

Take note of:

  • File: The binary log file name (e.g., db-master1-bin.000003)
  • Position: The log position (e.g., 347)

Then, in a separate terminal, unlock tables:

UNLOCK TABLES;

🛠️ Step 5: Configure the Slave Server

Edit the config file on the slave:

sudo vim /etc/mysql/mariadb.conf.d/50-server.cnf

Set the following:

[mariadb]
log-bin
server_id=2
log-basename=slave1
binlog-format=mixed

🔄 Step 6: Set Up Slave Replication

Access the MariaDB shell on the slave server:

sudo mysql

Run the following (replace values as needed):

CHANGE MASTER TO
  MASTER_HOST='192.168.6.160',
  MASTER_USER='radin',
  MASTER_PASSWORD='123',
  MASTER_PORT=3306,
  MASTER_LOG_FILE='db-master1-bin.000003',
  MASTER_LOG_POS=347,
  MASTER_CONNECT_RETRY=10;

Start the slave:

START SLAVE;

Check slave status:

SHOW SLAVE STATUS\G

Look for:

  • Slave_IO_Running: Yes
  • Slave_SQL_Running: Yes

🔁 Resetting the Slave

To reset the slave configuration:

STOP SLAVE;
RESET SLAVE ALL;