MongoDB Replica set deployment

Source: Internet
Author: User
Single-node deployment

The premise of this section is that you have already downloaded and installed the MongoDB and are able to function properly. Run a replica set on a single node and can only be used as a development and test and not as a production environment.

Let's build a replica set with Rs0 as the name. The configuration environment is Ubuntu 12.10 and MongoDB 2.4.

steps:

1. create three folders with the following command:

Mkdir-p/srv/mongodb/rs0-0/srv/mongodb/rs0-1/srv/mongodb/rs0-2

2. start three mongod processes, note that the port number cannot be the same and is not occupied. You can use three terminals to connect to the same Linux, and then run a command at each terminal, one time to simulate the use of the three servers.

Here are the commands to start three Mongod processes:

Mongod--port 27017--dbpath/srv/mongodb/rs0-0--replset rs0--smallfiles--oplogsize 128--logpath/var/log/mongod1.log --fork
mongod--port 27018--dbpath/srv/mongodb/rs0-1--replset rs0--smallfiles--oplogsize 128--logpath/var/log/ Mongod2.log--fork
mongod--port 27019--dbpath/srv/mongodb/rs0-2--replset rs0--smallfiles--oplogsize 128-- Logpath/var/log/mongod3.log--fork

The replset parameter is followed by the replica set name;

smallfiles and oplogsize are designed to reduce the disk occupancy rate, which is used only for testing purposes and is not recommended in production environments;

The logpath parameter specifies the path of the log save;

--fork refers to running this process as a background process.

3. now start a MONGO process that connects one of the three processes that you just started:

MONGO--port 27017

4. establish a configuration object for the replica set to initialize the replica set. The contents of the object are as follows:

rsconf = {
_id: "Rs0", Members
: [
{
_id:0,
host: ' 

where hostname refers to your server name, or IP address. Because it is local, so you can use localhost.

5. Use the rs.initiate () method to initialize the replica set:

Rs.initiate (rsconf)

6. after running the previous command, you will be prompted to wait a few minutes before you can see the results. Now view the replica cluster information with rs.conf () :

Rs.conf ()

You will find only one node member, and this is the node you are currently working on.

7. Now add the remaining two mongod processes to the replica set using the rs.add () command:

Rs.add ("

Note that you want to replace the hostname with your server name. Wait a moment.

Then using rs.status () to view the current replica cluster information, you will find that there are now three replica set members. The one-node replica set is completed for deployment. You can try to insert some test data first, then turn off one of the mongod processes and simulate node failures. Then use MONGO to connect to the new master node, and then try to insert or read the information to the database to see if it can read and write normally.
Multi-node deployment

The multiple-node production environment is not quite the same as the one-node test development environment, but Mongod is running on different servers, but it is necessary to ensure that each node is able to use name resolution. In order to prevent each run Mongod to add a wide range of parameters, you can save the parameters to a file, start with --config to specify the configuration file.

Steps

1. first specify the names of the three servers and add them to the hostname file. Where the IP address and name of the server can be modified according to their actual environment.

Server 1:mdb1,10.6.12.79

Server 2:mdb2,10.6.12.80

Server 3:mdb3,10.6.12.81

Server 4:mdb4,10.6.12.82

Once configured, use the ping command to test each other to see if the server name can be resolved.

2. Edit the test file, the filename is customized, here named config.conf. The contents are as follows:

Port = 27017
#需要监听的客户端IP地址, which only accepts IP connections from this section and improves security. (Default to accept all IP requests)
BIND_IP = 10.6.12.79
dbpath =/srv/mongodb
fork = true
Replset = Rs0

3. Copy the configuration file to the other two servers using the SCP command, and start the Mongod process using the following command:

Where/src/config.conf is the path to the configuration file.

4. use MONGO to connect to one of the mongod processes and initialize the replica set with rs.initiate () :

> rs.initiate ()
{"
	Info2": "No configuration explicitly specified--making one",
	"Me": "mdb2:27017", 
   "info": "Config now saved locally.  " Should come online in about a minute. ",
	" OK ": 1
}

5. use rs.conf () to view the current situation:

> rs.conf ()
{
	"_id": "Rs0",
	"version": 1,
	"members": [
		{
			"_id": 0, "
			Host": "MD b2:27017 "
		}
	]
}

6. use rs.add () to add additional three nodes to the replica set:

> Rs.add ("mdb1:27017")
{"OK": 1}
rs0:primary> rs.add ("mdb3:27017")
{"OK": 1}

Add one more quorum node at the same time:

Rs0:primary> Rs.addarb ("mdb4:27017")
{"OK": 1}

As you can see from the above, there is no rs0:primary at first, and when you add a member, the node becomes the master node, which in fact means that the node you added has successfully joined the replica set.

This is the cluster node deployment complete. However, in practical applications, because of the need to operate back and forth on different servers, it is recommended that you use a script to start the deployment process automatically.

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.