Install, Secure, Backup, Restore and Connect MongoDB database with separate user Role in Ubuntu 16.0

A lifelong learner. Love to travel. Listen to music.
Search for a command to run...

A lifelong learner. Love to travel. Listen to music.
No comments yet. Be the first to comment.
Best Practices for Path Structure, Versioning, and Error Handling

Understanding the Performance Implications of async and defer Attributes

Overview static: Default position. It does not allow setting properties like top, bottom, left, right, z-index relative: Acts the same as static. but allows positioning it relatively by putting properties like top, bottom, left, right, z-index absolu...

Overview When an element in the browser has a conflict of styles, the browser uses a set of rules to determine which style should be rendered. This set of rules is defined as CSS Selector Specificity. For instance, for an element, if we have the foll...

Master block, inline, flex, and grid to build better web layouts

Today we take a ubuntu 16.04 server. Install MongoDB server there and secure it with users of various role. We use different user to backup, restore and read write operation. Also we will cover, how to connect a authenticate database from a node app using mongoose ODM tool.
Install MongoDB (You can skip this part and follow official doc)
Run MongoDB
Enable Authentication in our database server
Create user
Backup or dump database
Restore backup or dump database
Connect authenticate database from a Node app using Mongoose ODM tool.
Step 1 can be skipped by the official doc. Step 2 , 3 and 4 are heavily inspired from Digital Ocean Blog. So you can keep an eye on that.
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
echo “deb [ arch=amd64,arm64 ] [https://repo.mongodb.org/apt/ubuntu](https://repo.mongodb.org/apt/ubuntu) xenial/mongodb-org/4.0 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org
Now we are done installing MongoDB.
sudo systemctl start mongod
mongo
sudo systemctl status mongod
sudo systemctl enable mongod
Now we are done running our MongoDB server.
Let’s consider we have a database named ‘my-database’
To create any user first we need to create a super user. The super user can create and assign role to other users to perform operation in database.
Let’s create a super user.
mongo
use admin
db.createUser(
{
user: "my-super-user",
pwd: "my-super-user-password",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
Now a super user is created.
exit
Now in your Mongo Daemon configuration file, enable authentication.
sudo nano /etc/mongod.conf
Now replace “#security” with the following two line. Here we remove comment from security. Then enable the authentication.
security:
authorization: "enabled"
sudo systemctl restart mongod
sudo systemctl status mongod
You should see the active property running.
mongo -u my-super-user -p --authenticationDatabase admin
For password, give the previously created super user password “my-super-user-password”
use admin
db.createUser(
{
user: "my-user",
pwd: "my-user-password",
roles: [ { role: "readWrite", db: "my-database" } ]
}
)
db.createUser(
{
user: "my-backup-user",
pwd: "my-backup-user-password",
roles: [{ role: "backup", db: "admin" }]
}
)
Now we have two users, one for read-write and database restoring authorized. Another user can only take database backup.
To backup or dump the database, we have to use the user with ‘backup’ role.
mongodump --username my-backup-user --password my-backup-user-password
In order to restore the database, we have to use a database user with read-write privilege. First go to the directory, that contains dump database.
mongorestore --username my-user --password my-user-password
Now your database will be restored.
npm i mongoose
const mongoose = require('mongoose');
const uri = 'mongodb://localhost:27017/my-database';
const option = {
"useNewUrlParser": true,
"auth": { "authSource": "admin" },
"user": "my-user",
"pass": "my-user-password"
};
mongoose.connect(uri, option);
node index.js
So here we are. Our database is secure with role based user. Stay tuned and if there is a confusing term or something, response below. I will replay ASAP.