Create a NoSQL Distributed Database Based on MongoDb, mongodbnosql
1. Install MongoDB on Ubuntu
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org
2, create 3 virtual machines, are wzq-mongo1, wzq-mongo2, wzq-mongo3.
The IP addresses of the three VMS are as follows:
Wzq-mongo1-192.168.0.60,
Wzq-mongo2-192.168.0.64,
Wzq-mongo3-192.168.0.69
3. Edit mongodb. conf on each virtual machine.
sudo vim /etc/mongodb.conf
Change the following settings:
replSet = rs0bind_ip = 192.168.0.60
Because mongodb 3.0 is started by the host name, you sometimes need to modify the ip address corresponding to the host name in hosts.
Replace
127.0.1.1 wzq-mongo1
Change
192.168.0.60 wzq-mongo1
Restart mongodb
sudo service mongod restart
4. Start mongodb on wzq-mongo1
mongo 192.168.0.60
5. initialize replication
mongo> rs.initiate()
6. Then add members to the new rs, as shown below:
rs0:PRIMARY> rs.add("192.168.0.60:27017")rs0:PRIMARY> rs.add("192.168.0.64:27017")rs0:PRIMARY> rs.add("192.168.0.69:27017")
7. view the current status
rs0:PRIMARY> rs.status()
The following result is displayed:
rs0:PRIMARY> rs.status(){"set" : "rs0","date" : ISODate("2015-03-23T14:47:56.002Z"),"myState" : 1,"members" : [{"_id" : 0,"name" : "192.168.0.60:27017","health" : 1,"state" : 1,"stateStr" : "PRIMARY","uptime" : 2303,"optime" : Timestamp(1427121702, 1),"optimeDate" : ISODate("2015-03-23T14:41:42Z"),"electionTime" : Timestamp(1427121785, 1),"electionDate" : ISODate("2015-03-23T14:43:05Z"),"configVersion" : 89294,"self" : true},{"_id" : 1,"name" : "192.168.0.69:27017","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 290,"optime" : Timestamp(1427121702, 1),"optimeDate" : ISODate("2015-03-23T14:41:42Z"),"lastHeartbeat" : ISODate("2015-03-23T14:47:54.312Z"),"lastHeartbeatRecv" : ISODate("2015-03-23T14:47:54.059Z"),"pingMs" : 811,"configVersion" : 89294},{"_id" : 2,"name" : "192.168.0.64:27017","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 290,"optime" : Timestamp(1427121702, 1),"optimeDate" : ISODate("2015-03-23T14:41:42Z"),"lastHeartbeat" : ISODate("2015-03-23T14:47:55.770Z"),"lastHeartbeatRecv" : ISODate("2015-03-23T14:47:55.810Z"),"pingMs" : 992,"configVersion" : 89294}],"ok" : 1}
8. Test
rs0:PRIMARY> db.test.insert({name:"wang",code:"w"})
In this way, you can view the newly added results on the other two VMS.
Congratulations,
In this process, the following problems may occur:
Question 1
Failed global initialization: BadValue Invalid or no user locale set. Please ensure LANG and/or LC_* environment variables are set correctly.
Solution
$ export LC_ALL=C
Question 2
Error: error: {"$ err": "not master and slaveOk = false", "code": 13435}
Solution
rs.slaveOk()
Question 3
A node is always in the RECOVERING State.
The general solution is:
1. Delete the SECONDARY node from the master node.
rs.remove("192.168.0.64:27017")
2. disable this mongodb instance.
3. Delete the data file on the secondary node.
Delete all files under/var/lib/mongodb
4. Restart the secondary node.
5. Add the secondary node to the master node.
rs0:PRIMARY> rs.add("192.168.0.64:27017")
Question 4: modify the configuration of the replica set
If the configuration is modified in the configuration file, but the old configuration is still used when the replica set is started,
You can use the following statement to force the new configuration in mongo.
conf = rs.conf()cfg.members[0].host="192.168.0.64:27018"cfg.members[1].host="192.168.0.69:27018"cfg.members[2].host="192.168.0.60:27018"rs.reconfig(cfg,{force:true})