2015-09-25 48 views
2

我試圖用loopback連接到帶Rackspace w/SSL的MongoDB數據庫,但它不工作。它似乎連接正常;如果我輸入了錯誤的證書(有意),我收到一條錯誤消息,說「無法連接」,但是當我使用正確的證書時,沒有錯誤顯示,所以我認爲我連接正常。但是,當我試圖查詢數據庫時總是超時,任何想法發生了什麼?如何使用帶環回的SSL連接到MongoDB數據庫

我datasources.json看起來像:

"db": { 
    "name": "mongodb", 
    "url": "mongodb://username:[email protected]:port/dbName?ssl=true", 
    "debug": true, 
    "connector": "mongodb" 
    } 

我一直唸叨需要一個證書文件的事情,但是,如果在這種情況下適用不知道。

任何幫助將不勝感激!

+0

不知道如何解決這個問題拿到證書,但你可以試試在那裏添加''secure':true'。您還可以通過設置DEBUG env變量並運行您的應用程序來查看調試日誌:'DEBUG = loopback node server/server.js' –

回答

-2

使用datasources.json如下

app_db: { 
"host": "127.0.0.1", 
"port": 27017, 
"database": "test", 
"name": "app_db", 
"username": "youruser", 
"password": "yourpassword", 
"connector": "mongodb", 
"ssl":true, 
"server": { 
    "auto_reconnect": true, 
    "reconnectTries": 100, 
    "reconnectInterval": 1000, 
    "sslValidate":false, 
    "checkServerIdentity":false, 
    "sslKey":fs.readFileSync('path to key'), 
    "sslCert":fs.readFileSync('path to certificate'), 
    "sslCA":fs.readFileSync('path to CA'), 
    "sslPass":"yourpassphrase if any" 

} 

用戶名,
密碼,
auto_reconnect,
嘗試和間隔都是可選的。點擊以下鏈接
使用OpenSSL使用
https://docs.mongodb.com/manual/tutorial/configure-ssl/

0

使用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

+0

此帖已經有很長時間了。 –

+0

爲什麼你不解釋你發佈的2個鏈接中的內容,並且將鏈接移動到單獨的行而不是保持在同一行上? –

相關問題