AMP AMP

How to Create and Configure MongoDb on Centos 7.6

How To Create and Configure MongoDB 4.0 On CentOS 7.6

Verify the version of MongoDB as follows.

[root@linuxhelp ~]# mongod --version
db version v4.0.11
git version: 417d1a712e9f040d54beca8e4943edce218e9a8c
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
    distmod: rhel70
    distarch: x86_64
    target_arch: x86_64

Creation of an Admin user

Connect to the mongoDB server via command line interface as follows

[root@linuxhelp ~]# mongo
MongoDB shell version v4.0.11
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("3683877a-3319-4c45-8739-cf27b540af08") }
MongoDB server version: 4.0.11
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	http://docs.mongodb.org/
Questions? Try the support group
	http://groups.google.com/group/mongodb-user
Server has startup warnings: 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
## List the databases available in the MongoDB
> show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB

## Switch Over to the admin database
> use admin;
switched to db admin

## Create an user with admin privileges
> db.createUser(
... {
... user:"user1",
... pwd:"linuxc",
... roles:[{role:"root",db:"admin"}]
... }
... )
Successfully added user: {
	"user" : "user1",
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}
> exit
bye

Now,Login to the MongoDB server using the user1 credentials as shown below

[root@linuxhelp ~]# mongo -u user1 -p --authenticationDatabase admin
MongoDB shell version v4.0.11
Enter password:
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("3072264f-d974-4b0a-bda3-c4b91a6dd775") }
MongoDB server version: 4.0.11
Server has startup warnings: 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> exit
Bye

Create And Drop the database

Login to the mongoDB server To Create and drop the database

 [root@linuxhelp ~]# mongo
MongoDB shell version v4.0.11
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a2a1ebdb-4170-4193-b7c5-bd711377ce3a") }
MongoDB server version: 4.0.11
Server has startup warnings: 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

## List the available databases in the MongoDb server
> show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB

Create a database
> use mongo;
switched to db mongo

Check out the database that you are currently switched in using the below command
> db
mongo

Check the database list after the creation of database
> show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB

To keep the created database into the list, need to insert the document into the database
> db.users.insert({"id":1})
WriteResult({ "nInserted" : 1 })

After the successful insertion of a document inside the database, now check the database list where you could find the created database name
> show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB
mongo   0.000GB

Drop the newly created database as follows
> db.dropDatabase ()
{ "dropped" : "mongo", "ok" : 1 }

Now verify the database list wher the mongo database has been dropped
> show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB

> exit
bye

Add and Drop a user for a Database

Connect to the mongoDb server shell as follows

[root@linuxhelp ~]# mongo
MongoDB shell version v4.0.11
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("c16982c9-73ee-454c-a006-b9256188cb85") }
MongoDB server version: 4.0.11
Server has startup warnings: 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

## create a database
> use mydb;
switched to db mydb

## Create a user for mydb database with read and write access
> db.createUser(
... {
... user:"user2",
... pwd:"linuxc",
... roles:["readWrite"]
... }
... )
Successfully added user: { "user" : "user2", "roles" : [ "readWrite" ] }

## List the mydb database users by executing the following command

> db.getUsers();
[
	{
		"_id" : "mydb.user2",
		"userId" : UUID("13340b07-909f-4445-8e62-534ccc08e7e1"),
		"user" : "user2",
		"db" : "mydb",
		"roles" : [
			{
				"role" : "readWrite",
				"db" : "mydb"
			}
		],
		"mechanisms" : [
			"SCRAM-SHA-1",
			"SCRAM-SHA-256"
		]
	}
]

## Now drop the user2 as follows

> db.dropUser('user2')
true

 ##Now list the mydb database user

> db.getUsers();
[ ]

##  Check the currently switched in the database by executing the following command

> db
mydb

With this, creation of admin user, database and user for a specific database, Dropped the database and also user for a specific database in MongoDB server on CentOS 7.6 comes to end.

FAQ
Q
What is the latest version of MongoDb server?
A
The latest version of MongoDb server is 4.0.11.
Q
What is the default port of a MongoDb server?
A
The default port of a MongoDb server is 27017
Q
What is the command to list the users of a particular database in MongoDB?
A
db.getUsers() is the command to list the users of a particular database in MongoDB
Q
What is the command to list the available databases in the MongoDB?
A
show dbs; is the command that lists the available databases in the MongoDB server
Q
What is the command to know the currently switched in database of MongoDB?
A
db is the command to know the currently switched in database of MongoDB