我正在尝试使用环回连接到 Rackspace 中带有 SSL 的 MongoDB 数据库,但它不起作用。似乎连接正常;如果我(故意)输入错误的凭据,我会收到一条错误消息,提示“无法连接”,但是当我使用正确的凭据时,不会显示任何错误,所以我认为我连接正常。但是当我尝试查询数据库时它总是超时,知道发生了什么事吗?
我的 datasources.json 看起来像这样:
"db": {
"name": "mongodb",
"url": "mongodb://username:password@iad-mongos2.objectrocket.com:port/dbName?ssl=true",
"debug": true,
"connector": "mongodb"
}
我一直在阅读有关需要证书文件的内容,但不确定这是否适用于这种情况。
如有任何帮助,我们将不胜感激!
最佳答案
如下使用 datasources.env.js
var cfenv = require('cfenv');
var appenv = cfenv.getAppEnv();
// Within the application environment (appenv) there's a services object
var services = appenv.services;
// The services object is a map named by service so we extract the one for MongoDB
var mongodb_services = services["compose-for-mongodb"];
var credentials = mongodb_services[0].credentials;
// Within the credentials, an entry ca_certificate_base64 contains the SSL pinning key
// We convert that from a string into a Buffer entry in an array which we use when
// connecting.
var ca = [new Buffer(credentials.ca_certificate_base64, 'base64')];
var datasource = {
name: "db",
connector: "mongodb",
url:credentials.uri,
ssl: true,
sslValidate: false,
sslCA: ca
};
module.exports = {
'db': datasource
};
http://madkoding.gitlab.io/2016/08/26/loopback-mongo-ssl/ https://loopback.io/doc/en/lb3/Environment-specific-configuration.html#data-source-configuration
关于mongodb - 如何使用带 Loopback 的 SSL 连接到 MongoDB 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32774243/