2014-06-24 90 views
9

我試圖通過貓鼬連接到MongoDB replicaSet。我用這個link
配置JSON:
Mongoose連接到副本集

"mongoose": { 
    "uri": "mongodb://localhost:27022/chat,localhost:27021,localhost:27020", 
    "options": { 
     "replset": { "rs_name": "rs0" }, 
     "server": { 
      "poolSize": 3, 
      "socketOptions": { 
       "keepAlive": 1 
      } 
     } 
    } 
} 

貓鼬連接:

var mongoose = require('mongoose'); 
mongoose.connect(config.get('mongoose:uri'), config.get('mongoose:options')); 

和啓動應用程序後,我得到異常:

Error: host must be specified [undefined] 
at new exports.ConnectionPool (c:\Users\lor1an\Downloads\prj_chat-master\prj_chat-master\node_modules\mongodb\lib\mongodb\connection\connection_pool.js:18:11) 
at Server.connect (c:\Users\lor1an\Downloads\prj_chat-master\prj_chat-master\node_modules\mongodb\lib\mongodb\connection\server.js:335:25) 
at Db.open (c:\Users\lor1an\Downloads\prj_chat-master\prj_chat-master\node_modules\mongodb\lib\mongodb\db.js:264:23) 
at MongoStore._open_database (c:\Users\lor1an\Downloads\prj_chat-master\prj_chat-master\node_modules\connect-mongo\lib\connect-mongo.js:174:15) 
at MongoStore._get_collection (c:\Users\lor1an\Downloads\prj_chat-master\prj_chat-master\node_modules\connect-mongo\lib\connect-mongo.js:169:14) 
at MongoStore.get (c:\Users\lor1an\Downloads\prj_chat-master\prj_chat-master\node_modules\connect-mongo\lib\connect-mongo.js:213:10) 
at Object.session [as handle] (c:\Users\lor1an\Downloads\prj_chat-master\prj_chat-master\node_modules\connect\node_modules\express-session\index.js:215:11) 
at next (c:\Users\lor1an\Downloads\prj_chat-master\prj_chat-master\node_modules\connect\lib\proto.js:194:15) 
at Object.module.exports [as handle] (c:\Users\lor1an\Downloads\prj_chat-master\prj_chat-master\middleware\resExtensions.js:21:2) 
at next (c:\Users\lor1an\Downloads\prj_chat-master\prj_chat-master\node_modules\connect\lib\proto.js:194:15) 

Db的:聊天,主服務器:localhost:27022。

另外我試着刪除另外兩臺服務器(只保留配置json中的主服務器),並且我看到它知道二級服務器(我用過日誌)。我認爲這是關於mongodb元數據。但是,當我關閉主要的一個,它完成了它的工作(難怪),我需要它,所以它可以使用第二個替代。
任何想法?

回答

14

我們使用這樣的:

if(config.db.indexOf('replicaSet') > - 1) { 
    dbOptions = { 
    db: {native_parser: true}, 
    replset: { 
     auto_reconnect:false, 
     poolSize: 10, 
     socketOptions: { 
     keepAlive: 1000, 
     connectTimeoutMS: 30000 
     } 
    }, 
    server: { 
     poolSize: 5, 
     socketOptions: { 
     keepAlive: 1000, 
     connectTimeoutMS: 30000 
     } 
    } 
    }; 
} 

var db = mongoose.connect(config.db, dbOptions); 

其中

config.db = 'mongodb://USER:[email protected]:port1,host2:port2/DBNAME?replicaSet=RSNAME' 

Auto_reconnect熄滅按https://team.goodeggs.com/reconnecting-to-mongodb-when-mongoose-connect-fails-at-startup-83ca8496ca02

+1

@ lor1an正如在這個答案中指出的那樣,你的dbname應該在host:ports列表後面*因此你的連接字符串應該是:'mongodb:// localhost:27022,mongodb:// localhost :27021,mongodb:// localhost:27020/chat' – MForMarlon

+0

@MForMarlon正如我的,對吧? – malix

+0

是的。語法對於mongo連接起作用非常重要。 – MForMarlon

5

您的連接字符串可能無效。

「URI」:你應該爲每個副本集成員提供URI 「的mongodb://本地主機:27022 /聊天,本地主機:27021爲localhost:27020」

您應該檢查replica set connection節在Mongoose文檔中。

+0

我試了一下,在開始時,但沒有任何工程。( – lor1an

+0

@ user3772831你得到什麼錯誤,當您使用URI我建議? –

+0

堆棧跟蹤是和以前一樣。 – lor1an

6

我有這樣的煩惱了。我從經驗中學到的是:

僅當連接URI包含單個非羣集連接(又名單個連接字符串)時,纔會調用「服務器」塊。

僅當連接URL包含逗號分隔的連接字符串(又名複製集)列表時,纔會調用「replset」塊。

var options = { 

    db: { 
     native_parser: true 
    }, 

    // This block gets run for a non replica set connection string (eg. localhost with a single DB) 
    server: { 
     poolSize: 5, 
     reconnectTries: Number.MAX_VALUE, 
     ssl: false, 
     sslValidate: false, 
     socketOptions: { 
      keepAlive: 1000, 
      connectTimeoutMS: 30000 
     } 
    }, 

    // This block gets run when the connection string indicates a replica set (comma seperated connections) 
    replset: { 
     auto_reconnect: false, 
     poolSize: 10, 
     connectWithNoPrimary: true, 
     ssl: true, 
     sslValidate: false, 
     socketOptions: { 
      keepAlive: 1000, 
      connectTimeoutMS: 30000 
     } 
    } 
}; 

該塊在本地主機和生產環境中工作。 希望它有幫助。

+1

謝謝。你回答了我一直在琢磨的主要問題。 – ThinkingInBits

+1

該文件不是很清楚這一點 - 我發現了試驗和錯誤.. – ChrisRich

+0

是啊monodb js連接選項文件可能*真正*提高。 – UpTheCreek

相關問題