使用Mongoose进行Mongodb安全服务器设置

时间:2022-05-23 11:45:54

The setup i am trying to success is to have a node process which create databases, and other servers access those databases with a secure way.
So my idea was to create the database from node with a user and pass. Then open the server mongodb port to open access and lock the mongo admin user. If that theory is good:

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

  1. How to make user with mongoose so that database will be accessible only with that user?
  2. 如何使用mongoose的用户,以便只有该用户才能访问数据库?
  3. On the /etc/mongodb.conf should i only add bind_ip = 0.0.0.0 and that's all?
  4. 在/etc/mongodb.conf上我应该只添加bind_ip = 0.0.0.0,那就是全部?

PS: i am using Ubuntu 16:04 and latest Mongodb.

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

Edit: 13/08/17
What i have success until now is to addUser = db.createUser({user: "admin",pwd: "admin",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]}); for admin database, connect with it while database is under --auth and trying to create other database via that connection, like below.

编辑:13/08/17我到目前为止成功的是addUser = db.createUser({user:“admin”,pwd:“admin”,roles:[{role:“userAdminAnyDatabase”,db:“admin”} ]});对于管理数据库,当数据库在--auth下并且尝试通过该连接创建其他数据库时连接它,如下所示。

var adminConnection = mongoose.createConnection('mongodb://admin:admin@localhost:27017/admin', {
    useMongoClient: true
});
console.log(typeof adminConnection.db.executeDbAdminCommand);//function

2 个解决方案

#1


2  

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"

In node js how to use mongoose and connecting to your mongodb database as follows

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

May my work solve your issue and all best

愿我的工作能够最好地解决您的问题

#2


0  

Generally i almost did what i wanted. Here is the solution.

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

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.');
            });
        });
    });
});

Generally i am creating Collection with document to create database, and when i am removing that Collection, database automatically get deleted, if there will be option to not delete it, that will be good improvement for the solution.

通常我正在创建带有文档的Collection以创建数据库,当我删除该Collection时,数据库会自动被删除,如果有选项不删除它,这将是解决方案的良好改进。

#1


2  

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"

In node js how to use mongoose and connecting to your mongodb database as follows

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

May my work solve your issue and all best

愿我的工作能够最好地解决您的问题

#2


0  

Generally i almost did what i wanted. Here is the solution.

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

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.');
            });
        });
    });
});

Generally i am creating Collection with document to create database, and when i am removing that Collection, database automatically get deleted, if there will be option to not delete it, that will be good improvement for the solution.

通常我正在创建带有文档的Collection以创建数据库,当我删除该Collection时,数据库会自动被删除,如果有选项不删除它,这将是解决方案的良好改进。