Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose connection to replica set

I tried to connect to MongoDB replicaSet via mongoose. I used this link.
Configuration json:

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

Mongoose connect:

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

And after launching application i got exception:

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: chat, primary server: localhost:27022.

Also I tried remove two other servers (keeping only the primary one in config json) and I saw that it knows about the secondary servers (I used log). I think it's about mongodb meta-data. But when I shutdown primary one, it finished its work (no wonder), I need it so it can use the secondary one instead.
Any ideas?

like image 865
lor1an Avatar asked Jun 24 '14 21:06

lor1an


People also ask

How does replica set connect to MongoDB?

To connect to a replica set deployment, specify the hostname and port numbers of each instance, separated by commas, and the replica set name as the value of the replicaSet parameter in the connection string. In the following example, the hostnames are host1 , host2 , and host3 , and the port numbers are all 27017 .

Do I need to close Mongoose connection?

However, there are times when you will want to close the connection. For example, if your application is shutting down, or restarting, the database connection needs to be manually closed, or if you are running a single-hit script rather than a persistent application.

How do I join two databases in MongoDB?

According to the fine manual, createConnection() can be used to connect to multiple databases. However, you need to create separate models for each connection/database: var conn = mongoose. createConnection('mongodb://localhost/testA'); var conn2 = mongoose.


1 Answers

# mongoose connect secondary replicateSet

<pre>
let url = 'mongodb://mongo01:1001,mongo02:1002/db_name?replicaSet=xxx-replica' 
// 1 

let conn = mongoose.createConnection(url, {user:'xxx', pass:'xxx', autoIndex: false, replset: {readPreference: 'secondary'}});

// 2  

let conn = mongoose.createConnection(url, {user:'xxx', pass:'xxx', autoIndex: false});
let schema =  new mongoose.Schema({},{read:'secondary'});
let model = conn.model(modelName, schema);

//3
let conn = mongoose.createConnection(url, {user:'xxx', pass:'xxx', autoIndex: false};
let schema = new mongoose.Schema({});
let model = conn.model(modelName, schema);
model.find().read('secondary').then(info => {});

</pre>
like image 186
Jack LI Avatar answered Oct 13 '22 16:10

Jack LI