跳到主要内容
CNode

mongodb开启auth,mongoose需要怎么连接?

问答
Llinkenliu发布于 10 年前最后回复 10 年前
248980

现在目前的情况是控制台和用工具连接都没有问题,但是mongoose查询数据时就会报未授权的错误。

  • 代码如下:
const connect =  ()=> {mongoose.connect('mongodb://username:[email protected]:27017/database');};
connect();
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
mongoose.connection.on('disconnected', connect);
  • 然后试了这样子 还是不行
var options = {
    db: { native_parser: true },
    server: { poolSize: 5 },
    replset: { rs_name: 'userAdmin' },
    user: xxx
    pass: xxx
}
const connect =  ()=> {mongoose.connect('mongodb://xxx:[email protected]:27017/xxx',options)};
connect();
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
mongoose.connection.on('disconnected', connect);
var options = {
    db: { native_parser: true },
    server: { poolSize: 5 },
    replset: { rs_name: 'userAdmin' },
    user: xxx
    pass: xxx
}
const connect =  ()=> {mongoose.connect('mongodb://127.0.0.1:27017/xxx',options)};
connect();
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
mongoose.connection.on('disconnected', connect);

都不行,这个要怎么破?

查看回复

回复 (2)

L
linkenliu#210 年前

@WindTrace 就可以了。感谢,磨蹭了好久。还得看看为什么要加个authSource=admin,文档连接的地方都没提到,还是我看的太不仔细了。

W
WindTrace#110 年前

使用下面的试试: mongodb://XXX:[email protected]:27017/yourdb?authSource=admin

参与回复

登录后即可参与回复。登录