我需要帮助来修正错误.我的ubuntu服务器16.04正在运行:
MongoDBshell 版本v4.0.9
node 11.6
mongoose 5.3.4
当安全系统打开/etc/mongod时.配置文件是:
security:
authorization: "disabled"
const mongoURI = 'mongodb://writeApp:writeApp9779@127.0.0.1:27017/writeapp';
const db = mongoose.connect(mongoURI, { useNewUrlParser: true });
所有数据库命令都有效.即使我输入了错误的密码进行了判断,也显示身份验证失败.但正如您所知,mongodb仍将在禁用授权的情况下工作.
我的用户数据经db判断无误.从命令行获取用户()
> db.getUsers();
[
{
"_id" : "admin.root",
"userId" : UUID("8f6c1295-a261-4057-9a29-8a9919437841"),
"user" : "root",
"db" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
},
{
"_id" : "admin.writeApp",
"userId" : UUID("ee148506-1860-4739-80db-17352e0e2ccb"),
"user" : "writeApp",
"db" : "admin",
"roles" : [
{
"role" : "dbOwner",
"db" : "writeapp"
},
{
"role" : "listDatabases",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
]
The real problem:
{ MongoError: command find requires authentication
............
............
ok: 0,
errmsg: 'command find requires authentication',
code: 13,
codeName: 'Unauthorized',
name: 'MongoError',
[Symbol(mongoErrorContextSymbol)]: {} }
花了很长时间后,我使用mongo shell连接:
mongo -u "writeApp" -p writeApp9779 --authenticationDatabase "admin"
> use writeapp;
switched to db writeapp
> db.users.find();
{ "_id" : ObjectId("5cb63f23755c4469f8f6e2e6"),
..........
}
它在控制台中工作,但在应用程序中不工作.我找不到任何解决办法.真的需要你的体贴.顺便说一下,这里是一个示例 node .js
Category.find(function(err, categories){
if(err) console.log(err);
else {
app.locals.categories = categories;
}
});
型号:
var mongoose =require('mongoose');
var CategorySchema = mongoose.Schema({
title: {
type:String,
required:true
},
slug: {
type:String
}
});
module.exports = mongoose.model("Category", CategorySchema);