First, configure the environment
1. Two CentOS server with MongoDB installed; (Installation reference http://281816327.blog.51cto.com/907015/1598270)
2. The IP of two servers is 192.168.1.112, 192.168.1.113 respectively;
3. Two server firewalls let go of the default 27017 port of MongoDB;
Second, configure the server
Both servers do the following configuration
Create a/data/db folder
Mkdir/date && mkdir/data/db
Modifying a configuration file
Vim/etc/mongodb.conf
Some of the important parameters are modified as follows:
#where to log logpath=/var/log/mongodb/mongod.loglogappend=true# fork and run in background fork=truedbpath=/data/db# Location of Pidfile pidfilepath=/var/run/mongodb/mongod.pid# Listen to local interface only. Comment out to listen on all interfaces. #bind_ip =127.0.0.1# Enable The HTTP interface (Defaults to Port 28017). httpinterface=true# in replicated MONGO databases, specify the replica set name here Replset=spock
Start MongoDB:
Mongod–f/etc/mongodb.conf–-rest
Third, configure the replica set
Enter MongoDB's shell on 192.168.1.122, test the connection 192.168.1.113 and create a config document:
[[email protected] ~]# mongo mongodb shell version: 2.6.6 connecting to: test > db= (New Mongo (" 192.168.1.113:27017 ")). Getdb (" test ") test > config={" _id ":" Spock ", ... " members ": [ ... {" _id ": 0," host ":" 192.168.1.112:27017 "}, ... {" _id ": 1," host ":" 192.168.1.113:27017 "}]} { "_id" : "Spock" , "Members" : [ { "_id" : 0, " Host " : " 192.168.1.112:27017 " }, { "_id" : 1, "Host"  : "192.168.1.113:27017" } ] }
Initialize replica set:
> rs.initiate (config) {"info": "Config now saved locally. Should come online in about a minute. "," OK ": 1}
Iv. Verification Results
Enter the MongoDB shell on the 192.168.1.112:
[[email protected] ~]# MONGO MongoDB shell version:2.6.6 connecting To:test spock:secondary>
Enter the MongoDB shell on the 192.168.1.112:
[[email protected] ~]# MONGO MongoDB shell version:2.6.7 connecting To:test spock:primary>
You can see the spock:secondary and Spock:primary keywords, indicating that the replica set configuration is complete;
This article is from the "Margin with Wish" blog, please be sure to keep this source http://281816327.blog.51cto.com/907015/1610153
"MongoDB Learning Note 31" MongoDB Configuration Replica set