NoSql——mongoDB(副本集)

前端之家收集整理的这篇文章主要介绍了NoSql——mongoDB(副本集)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

上篇博客说了MongoDB的主从复制,可以实现简单的数据库安全备份;但是面临的一个问题shi :如果主机down掉了,那么一些都玩完了,显然这是不太友好的!所以,就有了MongoDB的副本集!

一、基本概念

  在主从复制服务器的概念中,主机宕机,那么从机不会自动转换为主机。但是在副本集中,主机宕机,从机则自动升级为主机,原主机恢复后当做从机来用!


二、整体结构




三、具体实践


  4444.conf


			dbpath = D:\MongoData\03\A
			port=4444
			bind_ip=127.0.0.1
			replSet=child/127.0.0.1:5555



  5555.conf


			dbpath = D:\MongoData\03\B
			port=5555
			bind_ip=127.0.0.1
			replSet=child/127.0.0.1:6666



  6666.conf


			dbpath = D:\MongoData\03\C
			port=6666
			bind_ip=127.0.0.1
            replSet=child/127.0.0.1:4444



  start-4444.bat


			mongod.exe --config 4444.conf


  start-5555.bat


			mongod.exe --config 5555.conf


  start-6666.bat


            mongod.exe --config 6666.conf


  client-4444.bat


			mongo 127.0.0.1:4444


  client-5555.bat


			mongo 127.0.0.1:5555


  client-6666.bat


            mongo 127.0.0.1:6666


  执行脚本:

  

			use admin
			db.runCommand({
				"replSetInitiate":{
					"_id":'child',"members":[
						{
							"_id":1,"host":"127.0.0.1:4444"
						},{
							"_id":2,"host":"127.0.0.1:5555"
						},{
							"_id":3,"host":"127.0.0.1:6666"
						}
					]
				}
			})<pre name="code" class="plain">			
 


   Rs.status()查看副本集状态


			child:PRIMARY> rs.status()
			{
			        "set" : "child","date" : ISODate("2015-07-10T03:33:11Z"),"myState" : 1,"members" : [
			                {
			                        "_id" : 1,"name" : "127.0.0.1:4444","health" : 0,"state" : 8,"stateStr" : "(not reachable/healthy)","uptime" : 0,"optime" : Timestamp(1436498327,10),"optimeDate" : ISODate("2015-07-10T03:18:47Z"),"lastHeartbeat" : ISODate("2015-07-10T03:33:07Z"),"lastHeartbeatRecv" : ISODate("2015-07-10T03:24:21Z"),"pingMs" : 0
			                },{
			                        "_id" : 2,"name" : "127.0.0.1:5555","health" : 1,"state" : 1,"stateStr" : "PRIMARY","uptime" : 1069,"electionTime" : Timestamp(1436498665,1),"electionDate" : ISODate("2015-07-10T03:24:25Z"),"self" : true
			                },{
			                        "_id" : 3,"name" : "127.0.0.1:6666","state" : 2,"stateStr" : "SECONDARY","uptime" : 996,"lastHeartbeat" : ISODate("2015-07-10T03:33:10Z"),"lastHeartbeatRecv" : ISODate("2015-07-10T03:33:09Z"),"pingMs" : 0,"syncingTo" : "127.0.0.1:5555"
			                }
			        ],"ok" : 1
			}


  注意:三个服务必须形成闭环系统,A→B,B→C,C→A

 至此,MongoDB的副本集整合完毕,当主服务器down掉之后,会从副本中“推举”出来一个作为服务器进行服务!当然,MongoDB提供的集群整合不仅仅包括主从复制,和副本集,还提拱了分片的功能,这个放到下篇说吧!


(未完待续,下篇说MongoDB的分片)

原文链接:https://www.f2er.com/nosql/203847.html

猜你在找的NoSQL相关文章