How to deploy a replica set in a MongoDB Database

Source: Internet
Author: User
Tags mongo shell

How to deploy a replica set in a MongoDB Database

This article describes how to create three existing mongo instances without access control into a replica set containing three members.

To Deploy a Replica Set With Access Control, view Deploy Replica Set With Keyfile Access Control. If you want to deploy a mongo instance as a Replica Set, you can view Convert a Standalone to a Replica Set.

Introduction

The replica set of the three Members provides sufficient redundancy to ensure most network problems and other system failures. These replica sets are capable of coping with many distributed read operations. The replica set should contain an odd number of members, which ensures stable election.

The basic step is to start the mongo instance, turn it into a replica set Member, configure the replica set, and add the mongo instance to the replica set.

Requirements

For production deployment, make sure that the mongo instances on different machines are separated as much as possible. If the production deployment uses virtual machines, each mongo instance must be deployed on a separate host server, which provides services from different power sources and networks.

Before you deploy a replica set, you must install mongo, which will become part of the replica set. The installation method is not described in detail.

Before creating a replica set, verify that the network configurations between Members can communicate with each other.

Note Architecture

In the production environment, deploy each member of the replica set to its own machine. If possible, bind it to standard mongo port 27017. Use the bind_ip option to ensure that mongo listens for connections from applications with configured addresses.

Connectivity

Ensure the network connection between members of the replica set and the security and effectiveness of the network between clients. Consider the following:

1. Establish a virtual private network to ensure that the network topology communicates with members of a single site through the Local Area Network

2. configure access control to prevent unknown clients from connecting to the replica set

3. configure network and firewall rules so that only the default mongo port and incoming and outgoing packets from the deployment are allowed.

Finally, make sure that each member of the replica set can be accessed through a recognizable DNS or host name. You should reasonably configure the DNS name or system/etc/hosts to reflect this configuration.

Configuration

You can specify the runtime configuration of each system by saving it in the/etc/mongod. conf configuration file or corresponding location. Create a directory for saving data files before deploying mongo.

Procedure

The steps below summarize the steps to deploy replica sets without access control.

1. Use the appropriate options to start each member of the replica set

For each member, start mongo and use the replSet option to specify the replica set. You can also specify other special parameters. For more information, see the official documentation.

If the program connects more than one replica set, each replica set should have a unique name. Some drivers connect to the replica set group based on the replica set name.

The following example uses -- replSet to specify the replica set name.

mongod --replSet "rs0"

By default, mongo data is stored in/data/db. Make sure the directory exists. Otherwise, the startup fails. You can also specify the storage directory.

You can also specify the replica set name in the configuration file. Then, specify the path of the configuration file through the -- config parameter to start mongo in the configuration file.

By default, there is a configuration file in/etc/initd. conf. You can refer to this file to modify it as needed.

mongod --config 
 

In the production environment, you can configure the initialization script to manage the process.

2. Connect to the replica integrator through mongo shell

For example, connect to the local default port 27017mongo instance and run the following command:

mongo
3. initialize the replica set

Use rs. initiate () in only one replica integrator ():

rs.initiate( {   _id : "rs0",   members: [ { _id : 0, host : "mongodb0.example.net:27017" } ]})

Mongo initializes the replica set, which is composed of the current members and uses the default replica set configuration.

4. Check the configuration of the initialized replica set

Use rs. conf () to display the replica set configuration object:

rs.conf()

The replica set configuration object is similar to the following information:

{   "_id" : "rs0",   "version" : 1,   "protocolVersion" : NumberLong(1),   "members" : [      {         "_id" : 0,         "host" : "mongodb0.example.net:27017",         "arbiterOnly" : false,         "buildIndexes" : true,         "hidden" : false,         "priority" : 1,         "tags" : {         },         "slaveDelay" : NumberLong(0),         "votes" : 1      }   ],   "settings" : {      "chainingAllowed" : true,      "heartbeatIntervalMillis" : 2000,      "heartbeatTimeoutSecs" : 10,      "electionTimeoutMillis" : 10000,      "catchUpTimeoutMillis" : 2000,      "getLastErrorModes" : {      },      "getLastErrorDefaults" : {         "w" : 1,         "wtimeout" : 0      },      "replicaSetId" : ObjectId("585ab9df685f726db2c6a840")   }}
5. Add other members to the replica set

Use the rs. add () method to add other members. You must connect to primary to add members to the replica set.

In some cases, rs. add () triggers the election. If the mongo you connected becomes secondary, you need to connect to the new primary through the mongo shell to continue adding new members. Use rs. status () to confirm the primary of the replica set.

The following example adds two members:

rs.add("mongodb1.example.net")rs.add("mongodb2.example.net")

After that, you have all the functional replica sets. New replica Assembly election primary.

6. Check the replica set status

Use rs. status:

rs.status()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.