Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Thursday, 8 February 2024

MongoDB Replication: Setup and Configuration

Setting up and configuring MongoDB replication involves several steps, including configuring the primary and secondary nodes, initiating the replication process, and monitoring the replication status. Below is a step-by-step guide to setting up and configuring MongoDB replication:


 1. Prerequisites:


- MongoDB installed on all nodes

- Connectivity between nodes (e.g., via network)

- Administrative access to MongoDB instances


 2. Configure Primary Node:


 1. Edit MongoDB configuration file:



sudo nano /etc/mongod.conf



 2. Specify replication settings:


yaml

 MongoDB replica set configuration

replication:

  replSetName: <replica_set_name>



 3. Restart MongoDB service:



sudo systemctl restart mongod



 3. Initialize Replica Set:


 1. Connect to MongoDB primary node:



mongo



 2. Initialize replica set:


javascript

rs.initiate()



 4. Add Secondary Nodes:


 1. Connect to MongoDB primary node:



mongo



 2. Add secondary nodes to the replica set:


javascript

rs.add("<secondary_node_hostname>:27017")



 3. Verify replica set status:


javascript

rs.status()



 5. Monitor Replication:


 1. Check replication lag:


javascript

rs.printSlaveReplicationInfo()



 2. Monitor replica set status:


javascript

rs.status()



 3. View replica set configuration:


javascript

rs.conf()



 Additional Configuration (Optional):


- **Arbiter Node**: Add an arbiter node for tie-breaking in elections.

- **Security Settings**: Enable authentication and set up users with appropriate privileges.

- **Monitoring**: Use MongoDB monitoring tools (e.g., MongoDB Ops Manager, MongoDB Cloud Manager) for monitoring and management.

- **Backup**: Implement backup and restore procedures for data protection.


 Example Replica Set Configuration (mongod.conf):


yaml

storage:

  dbPath: /var/lib/mongodb

  journal:

    enabled: true

systemLog:

  destination: file

  logAppend: true

  path: /var/log/mongodb/mongod.log

net:

  port: 27017

  bindIp: 0.0.0.0

replication:

  replSetName: myReplicaSet



 Example Replica Set Initialization (MongoDB Shell):


javascript

rs.initiate(

  {

    _id: "myReplicaSet",

    members: [

      { _id: 0, host : "primary.example.com:27017" },

      { _id: 1, host : "secondary1.example.com:27017" },

      { _id: 2, host : "secondary2.example.com:27017" }

    ]

  }

)



 Example Replica Set Monitoring (MongoDB Shell):


javascript

rs.status()


This setup provides a basic configuration for MongoDB replication. You can expand on this setup by adding more secondary nodes, configuring additional replica set options, and implementing security measures based on your specific requirements and environment.

No comments:

Post a Comment

Please provide your feedback in the comments section above. Please don't forget to follow.