我需要帮助来修正错误.我的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);

推荐答案

1. How to connect from terminal and mongo:

安装MongoDB时,授权将被禁用.所以现在就这样吧.在管理数据库中创建根用户:

键入:mongod在一个终端中运行db,然后在另一个终端中运行命令mongo运行access mongo terminal

use admin
db.createUser(
  {
    user: "root",
    pwd: "pass123",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
  }
)
 

现在创建一个数据库:

运行use语句.如果数据库尚不存在,将创建该数据库

使用writeapp

要确认创建的数据库,必须插入一些数据,否则将无法创建.

之后,您已经创建了根用户和一个新的数据库.现在更改mongodb配置

nano /etc/mongod.conf 

将行更改为:

security:
   authorization: "enabled"

现在从终端重新启动db服务.

sudo systemctl restart mongod

判断状态:

sudo systemctl status mongod

从现在起,您需要密码才能登录mongo

mongo -u root -p pass123 --authenticationDatabase admin

现在,让我们在admin数据库中创建一个用户,但也要为特定的数据库(如writeapp)授予管理员权限

`use admin`

`db.createUser({user:"writetApp", pwd:"writeApp5299", roles:[{role:"dbOwner", db:"writeapp"}]});`

要判断用户:

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"
        }
    ],
    "mechanisms" : [
        "SCRAM-SHA-1",
        "SCRAM-SHA-256"
    ]
 }

退出mongo并登录

`mongo -u "writeApp" -p writeApp5299 --authenticationDatabase "admin"`

现在,如果您已经在其他数据库而不是admin中创建了用户,那么--authenticationDatabase "yourdbname"

现在执行来自mongo的命令.

 `使用writeapp`

 ` db.testcollection.find();`

2. How to connect from node.js:

程序1:

const mongoURI = 'mongodb://writeApp:writeApp5299@127.0.0.1:27017/writeapp';

const db = mongoose.connect(mongoURI, { useNewUrlParser: true });

程序2:

mongoose.connect('mongodb://writeApp:writeApp5299@127.0.0.1:27017/writeapp?auththSource=writeapp&w=1',{ useNewUrlParser: true });

无论你是否使用authSource,这两种方法都有效,根本不是问题:

The Problem: server.js or app.js runs first in any node.js app or the entry point which loads all modules/middleware/routes etc.
Now if any of the route has another mongodb connect which has issue, the application error simply shows the error that I showed:

command find/insert/update requires authentication

但问题是它没有显示哪个文件有问题.因此,在每一条路由上寻找问题都很耗时.所以问题解决了

Mongodb相关问答推荐

MongoDB(Mongoose)条件判断没有像我预期的那样工作

MongoDB合并按键更新值的对象数组

为什么数组长度0被认为是Mongoose中排序中最大的数字

Spring数据MongoDB(聚合)

MongoDB 在一个聚合中包含多个组

更新值导致错误 Golang MongoDB

仅当特定字段存在于 MongoDB 中时才更新它

MongoDB聚合:如何将查找结果放入嵌套数组中?

MongoDB - 分组并找到前 N 个

如何根据_id删除文档?

.insertOne 不是函数

Mongoexport 在日期范围内使用 $gt 和 $lt 约束

使用 ObjectId.GenerateNewId() 还是离开 MongoDB 创建一个?

如何在 MongoDB Map-reduce 映射函数中使用变量

mongo - Ruby连接问题

使用 MongoDB 的 map/reduce 来分组两个字段

如何在java中删除mongodb集合中的所有文档

无法将 Mongoose 连接到 Atlas

在 Ubuntu 13.10 (saucy) 中安装 Mongodb PHP 扩展的最简单方法?

pre save和validate之间的mongoose区别?什么时候用哪一个?