gpt4 book ai didi

MongoDB 3.0.1 复制设置

转载 作者:IT老高 更新时间:2023-10-28 13:18:58 25 4
gpt4 key购买 nike

我使用的是 MongoDB 3.0.1 版本。我正在尝试设置 mongodb 复制我们的机器。我在 septate 机器中使用了三个 mongodb 进行复制。

machine1 - master
machine2 - slave
machine3 - slave

我引用这个网址 http://docs.mongodb.org/manual/tutorial/deploy-replica-set/

我已经在 mongodb.conf 文件中为每台机器进行了配置

replSet = rs1
fork = true

我使用此命令在主机中添加了两个成员

rs.add(192.168.1.2)
rs.add(192.168.1.3)

但我在主机中插入了一个文档,但没有复制其他两台从机。我确实检查了从机抛出以下错误

> show dbs
2015-05-18T12:43:22.020+0530 E QUERY Error: listDatabases failed:{ "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" }
at Error (<anonymous>)
at Mongo.getDBs (src/mongo/shell/mongo.js:47:15)
at shellHelper.show (src/mongo/shell/utils.js:630:33)
at shellHelper (src/mongo/shell/utils.js:524:36)
at (shellhelp2):1:1 at src/mongo/shell/mongo.js:47
>
> rs.conf()
2015-05-18T12:43:38.692+0530 E QUERY Error: Could not retrieve replica set config: {
"info" : "run rs.initiate(...) if not yet done for the set",
"ok" : 0,
"errmsg" : "no replset config has been received",
"code" : 94
}
at Function.rs.conf (src/mongo/shell/utils.js:1011:11)
at (shell):1:4 at src/mongo/shell/utils.js:1011
>

请帮我解决问题。谢谢并提前。

编辑:

rs1:PRIMARY> rs.conf()
{
"_id" : "rs1",
"version" : 4,
"members" : [
{
"_id" : 0,
"host" : "analyzer-xubuntu:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slaveDelay" : 0,
"votes" : 1
},
{
"_id" : 1,
"host" : "192.168.1.31:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0.75,
"tags" : {

},
"slaveDelay" : 0,
"votes" : 1
},
{
"_id" : 2,
"host" : "192.168.1.33:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0.5,
"tags" : {

},
"slaveDelay" : 0,
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatTimeoutSecs" : 10,
"getLastErrorModes" : {

},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
}
}
}

rs1:PRIMARY> rs.status()
{
"set" : "rs1",
"date" : ISODate("2015-05-18T09:07:31.767Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "analyzer-xubuntu:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 9236,
"optime" : Timestamp(1431939509, 2),
"optimeDate" : ISODate("2015-05-18T08:58:29Z"),
"electionTime" : Timestamp(1431931054, 2),
"electionDate" : ISODate("2015-05-18T06:37:34Z"),
"configVersion" : 4,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.1.31:27017",
"health" : 1,
"state" : 0,
"stateStr" : "STARTUP",
"uptime" : 8953,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2015-05-18T09:07:29.831Z"),
"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
"pingMs" : 1,
"configVersion" : -2
},
{
"_id" : 2,
"name" : "192.168.1.33:27017",
"health" : 1,
"state" : 0,
"stateStr" : "STARTUP",
"uptime" : 8946,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2015-05-18T09:07:30.533Z"),
"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
"pingMs" : 1,
"configVersion" : -2
}
],
"ok" : 1
}

最佳答案

让我们一步一步来。

你的 MongoDB 配置没问题,剩下的工作可以在 shell 中完成。我将使用机器名称而不是 IP。

首先连接到 machine1,然后在其中运行以下命令:

> conf = {
_id: "rs1",
members:
[
{_id : 0, host : "machine1:27017"},
{_id : 1, host : "machine2:27017"},
{_id : 2, host : "machine3:27017"}
]
}
> rs.initiate(conf)

然后只需在辅助节点上运行 rs.slaveOk()secondaries 将开始复制,您将能够查询它们并查看从 primary 插入的数据。

关于MongoDB 3.0.1 复制设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30297133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com