我有一个带有ssl服务器验证的mongo副本集集群。我该如何用猫鼬来连接它?
const key = fs.readFileSync(<Path to file>);
mongoose.connect(dbConectionString, {
sslCA: key,
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
}).then((x) => {
console.log('connected');
}).catch((err) => {
console.log(err);
});
文件没有文件结尾,看起来像这样:
-----BEGIN CERTIFICATE-----
MrPXxLy9NPj8isOutrLD29IY2A0V4RlcIxS0L7sVOy0zD6pmzMMQMD/5ifuIX6bq
[16 more rows]
VF1talRQZJjwryXlXboCG4156MKpL201L2WWjk0rvPUZ
-----END CERTIFICATE-----
这有效(MongoDB指南针)
此设置的错误消息
connection error: MongooseError [MongooseServerSelectionError]: connection <monitor> to <server-ip>:<server-port> closed
at new MongooseServerSelectionError (D:\git_repos\WirVsVirus\Coronahelfer-Hackathon\node_modules\mongoose\lib\error\serverSelection.js:22:11)
at NativeConnection.Connection.openUri (D:\git_repos\WirVsVirus\Coronahelfer-Hackathon\node_modules\mongoose\lib\connection.js:823:32)
at Mongoose.connect (D:\git_repos\WirVsVirus\Coronahelfer-Hackathon\node_modules\mongoose\lib\index.js:333:15)
at new DBConection (D:\git_repos\WirVsVirus\Coronahelfer-Hackathon\.build\src\config\db\connection.js:13:28)
at Server.dbConfig (D:\git_repos\WirVsVirus\Coronahelfer-Hackathon\.build\src\server.js:31:20)
at new Server (D:\git_repos\WirVsVirus\Coronahelfer-Hackathon\.build\src\server.js:18:14)
at Function.bootstrap (D:\git_repos\WirVsVirus\Coronahelfer-Hackathon\.build\src\server.js:24:16)
at Object.<anonymous> (D:\git_repos\WirVsVirus\Coronahelfer-Hackathon\.build\src\server.js:36:23)
at Module._compile (internal/modules/cjs/loader.js:1151:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1171:10)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47 {
message: 'connection <monitor> to <server-ip>:<server-port> closed',
name: 'MongooseServerSelectionError',
reason: TopologyDescription {
type: 'Single',
setName: null,
maxSetVersion: null,
maxElectionId: null,
servers: Map(1) {
'<server-ip>:<server-port>' => [ServerDescription]
},
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null
},
[Symbol(mongoErrorContextSymbol)]: {}
}
答案 0 :(得分:0)
找到了解决方案。将ssl = true添加到您的连接字符串中
this.DB_URL = 'mongodb://user:password@ip:port/collection?authSource=admin&ssl=true';