• Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial News Comments FAQ Related Articles

How to Create and Configure MongoDb on Centos 7.6

  • 00:35 mongod --version
  • 00:43 mongo
  • 00:57 show dbs;
  • 01:06 use admin;
  • 01:11 db.createUser( { user:"user1", pwd:"linuxc",roles:[{role:"root",db:"admin"}] } )
  • 01:57 mongo -u user1 -p --authenticationDatabase admin
  • 02:37 use mongo;
  • 03:05 db.users.insert({"id":1})
  • 03:19 db.dropDatabase ()
  • 03:53 db.createUser( { user:"user2", pwd:"linuxc",roles:["readWrite"] } )
  • 04:22 db.getUsers();
  • 04:34 db.dropUser('user2')
{{postValue.id}}

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.

Tags:
caden
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

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

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 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 default port of a MongoDb server?

A

The default port of a MongoDb server is 27017

Q

What is the latest version of MongoDb server?

A

The latest version of MongoDb server is 4.0.11.

Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help David Lopez Guillen ?
Ayuda urgente instale SSL para servidor Opensuse y ahora no funciona tengo servicio web

hola segui este tutorial para tener un certificado ssl y ahora no se ve mi app en la red, espero alguien pueda ayudarme, tengo M9oodle en3.5 en un servidor open suse y ahora no funciona por favor ayuda.

https://www.linuxhelp.com/how-to-create-ssl-certificate-in-opensuse

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.