Create a replica set on the MongoDB Cluster

Source: Internet
Author: User
Tags install mongodb mongo shell

Create a replica set on the MongoDB Cluster

1. Replica Set Configuration
After successfully running MongoDB on a single machine, you need to disable the mongod daemon before using the replica set to create a Mongodb cluster:

Mongod -- replSet setname -- fork

To use the setname of the replica set you set and start mongodb. Note: -- fork is not needed. It means that mongod can be executed in the background to run other programs in shell.

If you want to use these options in the configuration file, you can add them to the configuration file mongo. conf.

ReplSet = setname
Fork = true
OplogSize = 1024 # log file size, in MB. Mongod will create a file of the corresponding size when it is started. If it is set to too large, an error indicating insufficient disk space will be reported.

Then run mongod-f mongo. conf to start the process.

Other options see http://docs.mongodb.org/manual/reference/program/mongod/#cli-mongod-replica-set


2. Start and add nodes
After the configuration in 1. is enabled on each node, there are two ways to establish a replica set connection:

1) initialize the replica set and add node configuration
Log on to mongo using a client

Rs. initiate ()

Initialize the replica set. Wait for a while (mainly to create the oplog file). Then, the prompt is displayed, indicating that setname: PRIMARY> is successful. Then:

Rs. config ()

You can view the configuration status of the replica set, for example:

{
"_ Id": "clover ",
"Version": 5,
"Members ":[
{
"_ Id": 0,
"Host": "host1: 27017 ",
"ArbiterOnly": false,
"BuildIndexes": true,
"Hidden": false,
"Priority": 1,
"Tags ":{

},
"SlaveDelay": 0,
"Votes": 1
}
]...

Currently, there is only one primary node (primary). If you can access the mongo service of the other two nodes, enter:

Rs. add ("hostname: port ")

To add nodes. After successful addition, use:


[Html] view plain copy print? Rs. status ()

Rs. status () to view the copy status.


2) use the cluster configuration document to create a replica set

I have not verified this method. To use the configuration document, create a document after entering mongod shell:

Config = {
"_ Id": "setname ",
"Members ":[
{"_ Id": 0, "host": "host1: port "},
{"_ Id": 1, "host": "host2: port "},
{"_ Id": 2, "host": "host3: port "},
...
]
}

Use this document to start the replica set:

Rs. initiate (config)

To use this method, you must use mongo shell to create the configuration document first, instead of the configuration file.

3. Test replica set
First, use

Rs. isMaster ()

Get:

{
"SetName": "clover ",
"SetVersion": 7,
"Ismaster": true,
"Secondary": false,
"Hosts ":[
"Host1: 27017 ",
"Host2: 27017 ",
"Host3: 27017"
],
"Primary": "host1: 27017 ",
"Me": "host1: 27017 ",
"ElectionId": ObjectId ("5535366b872d670c6331e5bf "),
"Max bsonobjectsize": 16777216,
"MaxMessageSizeBytes": 48000000,
& Quot; maxWriteBatchSize & quot;: 1000,
"LocalTime": ISODate ("2015-04-21T05: 01: 31.370Z "),
"MaxWireVersion": 3,
"MinWireVersion": 0,
"OK": 1
}

In this way, you can view the status of the master node and replica set. It is worth mentioning that if the backup node (secondary) is set to 0 for the property hidden, then rs. isMaster () will not be able to observe this node, such as the following code:


[Html] view plain copy print? Cfg = rs. config () # obtain the current configuration
Cfg. members [2]. priority = 0 # set the priority of a node whose _ id is 2 to 0.
Cfg. members [2]. hidden = 1 # Set this node to hide, only valid when its priority is 0
Rs. reconfig (cfg) # application configuration

Cfg = rs. config () # obtain the current configuration
Cfg. members [2]. priority = 0 # set the priority of a node whose _ id is 2 to 0.
Cfg. members [2]. hidden = 1 # Set this node to hide, only valid when its priority is 0
Rs. reconfig (cfg) # Set the node with _ id 2 to be hidden, so that its status is not rs. isMaster, but it can be rs. status.

Insert a document in mongo shell:

For (I = 0; I <1000; I ++) {test. coll. insert ({count: I })}

Db. coll. count () # test whether the document is successfully inserted


1000

Then, check the status on the backup node:

New Mongo ("host2: 27017 ")
Db. coll. count ()
1000

The database on host2 is also updated.

Then, try to shut down the mongod process on host1, which may occur in actual production. Check the situation of host2 and host3:

In the bash of host1:

Sudo bin/mongod -- shutdown
Killingprocess with pid: 15910

Then log on to the mongo shell on host2 and find that the prompt has changed to: setname> primary, indicating that host2 has changed to the master node and db on host2. coll. count () returns 1000, indicating that the backup data is still in progress.

Restart mongod-f mongo. conf from the cluster configuration file. If the prompt is setname: SECONDARY>, it indicates that it is changed to a backup node. In host2, use rs. isMaster () to test the result:

"SetName": "clover ",
"SetVersion": 9,
"Ismaster": true,
"Secondary": false,
"Hosts ":[
"Host1: 27017 ",
"Host2: 27017"
],
"Passives ":[
"Host3: 27017"
],
"Primary": "host2: 27017 ",
"Me": "Maid: 27017", <

The replica set is changed to three hosts. Host2 is still the master node, and host3 is not selected as the master node because the priority is set to 0 and becomes negative (passives.

For more MongoDB tutorials, see the following:

CentOS compilation and installation of php extensions for MongoDB and mongoDB

CentOS 6 install MongoDB and server configuration using yum

Install MongoDB2.4.3 in Ubuntu 13.04

MongoDB beginners must read (both concepts and practices)

MongoDB Installation Guide for Ubunu 14.04

MongoDB authoritative Guide (The Definitive Guide) in English [PDF]

Nagios monitoring MongoDB sharded cluster service practice

Build MongoDB Service Based on CentOS 6.5 Operating System

MongoDB details: click here
MongoDB: click here

This article permanently updates the link address:

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.