使用Mongoose的Mongodb安全服务器安装

我试图成功的设置是有一个创build数据库的节点进程,其他服务器以安全的方式访问这些数据库。
所以我的想法是从一个用户和通过节点创build数据库。 然后打开服务器mongodb端口打开访问权限并lockingmongo admin用户。 如果这个理论是好的,

  1. 如何使用mongoose,使数据库将只能用该用户访问?
  2. /etc/mongodb.conf我应该只添加bind_ip = 0.0.0.0 ,这一切?

PS:我使用Ubuntu 16:04和最新的Mongodb。

编辑:13/08/17
db.createUser({user: "admin",pwd: "admin",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]}); = db.createUser({user: "admin",pwd: "admin",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]}); 对于admin数据库,当数据库在--auth下并且试图通过该连接创build其他数据库时连接它,如下所示。

 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" 

在节点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; 

愿我的工作能够解决您的问题,并一切顺利

一般来说,我几乎做了我想要的。 这是解决scheme。

 var a_conn = mongoose.createConnection('mongodb://admin:admin@localhost:27017/admin', { useMongoClient: true }); a_conn.once('open', function() { a_conn.useDb('w_one'); a_conn.otherDbs[0].db.addUser('user', 'pass', { db: 'w_one', roles: ["readWrite"] }); var Schema = mongoose.Schema({}); var Collection = a_conn.otherDbs[0].model('cool', Schema, 'cool'); var doc = new Collection({}); doc.save(function() { doc.remove(function() { var testConn = mongoose.createConnection('mongodb://user:pass@localhost:27017/w_one', { useMongoClient: true }); testConn.once('open', function() { //Collection.collection.drop('cool'); console.log('Database is ready.'); }); }); }); }); 

一般来说,我创build集合与文档创build数据库,当我删除该集合,数据库自动删除,如果有将不删除它的选项,这将是一个很好的解决scheme的改进。