搭建副本集
1:首先创建3台虚拟机作为配置环境
IP1:192.168.101.175
IP2:192.168.101.176
IP3:192.168.101.177
2.下载MongoDB 3.4版本,没有的文件或者文件夹请自行创建。
3.编辑配置文件E:\mongodb-3.4\conf\mongo.conf
IP1:192.168.101.177
#数据库数据存放目录dbpath=E:\mongodb-3.4\data#数据库日志存放目录logpath=E:\mongodb-3.4\log\MongoDB.log#以追加的方式记录日志logappend=true#启用日志文件,默认启用journal=true #端口号 默认为27017port=27017#副本集名称replSet=rs1/192.168.101.176:27017
IP2:192.168.101.176
#数据库数据存放目录dbpath=E:\mongodb-3.4\data#数据库日志存放目录logpath=E:\mongodb-3.4\log\MongoDB.log#以追加的方式记录日志logappend=true#启用日志文件,默认启用journal=true #端口号 默认为27017port=27017#副本集名称replSet=rs1/192.168.101.177:27017
IP3:192.168.101.175
#数据库数据存放目录dbpath=E:\mongodb-3.4\data#数据库日志存放目录logpath=E:\mongodb-3.4\log\MongoDB.log#以追加的方式记录日志logappend=true#启用日志文件,默认启用journal=true #端口号 默认为27017port=27017#副本集名称replSet=rs1/192.168.101.176:27017,192.168.101.177:27017
4.添加Windows服务
cmd命令切换到目录E:\mongodb-3.4\bin
输入以下命令
mongod --install -f E:\mongodb-3.4\conf\mongo.conf
启动MongoDB服务:net start MongoDB
停止MongoDB服务:net stop MongoDB
删除MongoDB服务:sc delete MongoDB
5.重启服务
依次重启192.168.101.177、192.168.101.176、192.168.101.175服务
6.搭建副本集
在IP1:192.168.101.177上启动mongod
#使用admin数据库use admin#定义副本集配置config = { _id:"rs1", members:[ {_id:0,host:"192.168.101.177:27017"}, {_id:1,host:"192.168.101.176:27017"}, {_id:2,host:"192.168.101.175:27017"} ] }#初始化副本集配置rs.initiate(config)
7.验证配置的副本集rs.status()
{ "set" : "rs1", "date" : ISODate("2018-02-01T08:12:43.079Z"), "myState" : 1, "term" : NumberLong(1), "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1517472756, 1), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1517472756, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1517472756, 1), "t" : NumberLong(1) } }, "members" : [ { "_id" : 0, "name" : "192.168.101.177:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 714, "optime" : { "ts" : Timestamp(1517472756, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-02-01T08:12:36Z"), "electionTime" : Timestamp(1517472425, 1), "electionDate" : ISODate("2018-02-01T08:07:05Z"), "configVersion" : 1, "self" : true }, { "_id" : 1, "name" : "192.168.101.176:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 348, "optime" : { "ts" : Timestamp(1517472756, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1517472756, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-02-01T08:12:36Z"), "optimeDurableDate" : ISODate("2018-02-01T08:12:36Z"), "lastHeartbeat" : ISODate("2018-02-01T08:12:42.251Z"), "lastHeartbeatRecv" : ISODate("2018-02-01T08:12:42.537Z"), "pingMs" : NumberLong(1), "syncingTo" : "192.168.101.177:27017", "configVersion" : 1 }, { "_id" : 2, "name" : "192.168.101.175:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 348, "optime" : { "ts" : Timestamp(1517472756, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1517472756, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-02-01T08:12:36Z"), "optimeDurableDate" : ISODate("2018-02-01T08:12:36Z"), "lastHeartbeat" : ISODate("2018-02-01T08:12:42.270Z"), "lastHeartbeatRecv" : ISODate("2018-02-01T08:12:42.634Z"), "pingMs" : NumberLong(1), "syncingTo" : "192.168.101.177:27017", "configVersion" : 1 } ], "ok" : 1}
8.在192.168.101.177(Primary)上读写数据
db.users.insert({_id:1,name:"mf",age:24})
9.检查副本集数据同步情况
模拟primary节点宕机的情况
可以看到**已经成了primary节点。主界面宕机导致了整个集群发生一次election,实现了failover。等**恢复了会自动成为secondary节点:
修改配置
可以随时修改副本集的配置:添加、删除、修改已有的成员。很多常用的操作都有对应的shell函数。
rs.add("host:port") => 添加一个节点
rs.remove("host:port") => 删除成员
rs.config() => 查看副本集配置
rs.reconfig(config) => 根据config配置对象,重新配置副本集
注意:重新配置副本集时, 主节点会先退化为备份节点, 以便接受新的配置。然后会恢复。这段时间,副本集中可能没有主节点,之后会恢复
连接复本集的正确姿势
引用命名空间: MongoDB.Driver.dll MongoDB.Bson.dllMongoClientSettings set = new MongoClientSettings();Listservers = new List ();servers.Add(new MongoServerAddress("192.168.101.175", 27017));servers.Add(new MongoServerAddress("192.168.101.176", 27017));servers.Add(new MongoServerAddress("192.168.101.177", 27017));set.Servers = servers;//设置副本集名称set.ReplicaSetName = "rs1";//设置超时时间为3秒set.ConnectTimeout = new TimeSpan(0, 0, 0, 3, 0);MongoClient client = new MongoClient(set);MongoServer server = client.GetServer();MongoDatabase db = server.GetDatabase("test");MongoCollection coll = db.GetCollection("test");//插入BsonDocument bd = new BsonDocument();bd.Add("name", "zhanjindong");bd.Add("age", 23);bd.Add("sex", "男D");coll.Insert(bd);//读取QueryDocument qd = new QueryDocument();qd.Add("name", "zhanjindong");qd.Add("age", 23);qd.Add("sex", "男D");BsonDocument rd = coll.FindOneAs (qd);Console.WriteLine(rd.ToString());