MongoDB's replica set configuration

Source: Internet
Author: User
Tags install mongodb



What is a MongoDB replica set:



A MongoDB replica set is a cluster of replicas consisting of a set of MongoDB servers. The cluster contains a primary master server and several secondary backup servers or Artiber election servers. Secondary synchronizes data to the primary server, enabling data backup of the servers in the cluster. When primary is down or unable to provide services, the cluster elects a new primary server again to ensure the service is healthy and the data is secure.









To configure a replica set :





Configuring the Environment :



server1:192.168.189.129:5555



server2:192.168.189.131:5555



server3:192.168.189.132:5555








One: Download, install MongoDB.

#cd / usr / local / src
 #wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.2.2.tgz
 #tar -zxvf mongodb-linux-x86_64-2.2.2.tgz
 #mv mongodb-linux-x86_64-2.2.2 / usr / local / mongodb

  Create database directory and log directory

server1:
     #mkdir -p / data / mongodb / db1 /
     #mkdir -p / usr / local / mongodb / logs /
Server2:
     #mkdir -p / data / mongodb / db2 /
     #mkdir -p / usr / local / mongodb / logs /
 



2: Create and configure mongodb.conf.

#cd / usr / local / mongodb /

Server1:
#vi mongodb.conf
port = 5555
dbpath = / data / mongodb / db1 /
logpath = /usr/local/mongodb/logs/m1.log
rest = true
fork = true
replSet = cmhdbset / 192.168.189.129: 5555,192.168.189.131: 5555 // Configure the replica set cluster
logappend = true
quiet = true
journal = true
 
Server2:
#vi mongodb.conf
port = 5555
dbpath = / data / mongodb / db2 /
logpath = /usr/local/mongodb/logs/m2.log
rest = true
fork = true
replSet = cmhdbset / 192.168.189.129: 5555,192.168.189.131: 5555
logappend = true
quiet = true
journal = true
 



3: Start mongodb and observe the status of mongodb replica set.

  Start mongodb for server1 and server2 in this order:

# / usr / local / mongodb / bin / mongod --f /usr/local/mongodb/mongodb.conf
  Check the logs and find that the replica set cannot be connected:

#tail --f /usr/local/mongodb/logs/m1.log
Mon Jul 29 16:48:53 [rsStart] replSet ca n‘t get local.system.replset config from self or any seed (EMPTYCONFIG)
 The reason is that the replica set has not been initialized, so it cannot be connected, so the replica set needs to be initialized.

       Use client script on server1 to connect to mongodb and initialize the replica set. The initialization command is as follows:

# db.runCommand ({"replSetInitiate": {"_id": "cmhdbset", "members": [{"_id": 1, "host": "192.168.189.129:5555"}, {"_id": 2 , "host": "192.168.189.131:5555"}]}})

  Operations such as:


  View replica set node status


  You can see that Server1 has been elected as the "Primary" active node, and Server2 has become the "Secondary" backup node.

 



Four: increase the mongodb replica set node.

  At this time, Server3 can be added to the cmhdbset replica set.

  First install mongodb, refer to the first step.

  Create and configure mongodb.conf:

#vi /usr/local/mongodb/mongodb.conf
port = 5555
dbpath = / home / data / mongodb / db3 /
logpath = /home/usr/local/mongodb/logs/m3.log
rest = true
fork = true
replSet = cmhdbset / 192.168.189.129: 5555,192.168.189.131: 5555 // Add existing cluster members
logappend = true
quiet = true
journal = true
 Start mongodb on server3:

# / usr / local / mongodb / bin / mongod --f /usr/local/mongodb/mongodb.conf
 Checking the logs shows that server3 cannot connect to the replica set:


  This is because there is no server3 information in the replica set. You need to add Server3 node information to the Primary node:


  Check the server3 log. Now server3 has joined the replica set and the status is Secondary:


  Check the status of the replica set, and see that server3 has information and the status is Secondary:




Five: delete existing nodes

  Log in to the Primary node and delete the Server3 node:


  Check the replica set information, Server3 node has been removed:


  Log in to mongodb on Server3 and find that the prompt changes to "removed":


 

 

======================================================= ==============================

Note: By default, the Secondary node cannot read or write. You need to set rs.slaveOk () before reading.

Note: Deleting and adding replica set nodes can only be performed on the primary active node. And the current active node cannot be deleted.

Note: Add an arbitration node: primary> rs.addArb ("host: port")

  The arbitration node only participates in voting and does not participate in elections.


 



 

Replica set node priority configuration:

Configure the priority of the nodes in the replica set, which can make us decide which one according to our ideas
The server acts as a Primay node.

   Set the priority parameter of Server1 to priority = 20 (the default is 1, the range is 1-100):


   Looking at the node configuration, the priority parameter of Server1 has been set to 20:


   After configuration, Server1 will be elected as the primary node.

       To configure Secondary-Only mode, just set the priority of the server to 0.



This article is from the "linux operation and maintenance" blog, please keep this source

MongoDB replication set configuration

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.