转载:https://blog.csdn.net/q1056843325/article/details/70941697

今天使用MongoDB时遇到了一些问题

 

建立数据库连接时出现了warnings

出现这个警告的原因是新版本的MongDB为了让我们创建一个安全的数据库
必须要进行验证
后来在外网找到了答案

解决方案如下:

创建管理员

use admin
db.createUser(
{
user: "userAdmin", //用户名
pwd: "123", //密码
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] //权限
}
)

重启MongoDB服务器

mongod --auth --port 27017 --dbpath <关联路径>
1
(端口默认就是27017可以不指定)

mongodb Access control is not enabled for the database 无访问控制解决方案

 

终端最后输出"[initandlisten] waiting for connections on port 27017",
启动完成

连接并认证

mongo --port 27017 -u "userAdmin" -p "123" --authenticationDatabase "admin"
1
添加额外权限用户

use test
db.createUser(
{
user: "tester",
pwd: "123",
roles: [ { role: "readWrite", db: "test" },
{ role: "read", db: "reporting" } ]
}
)
mongo --port 27017 -u "myTester" -p "xyz123" --authenticationDatabase "test"

MongoDB更新了,使用mongoose也不能简单的建立连接了
必须要添加必要参数

var mongoose = require('mongoose');
var db = mongoose.createConnection('localhost', 'test', 27017, {user: 'tester', pass: '123'});

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-15
  • 2022-12-23
  • 2021-05-31
  • 2021-08-04
猜你喜欢
  • 2022-12-23
  • 2021-12-11
  • 2021-11-27
  • 2021-09-22
  • 2021-10-19
  • 2021-08-29
  • 2022-12-23
相关资源
相似解决方案