Create a MongoDB replica set for read/write sharing

Source: Internet
Author: User
A replica set is a master-slave cluster with automatic fault recovery. The most obvious difference between a master-slave cluster and a replica set is that the replica set does not have a fixed master node. The replica set can be considered as a set.

A replica set is a master-slave cluster with automatic fault recovery. The most obvious difference between a master-slave cluster and a replica set is that the replica set does not have a fixed master node. The replica set can be considered as a set.

A replica set is a master-slave cluster with automatic fault recovery. The most obvious difference between a master-slave cluster and a replica set is that the replica set does not have a fixed master node.

You can use a replica set as a cluster. The entire cluster selects a master node and activates other nodes when it fails to work normally.

System:

CentOS 5.6 x86_64

Mongo: mongodb-linux-x86_64-2.0.6.tgz

Server1 10.0.1.121: 20001

Server2 10.0.1.212: 20001

Arbiter 10.0.1.212: 20002


Directory planning:

Separate the data directory And log list

Server1:

/Mongodb

/Mlogs

Put Application Log statistics to/usr/local/mongo

Server2:

/Mongodb

/Mlogs

/Arbiter

Put Application Log statistics to/usr/local/mongo


Deployment:

1. enable port 20001 on the firewall and disable selinux.


2. Install the mongo service on two machines respectively.

Tar xf mongodb-linux-x86_64-2.0.6.tgz-C/usr/local/mongo

3. Create a key file

Server1:

Mkdir/etc/keys

Echo "test123456"> r0

Server2:

Mkdir/etc/keys

Echo "test123456"> r1

Arbiter:

Echo "test123456"> r2

Chmod 600 r0 r1 r2

4. Start the mongo services of Server 1 and Server 2 respectively.

Server1:

/Usr/local/mongo/bin/mongod -- replSet rs1 -- keyFile/usr/local/mongo/key/r0 -- port 20001 -- fork -- dbpath =/mongodb/-- logpath/mlogs /Server1.log

-- Fork is enabled for the background

Netstat-tunlp | grep mongo check whether the port is enabled

Add to startup script/etc/rc. local

Server2:

/Usr/local/mongo/bin/mongod -- replSet rs1 -- keyFile/usr/local/mongo/key/r1 -- port 20001 -- fork -- dbpath =/mongodb/-- logpath/mlogs /Server2.log

Netstat-tunlp | grep mongo check whether the port is enabled


/Usr/local/mongo/bin/mongod -- replSet rs1 -- keyFile/usr/local/mongo/key/r2 -- port 20002 -- fork -- dbpath =/arbiter -- logpath/mlogs/ arbiter. log


Add the boot script/etc/rc. local to make the machine take effect after it is restarted.


Option:

-- Only

Copy a database on the slave node (all databases are copied by default)

-- Slavedelay

Used on slave nodes. The latency (in seconds) increases when the master node operations are applied)

-- Fastsync

The slave node is started Based on the Data snapshot of the master node. If the data directory is a data snapshot of the master node at the beginning, it is much faster to start the slave node with this option than full synchronization.

-- Autoresync

If the slave node is not synchronized with the master node, the slave node is automatically synchronized again.

-- OplogSize

Master oplog size (MB)

5. Configure and initialize Replica Sets

> Config_pongo = {

... _ Id: 'pongo', members :[

... {_ Id: 0, host: '10. 0.1.121: 100', priority: 1}, # If multiple nodes continue to increase

... {_ Id: 1, host: '10. 0.1.212: 20001 ', priority: 2 },

... {_ Id: 2, host: "10.0.1.212: 20002", arbiterOnly: true}]

...}

Rs. initiate (config_rs1); initialize Replica Sets


Note: For more information about expanding nodes and adding arbitration devices, see the following,

Type of nodes in the replica set

Standard: a regular node that stores a complete copy of data and can become an active node for voting.

Passive: stores complete data copies and participates in voting. It cannot be an active node.

Arbiter: the submitter only participates in the voting and cannot copy data or become an active node.

Each participating node (non-arbitrator) has a priority. Priority 0 is passive and cannot be an active node. If the priority is not 0, an active node is selected based on its size.

If the two values are the same, the latest data is an active node. Modify the priority key in the node configuration to configure it as a standard node or a passive node.


Note: If the configuration is successful, the master will become PRIMARY slave and will become SECONDARY.


6. Use rs. status () to view the status information of the replica set.


"Health": 1, -- 1 indicates normal; 0 indicates exception,

"State": 1, -- 1 indicates Primary; 2 indicates Secondary;

"StateStr": "PRIMARY", -- indicates that this machine is the master database

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.