我在IP为192.168.1.175
的计算机上通过以下命令设置了mongo 3.6副本集
$ sudo mongod --port 27017 --replSet "rs0" --bind_ip_all --dbpath ~/data/db/rs1 &
$ sudo mongod --port 27018 --replSet "rs0" --bind_ip_all --dbpath ~/data/db/rs2 &
$ sudo mongod --port 27019 --replSet "rs0" --bind_ip_all --dbpath ~/data/db/rs3
$ rs.initiate( {
_id : "rs0",
members: [
{ _id: 0, host: "localhost:27017" },
{ _id: 1, host: "localhost:27017" },
{ _id: 2, host: "localhost:27017" }
]
})
所以我假设,由于我正在使用--bind_ip_all
,因此我也能够从远程计算机连接此副本。
现在我正尝试使用IP为192.168.1.175
的计算机访问192.168.1.160
,以下是输出
emgda@ubuntu:~$ mongo 'mongodb://mongomachine:27017,mongomachine:27018,mongomachine:27019/?replicaSet=rs0' --quiet
rs0:PRIMARY> ^C
下面是小的Node.js代码,用于连接到相同的mongo URL,但出现错误。
const MongoClient = require("mongodb").MongoClient;
MongoClient.connect('mongodb://192.168.1.175:27018,192.168.1.175:27017,192.168.1.175:27019/emgda?replicaSet=rs0', { useUnifiedTopology: true })
.then(client => {
console.log("Connected correctly to server");
// specify db and collections
const db = client.db("emgda");
const collection = db.collection("aggregatehourly");
})
.catch(err => {
console.error(err);
});
下面是输出
{ MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
at Timeout.waitQueueMember.timer.setTimeout [as _onTimeout] (/home/emgda/Documents/mongo-connection/node_modules/mongodb/lib/core/sdam/topology.js:448:30)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
name: 'MongoServerSelectionError',
reason:
TopologyDescription {
type: 'ReplicaSetNoPrimary',
setName: 'rs0',
maxSetVersion: 1,
maxElectionId: null,
servers:
Map {
'localhost:27017' => [ServerDescription],
'localhost:27018' => [ServerDescription],
'localhost:27019' => [ServerDescription] },
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: 6 },
[Symbol(mongoErrorContextSymbol)]: {} }
在本地计算机上的mongo shell上运行命令时,我将其作为输出,
rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2020-01-24T06:15:26.540Z"),
"myState" : 1,
"term" : NumberLong(10),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1579846526, 1),
"t" : NumberLong(10)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1579846526, 1),
"t" : NumberLong(10)
},
"appliedOpTime" : {
"ts" : Timestamp(1579846526, 1),
"t" : NumberLong(10)
},
"durableOpTime" : {
"ts" : Timestamp(1579846526, 1),
"t" : NumberLong(10)
}
},
"members" : [
{
"_id" : 0,
"name" : "localhost:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 294,
"optime" : {
"ts" : Timestamp(1579846526, 1),
"t" : NumberLong(10)
},
"optimeDate" : ISODate("2020-01-24T06:15:26Z"),
"electionTime" : Timestamp(1579846244, 1),
"electionDate" : ISODate("2020-01-24T06:10:44Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "localhost:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 282,
"optime" : {
"ts" : Timestamp(1579846516, 1),
"t" : NumberLong(10)
},
"optimeDurable" : {
"ts" : Timestamp(1579846516, 1),
"t" : NumberLong(10)
},
"optimeDate" : ISODate("2020-01-24T06:15:16Z"),
"optimeDurableDate" : ISODate("2020-01-24T06:15:16Z"),
"lastHeartbeat" : ISODate("2020-01-24T06:15:24.871Z"),
"lastHeartbeatRecv" : ISODate("2020-01-24T06:15:25.718Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "localhost:27017",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "localhost:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 273,
"optime" : {
"ts" : Timestamp(1579846516, 1),
"t" : NumberLong(10)
},
"optimeDurable" : {
"ts" : Timestamp(1579846516, 1),
"t" : NumberLong(10)
},
"optimeDate" : ISODate("2020-01-24T06:15:16Z"),
"optimeDurableDate" : ISODate("2020-01-24T06:15:16Z"),
"lastHeartbeat" : ISODate("2020-01-24T06:15:24.894Z"),
"lastHeartbeatRecv" : ISODate("2020-01-24T06:15:24.702Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "localhost:27018",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1579846526, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1579846526, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
问题:
任何指导将不胜感激。
答案 0 :(得分:1)
最后,我找到了解决该问题的方法,
在我们启动副本集的同时,我们需要确保不必在成员的配置中提及localhost
(请参阅以下配置)。如果这样做,那么即使您使用命令--bind_ip 0.0.0.0,::
或--bind_all_ip
启动mongo服务器,其他远程服务器也将无法访问数据库。
以下是正确和不正确的配置。
错误
rs.initiate( {
_id : "rs0",
members: [
{ _id: 0, host: "localhost:27017" },
{ _id: 1, host: "localhost:27017" },
{ _id: 2, host: "localhost:27017" }
]
})
正确 [同一台计算机上的所有副本]
rs.initiate( {
_id : "rs0",
members: [
{ _id: 0, host: "192.168.1.175:27017" },
{ _id: 1, host: "192.168.1.175:27018" },
{ _id: 2, host: "192.168.1.175:27019" }
]
})
正确 [不同计算机上的所有副本]
rs.initiate( {
_id : "rs0",
members: [
{ _id: 0, host: "192.168.1.175:27017" },
{ _id: 1, host: "192.168.1.176:27018" },
{ _id: 2, host: "192.168.1.177:27019" }
]
})