我尝试成功的设置是创建一个创建数据库的节点进程,其他服务器以安全的方式访问这些数据库。
所以我的想法是用一个用户和传递从节点创建数据库。然后打开服务器mongodb端口以打开访问并锁定mongo admin用户。如果这个理论是好的:
/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" } ]});
用于管理数据库,当数据库在--auth
下并且尝试通过该连接创建其他数据库时连接它,如下所示。
var adminConnection = mongoose.createConnection('mongodb://admin:admin@localhost:27017/admin', {
useMongoClient: true
});
console.log(typeof adminConnection.db.executeDbAdminCommand);//function
答案 0 :(得分: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"
在节点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;
愿我的工作解决您的问题,并且最好的
答案 1 :(得分:0)
一般来说,我几乎做了我想要的。这是解决方案。
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.');
});
});
});
});
一般情况下,我创建带有文档的Collection来创建数据库,当我删除该Collection时,数据库会自动被删除,如果有选项不删除它,那将是解决方案的良好改进。