MongoDB's replica set configuration

Source: Internet
Author: User



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 a database directory with a 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/

 







II: Create, 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







Three: Start up MongoDB , observing MongoDB replica set status.



start in sequence Server1,server2 of the MongoDB:



#/usr/local/mongodb/bin/mongod–f/usr/local/mongodb/mongodb.conf


View the log and discover that the replica set could not be connected:



#tail –f /usr/local/mongodb/logs/m1.log
Mon Jul 29 16:48:53 [rsStart] replSet can‘t get local.system.replset config from self or any seed (EMPTYCONFIG)


Because the replica set is not initialized, you cannot connect, so you need to initialize the replica set.



connect mongodbwith client script on server1and initialize the replica set with the following initialization commands:



#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





can see Server 1 has been elected as "Primary" Active node, Server 20% for " Secondary "Backup node.








Four : Increase MongoDB replica set node.



then you can put Server 3 added to Cmhdbset The replica set.



Install first 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 


Open Server3 of the MongoDB:



#/usr/local/mongodb/bin/mongod–f/usr/local/mongodb/mongodb.conf


View Log Discovery Server 3 Unable to connect replica set:





this is because the replica set has not yet Server3 the information that needs to be Primary Add on Node Server3 node Information:





View Server 3 logs, now Server3 the replica set has been added with a status of Secondary:





View the replica set status and see that there is already a Server3 information, and the status is Secondary:








Five : Delete an existing node



Login Primary node, deleting Server3 node:






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





Login Server3 of the MongoDB , the discovery prompt changes to "removed":









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



Note: by default, secendary node can not read and write, need to set up Rs.slaveok () before the read operation can be performed.



Note: the Delete and add replica set node operations can only be Primary the active node. and the current active node cannot be deleted.



Note: To Add a quorum node: primary>rs.addarb ("Host:port")



The quorum node only participates in voting and does not participate in the election.












Replica set node priority configuration:



Configuring the priority of a node in a replica set makes it possible to decide which server to assume as our idea Primay node.



Set Server Priority parameters of 1 priority= 2 0 ( default = 1, range 1-100):





View the node configuration. Server 1 of Priority parameter has been set to :





when the configuration is complete, Server1 will be elected as Primary node.



If you want to configure secondary-only mode, simply set the server's priority to 0.






This article is from the Linux OPS blog, so be sure to keep this source http://icenycmh.blog.51cto.com/4077647/1677825



MongoDB's replica 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.