连接mongodb的命令
mongo 127.0.0.1:40000一、创建多个mongodb数据目录文件夹
node1
node2
node3
arbiter
二、副本集启动独立的mongod
mongod --replSet myapp --dbpath=node1 --port=40000
mongod --replSet myapp --dbpath=node2 --port=40001
mongod --replSet myapp --dbpath=node3 --port=40002
mongod --replSet myapp --dbpath=arbiter --port=40003三、初始化配置
连接node1,直接使用默认的配置初始化
rs.initiate()
初始化成功返回
{
"info" : "Config now saved locally. Should come online in about a minute.",
"me" : "liu-PC:40000",
"ok" : 1
}四、添加节点
rs.add("host:port")
rs.add("host:port",{arbiterOnly:true})六、查看运行状态
rs.status()返回
{
"set" : "myapp",
"date" : ISODate("2017-10-19T09:45:19.699Z"),
"myState" : 2,
"term" : NumberLong(4),
"syncingTo" : "liu-PC:40001",
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"appliedOpTime" : {
"ts" : Timestamp(1508406311, 1),
"t" : NumberLong(4)
},
"durableOpTime" : {
"ts" : Timestamp(1508406311, 1),
"t" : NumberLong(4)
}
},
"members" : [
{
"_id" : 0,
"name" : "liu-PC:40000",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 442,
"optime" : {
"ts" : Timestamp(1508406311, 1),
"t" : NumberLong(4)
},
"optimeDate" : ISODate("2017-10-19T09:45:11Z"),
"syncingTo" : "liu-PC:40001",
"configVersion" : 4,
"self" : true
},
{
"_id" : 1,
"name" : "liu-PC:40001",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 31,
"optime" : {
"ts" : Timestamp(1508406311, 1),
"t" : NumberLong(4)
},
"optimeDurable" : {
"ts" : Timestamp(1508406311, 1),
"t" : NumberLong(4)
},
"optimeDate" : ISODate("2017-10-19T09:45:11Z"),
"optimeDurableDate" : ISODate("2017-10-19T09:45:11Z"),
"lastHeartbeat" : ISODate("2017-10-19T09:45:18.106Z"),
"lastHeartbeatRecv" : ISODate("2017-10-19T09:45:17.758Z"),
"pingMs" : NumberLong(0),
"electionTime" : Timestamp(1508406307, 1),
"electionDate" : ISODate("2017-10-19T09:45:07Z"),
"configVersion" : 4
},
{
"_id" : 2,
"name" : "liu-PC:40002",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 13,
"optime" : {
"ts" : Timestamp(1508405846, 1),
"t" : NumberLong(3)
},
"optimeDurable" : {
"ts" : Timestamp(1508405846, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2017-10-19T09:37:26Z"),
"optimeDurableDate" : ISODate("2017-10-19T09:37:26Z"),
"lastHeartbeat" : ISODate("2017-10-19T09:45:18.270Z"),
"lastHeartbeatRecv" : ISODate("2017-10-19T09:45:19.277Z"),
"pingMs" : NumberLong(0),
"configVersion" : 4
},
{
"_id" : 3,
"name" : "liu-PC:40003",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"lastHeartbeat" : ISODate("2017-10-19T09:45:13.107Z"),
"lastHeartbeatRecv" : ISODate("2017-10-19T09:45:19.621Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Couldn't get a connection within the time limit",
"configVersion" : -1
}
],
"ok" : 1
}
本文介绍了如何通过创建多个数据目录文件夹并启动独立的mongod进程来搭建MongoDB副本集。文中详细展示了初始化配置、添加节点及仲裁节点的过程,并提供了运行状态查看的示例。
1428

被折叠的 条评论
为什么被折叠?



