步骤:
1、更新hosts文件
路径:C:\Windows\System32\drivers\etc
在hosts文件里添加:
127.0.0.1 mongodb1.example.net
127.0.0.1 mongodb2.example.net
2、添加文件夹
路径:D:\MongoDB\data
添加3个文件夹db26001,db26002,db26003
3、启动mongod
启动实例1
mongod --port 26001 --dbpath D:\MongoDB\data\db26001\ --replSet "rs0"
4、进入实例1
mongo --port 26001
运行语句:
rs.initiate()
返回结果:
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "CNZBNGCFTP:26001",
"ok" : 1
}
5、
运行语句:
rs.conf()
返回结果:
{
"_id" : "rs0",
"version" : 1,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "CNZBNGCFTP:26001",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5866046950b37ba72065b64d")
}
}
6、
启动实例2:
mongod --port 26002 --dbpath D:\MongoDB\data\db26002\ --replSet "rs0"
在实例1主窗口运行:
运行语句:
rs.add("mongodb1.example.net:26002")
7、
启动实例3:
mongod --port 26003 --dbpath D:\MongoDB\data\db26003\ --replSet "rs0"
在实例1主窗口运行:
运行语句:
rs.add("mongodb2.example.net:26003")
8、
运行语句:
rs.status()
{
"set" : "rs0",
"date" : ISODate("2016-12-30T06:59:20.351Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "CNZBNGCFTP:26001",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 367,
"optime" : {
"ts" : Timestamp(1483081136, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-12-30T06:58:56Z"),
"electionTime" : Timestamp(1483080809, 2),
"electionDate" : ISODate("2016-12-30T06:53:29Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 1,
"name" : "mongodb1.example.net:26002",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 83,
"optime" : {
"ts" : Timestamp(1483081136, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-12-30T06:58:56Z"),
"lastHeartbeat" : ISODate("2016-12-30T06:59:18.807Z"),
"lastHeartbeatRecv" : ISODate("2016-12-30T06:59:19.835Z"),
"pingMs" : NumberLong(1),
"syncingTo" : "CNZBNGCFTP:26001",
"configVersion" : 3
},
{
"_id" : 2,
"name" : "mongodb2.example.net:26003",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 23,
"optime" : {
"ts" : Timestamp(1483081136, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-12-30T06:58:56Z"),
"lastHeartbeat" : ISODate("2016-12-30T06:59:18.816Z"),
"lastHeartbeatRecv" : ISODate("2016-12-30T06:59:18.886Z"),
"pingMs" : NumberLong(1),
"configVersion" : 3
}
],
9、验证复制集是否连通
进入实例3(从节点):
mongo --port 26003
运行:
db.testcol123.insert({a1:111})
返回:
WriteResult({ "writeError" : { "code" : 10107, "errmsg" : "not master" } })
//从节点不能写入
--添加数据,测试是否传到从节点
进入实例1:
mongo --port 26001
运行:
db.testcol123.insert({a1:111})
返回:
WriteResult({ "nInserted" : 1 })
查看实例2,实例3
运行语句:
rs.slaveOk()
db.testcol123.find()
返回:{ "_id" : ObjectId("586606af88a8a8ddaf1dc3ad"), "a1" : 111 }
//已经传入从节点