【问题标题】:MongoDB: mongod shows that my app is not authorizedMongoDB: mongod 显示我的应用程序未授权
【发布时间】:2018-07-03 00:56:34
【问题描述】:

我已经安装了 mongodb,我的 mongodb 和 db 文件夹分别是 C:/mongoDB/bin 和 C:/data/db。我还设置了管理员用户,如上所述 https://docs.mongodb.com/manual/tutorial/enable-authentication/

现在我想通过 Express 和 Mongoose 执行需要在数据库 mApp 上读写的基本 CRUD 操作。我正在为下面的应用程序和架构提供代码。

代码有据可查,易于理解。

App.js

var express = require('express');
var app = express();

//Invoking user
var User = require('./schema.js');

//Creating an employee object by giving values to all properties
var User1 = new User({
  name: 'Anurag',
  username: 'Anurag2',
  password: 'abc',
  admin: false,
  location: 'somewhere',
  meta: {
    age: 25,
    website: 'abc.com'
  },
  createdAt: 'Jun 11 2017',
  updatedAt: 'Jun 11 2017'
}); //Remember to provide all records,otherwise document wont be saved.

//CRUD start. Creating a user document
User1.save(function(err, employ, num) {
  if (err) {
    console.log('error occurred');
  }
  console.log('saved ' + num + ' record');
  console.log('Details ' + employ);
});

/* To retrieve documents from database, you can retrieve all at
   once, or one at a time by find(), findById(), findOne() */

//To retrieve all documents
User.find({}, function(err, data) {
  if (err) {
    console.log('error occurred while retrieving all docs');
  }
  console.log(data);
});

User.findOne({
  username: 'Anurag2'
}, function(err, data) {
  if (err) {
    console.log('error in finding one document');
  }
  console.log(data);
});

User.update({
  location: 'someplace'
}, {
  location: 'anything'
}, function(err) {
  if (err) {
    console.log('error in updating');
  }
  console.log('updated');
});

//update one document
User.findOneAndUpdate({
  username: 'Anurag2'
}, {
  admin: true
}, function(err, data) {
  if (err) {
    console.log('error in finding and updating');
  }
  console.log('updated' + data);
});

//Delete a user document
User.remove({
  location: 'anywhere'
}, function(err) {
  if (err) {
    console.log('error occurred');
  }
  console.log('removed');
});

数据库架构(schema.js)

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mApp'); //myApp is the database being connected here.


//now we open a connection
var db = mongoose.connection;
db.once('open', function() {
  console.log('Connected to Database');
});
db.on('error', console.error.bind(console, 'connection error'));

//initialising a schema
var Schema = mongoose.Schema;

mongoose.Promise = require('bluebird'); //used as mpromise was showing deprecated on console.


//creating a schema
var userSchema = new Schema({
  name: String,
  username: {
    type: String,
    required: true,
    unique: true
  },
  password: {
    type: String,
    Required: true
  },
  admin: Boolean,
  location: String,
  meta: {
    age: Number,
    website: String
  },
  createdAt: Date,
  updatedAt: Date
});

//creating a model that uses this schema
var User = mongoose.model('User', userSchema);

//now we export this model
module.exports = User;

现在,我通过 admin 登录 mongo,并将 db 更改为 mApp。我通过节点运行应用程序。

mongod 控制台显示我无权对应用执行任何操作。

没有执行任何查询,我收到所有错误消息。为什么会这样?请帮我解决这个问题。

【问题讨论】:

  • 在您的问题中添加 mongod 错误的截图,以获得详细的错误
  • 同时粘贴用于在 Mongo 中创建用户凭据的命令。

标签: node.js mongodb express mongoose


【解决方案1】:

您已为您的数据库启用身份验证。

因此,您必须在连接字符串中提供相应的凭据

变化:

mongoose.connect('mongodb://localhost/mApp'); 

到

mongoose.connect('mongodb://username:password@host:port/database');

More information on mongoose documentation

【讨论】:

  • 做到了。现在 mongodb 告诉我使用身份验证凭据通过 MongoClient.connect 进行连接。试过了,没用var db=require('mongodb').Db; var MongoClient=require('mongodb').MongoClient; var mongoose=require('mongoose'); MongoClient.connect('mongodb://myUserAdmin:abc123@localhost:27017/admin'); var db1=mongoose.connection; db1.once('open',function(){ console.log('Connected to Database'); }); db1.on('error',console.error.bind(console,'connection error'));
猜你喜欢
  • 2012-07-21
  • 1970-01-01
  • 2022-11-14
  • 1970-01-01
  • 2018-02-27
  • 1970-01-01
  • 1970-01-01
  • 2012-12-24
  • 1970-01-01
相关资源
最近更新 更多