How to Replicate Databases How to Replicate Databases

How to Replicate Databases: A Guide for MongoDB, MariaDB, and PostgreSQL

Reading Time: 3 minutes

Database replication is a key strategy for improving performance, availability, and fault tolerance. In this guide about how to Replicate Databases, we’ll explore how to set up replication for MongoDB, MariaDB, and PostgreSQL, three popular databases used in modern applications.

What is Database Replication?

Database replication is the process of copying data from one database server (primary) to one or more other servers (replicas). This ensures data redundancy, enhances availability, and allows load balancing for read-heavy applications.

Why Replicate Your Database?

  • High Availability: Ensures continuous access even if a server fails.
  • Improved Performance: Distributes read queries among multiple servers.
  • Disaster Recovery: Protects against data loss by keeping backups.
  • Load Balancing: Helps manage traffic efficiently.

Replicate Databases: MongoDB Replication

MongoDB uses Replica Sets to manage replication. A Replica Set consists of a primary node (where writes occur) and multiple secondary nodes (which copy data from the primary).

Step 1: Configure MongoDB Instances

Ensure MongoDB is installed on all servers. Each node should have a unique hostname/IP.

Step 2: Start MongoDB with Replication Enabled

Start each MongoDB instance with the --replSet option:

mongod --replSet "rs0" --port 27017 --dbpath /data/db --bind_ip 0.0.0.0

Step 3: Initiate the Replica Set

Connect to MongoDB and run:

rs.initiate({
  _id: "rs0",
  members: [
    { _id: 0, host: "server1:27017" },
    { _id: 1, host: "server2:27017" },
    { _id: 2, host: "server3:27017" }
  ]
})

This command configures three nodes, one primary and two secondaries.

Step 4: Verify Replication

Run the following to check the replica set status:

rs.status()

If successful, one node will be PRIMARY, and others will be SECONDARY.

How to Replicate mongo db

Replicate Databases: MariaDB Replication

MariaDB supports Master-Slave replication, where a master server sends updates to one or more slaves.

Step 1: Configure the Master Server

Edit the MariaDB configuration file (/etc/mysql/mariadb.conf.d/50-server.cnf) and add:

[mysqld]
server-id=1
log-bin=mysql-bin
binlog-do-db=my_database

Restart MariaDB:

sudo systemctl restart mariadb

Create a replication user:

GRANT REPLICATION SLAVE ON *.* TO 'replica_user'@'%' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;

Run the following to get master status:

SHOW MASTER STATUS;

Note the File and Position values.

Step 2: Configure the Slave Server

Edit the MariaDB config file on the slave (/etc/mysql/mariadb.conf.d/50-server.cnf):

[mysqld]
server-id=2
relay-log=relay-bin

Restart MariaDB:

sudo systemctl restart mariadb

Connect to the slave and run:

CHANGE MASTER TO
  MASTER_HOST='master_ip',
  MASTER_USER='replica_user',
  MASTER_PASSWORD='password',
  MASTER_LOG_FILE='mysql-bin.000001',
  MASTER_LOG_POS=12345;
START SLAVE;

Check the status:

SHOW SLAVE STATUS\G

If Slave_IO_Running and Slave_SQL_Running are Yes, replication is working!

Top 10 DevOps Tools You Need to Know in 2025

Replicate Databases: PostgreSQL Replication

PostgreSQL uses Streaming Replication, where a primary server streams data changes to one or more standby servers.

Step 1: Configure the Primary Server

Edit the PostgreSQL configuration file (/etc/postgresql/15/main/postgresql.conf):

wal_level = replica
max_wal_senders = 3
wal_keep_size = 128MB

Allow connections from the standby server by editing pg_hba.conf:

host replication replica_user standby_ip/32 md5

Restart PostgreSQL:

sudo systemctl restart postgresql

Create a replication user:

CREATE ROLE replica_user WITH REPLICATION PASSWORD 'password' LOGIN;

Create a replication user:

CREATE ROLE replica_user WITH REPLICATION PASSWORD 'password' LOGIN;

Step 2: Set Up the Standby Server

Stop PostgreSQL on the standby server:

sudo systemctl stop postgresql

Clone the data from the primary:

pg_basebackup -h primary_ip -U replica_user -D /var/lib/postgresql/15/main -P -R

Start PostgreSQL on the standby:

sudo systemctl start postgresql

Check replication status:

SELECT * FROM pg_stat_replication;

If the standby is receiving data, replication is working!

Conclusion

Database replication is essential for improving performance, ensuring high availability, and safeguarding against failures. However, replication is a sensitive and important process that should be handled by specialists.

Use Centerglo’s special DevOps services to manage database replication effortlessly. Our expert team ensures your application remains operational at all times, eliminating concerns about downtime or data loss.

Reference:

40%

Off Especially For You 🎁

Enter your email and receive your exclusive discount, and keep up to date on our latest products & offers!

We don’t spam! Read our privacy policy for more info.

Leave a Reply

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