我尝试通过mongoose连接到MongoDB replicaSet。我使用了link
配置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'));
启动应用程序后,我得到了例外:
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,主服务器:localhost:27022。
此外,我尝试删除另外两台服务器(仅保留config json中的主服务器),我看到它知道了辅助服务器(我使用了日志)。我认为这是关于mongodb元数据的。但是当我关闭主要的一个时,它完成了它的工作(难怪),我需要它,所以它可以使用辅助它 有什么想法吗?
答案 0 :(得分:15)
我们用这个:
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:PW@host1:port1,host2:port2/DBNAME?replicaSet=RSNAME'
关闭Auto_reconnect
答案 1 :(得分:6)
我也遇到了麻烦。我从经验中学到的是:
仅当连接URI包含单个非群集连接(也称为单个连接字符串)时,才会调用“server”块。
仅当连接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
}
}
};
此块在localhost和生产环境中都有效。 希望它有所帮助。
答案 2 :(得分:5)
您的连接字符串可能无效。您应该为每个副本集成员提供URI:
“uri”:“mongodb:// localhost:27022 / chat,localhost:27021,localhost:27020”
您应该查看Mongoose文档中的replica set connection部分。
答案 3 :(得分:0)
# 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>