我试图成功的设置是拥有一个创建数据库的 Node 进程,其他服务器以安全的方式访问这些数据库。
所以我的想法是使用用户从 Node 创建数据库并通过。然后打开服务器的mongodb端口打开访问并锁定mongo admin用户。如果这个理论是好的:
- 如何使用 mongoose 创建用户,以便只有该用户才能访问数据库?
- 在
/etc/mongodb.conf
上我应该只添加bind_ip = 0.0.0.0
吗?
PS:我使用的是 Ubuntu 16:04 和最新的 Mongodb。
编辑:13/08/17
到目前为止我取得的成功是 addUser
= db.createUser({user: "admin",pwd: "admin",roles: [ { role: "userAdminAnyDatabase", db: "admin"} ]});
用于 admin 数据库,在数据库位于 --auth
下时与其连接,并尝试通过该连接创建其他数据库,如下所示。
var adminConnection = mongoose.createConnection('mongodb://admin:admin@localhost:27017/admin', {
useMongoClient: true
});
console.log(typeof adminConnection.db.executeDbAdminCommand);//function
最佳答案
Your /etc/mongod.conf YAML file will be look like this
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces put your ip in bindIp in form of Array like below
net:
port: 27017
bindIp: [127.0.0.1,84.20.57.18]
#before enabling security authorization you must add mongodb database user
security:
authorization: "enabled"
#Replication oplogsize mb set based on Main Memory of your Ubuntu Server (It will be good to set 1024 for speed of database Operation). In replSetName give your Replica set name or your Project Name Ex: smartcity
replication:
oplogSizeMB: 1024
replSetName: "smartcity"
在 node js 中如何使用 mongoose 并连接到你的 mongodb 数据库如下
var mongoose = require('mongoose');
var options = {
useMongoClient:true
};
var dbUrl = 'mongodb://<dbusername>:<dbpassword>@<db-ipaddress>:27017/<dbname>?replicaSet=<replicasetname>';//Ex:"mongodb://smartcityUser:smartcity1234@84.20.57.18:27017/smartcity?replicaSet=smartcity"
mongoose.connect(dbUrl,options);
mongoose.Promise = global.Promise;
愿我的工作解决您的问题,一切顺利
关于node.js - 使用 Mongoose 设置 Mongodb 安全服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45055445/