Mongodb Secure Server Setup with Mongoose
Your /etc/mongod.conf YAML file will be look like thisstorage: 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 belownet: port: 27017 bindIp: [127.0.0.1,84.20.57.18]#before enabling security authorization you must add mongodb database usersecurity: 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: smartcityreplication: oplogSizeMB: 1024 replSetName: "smartcity"
In node js how to use mongoose and connecting to your mongodb database as follows
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
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.