YouTip LogoYouTip

Mongodb Replication

# MongoDB Replication (Replica Set) MongoDB replication is the process of synchronizing data across multiple servers. Replication provides redundant backups of data and stores copies of data on multiple servers, improving data availability and ensuring data security. Replication also allows you to recover data from hardware failures and service interruptions. * * * ## What is Replication? * Ensuring data security * High data availability (24*7) * Disaster recovery * No downtime maintenance (e.g., backups, rebuilding indexes, compaction) * Distributed read access to data * * * ## MongoDB Replication Principle MongoDB replication requires at least two nodes. One is the primary node, which handles client requests, and the rest are secondary nodes, which replicate data from the primary node. Common configurations for MongoDB nodes include: one primary and one secondary, or one primary and multiple secondaries. The primary node records all operations in its oplog. Secondary nodes periodically poll the primary node to fetch these operations and then apply them to their own data copies, ensuring that the data on secondary nodes remains consistent with the primary node. The MongoDB replication structure is shown below: ![Image 2: MongoDB Replication Structure Diagram](#) In the structure diagram above, clients read data from the primary node. When clients write data to the primary node, the primary node interacts with the secondary nodes to ensure data consistency. ### Replica Set Features: * A cluster of N nodes * Any node can become the primary node * All write operations occur on the primary node * Automatic failover * Automatic recovery * * * ## MongoDB Replica Set Setup In this tutorial, we will use the same MongoDB instance to perform a primary-secondary experiment. The steps are as follows: 1. Stop the currently running MongoDB server. Now we start MongoDB by specifying the --replSet option. The basic syntax for --replSet is as follows: mongod --port "PORT" --dbpath "YOUR_DB_DATA_PATH" --replSet "REPLICA_SET_INSTANCE_NAME" ### Example mongod --port 27017 --dbpath "D:set upmongodbdata" --replSet rs0 The above example will start a MongoDB instance named rs0 on port 27017. After starting, open a command prompt and connect to the MongoDB service. In the Mongo client, use the command rs.initiate() to start a new replica set. We can use rs.conf() to view the replica set configuration. To view the replica set status, use the rs.status() command. * * * ## Adding Members to a Replica Set To add members to a replica set, we need to start the mongo service on multiple servers. Enter the Mongo client and use the rs.add() method to add members to the replica set. ### Syntax The basic syntax for the rs.add() command is as follows: >rs.add(HOST_NAME:PORT) ### Example Assume you have started a Mongo service named mongod1.net on port 27017. Use the rs.add() command in the client command window to add it to the replica set, as shown below: >rs.add("mongod1.net:27017")> In MongoDB, you can only add Mongo services to the replica set through the primary node. To determine if the currently running Mongo service is the primary node, use the command db.isMaster(). MongoDB replica sets differ from traditional primary-secondary setups. In a primary-secondary setup, all services stop if the primary host goes down. In a replica set, if the primary host fails, a secondary node takes over as the primary, preventing downtime.
← Jeasyui Ref PluginsPlugins Dt Treegrid β†’