SlideShare a Scribd company logo
basics
JERWIN ROY J
Database Adminstrator
meetjerwin@gmail.com
Agenda
★ Introduction
★ MongoDB Installation
-yum
-binary
★ Roles in MongoDb
★ User creation
★ Basic commands
★ Trace Slow queries
★ Important monitoring commands
Introduction
What is NoSQL database?
A NoSQL or Not Only SQL database provides a mechanism
for storage and retrieval of data other than the tabular relations used
in relational databases. Motivations for this approach include simplicity of
design, horizontal scaling and finer control over availability.
What is mongodb?
MongoDB is a cross-platform, document oriented database
that provides, high performance, high availability, and easy scalability.
MongoDB works on concept of collection and document. It is also one of the
leading NoSQL database.
Why should we use MongoDB?
➔ Document Oriented Storage : Data is stored in the form of JSON style
documents
➔ Index on any attribute
➔ Replication & High Availability
➔ Auto-Sharding
➔ Rich Queries
➔ Fast In-Place Updates
➔ Map Reduce functions
➔ Professional Support By MongoDB
Database
Database is a physical container for collections. Each database gets its own set of
files on the file system. A single MongoDB server typically has multiple databases.
Collection
Collection is a group of MongoDB documents. It is the equivalent of an RDBMS table.
A collection exists within a single database. Collections do not enforce a schema.
Documents within a collection can have different fields. Typically, all documents in a
collection are of similar or related purpose.
Document
A document is a set of key-value pairs. Documents have dynamic schema. Dynamic
schema means that documents in the same collection do not need to have the same
set of fields or structure, and common fields in a collection's documents may
hold different types of data.
Relationship of RDBMS terminology with MongoDB
Installation - yum
1.Create a repo file as below:
vim /etc/yum.repos.d/mongodb.repo
2.For 64-bit systems type the below information in repo file and save.
[mongodb-org-3.0]
name=MongoDB 3.0 Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1
3.To install all the packages of mongodb issue the below command:
yum install mongodb-org
4.Start the installed mongodb server using the below command:
service mongod start
5.The server is now started and to login to the mongo shell issue the below command:
mongo
No need to include the port because we are running it on the default port(27017).
6.To check the current status of mongod issue the below command:
service mongod status
7.To stop the running mongod server use the below command:
service mongod stop
8.To change the data directory with a new one,stop the current running instance and go
to the config file(/etc/mongod.conf) make the changes then save.
9.Now start the mongodb,it works with the new data directory.Make sure that the data
directory has mongod user permission.
1.The mongodb binary are found in the official page(https://www.mongodb.org/downloads).
2.Download the binary using wget
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.4.tgz
3.Now extract the downloaded file using the below command:
tar zxf mongodb-linux-x86_64-3.0.3.tgz
4.Now rename the extracted(mongodb-linux-x86 64-2.6.3) file into mongodb
mv mongodb-linux-x86 64-2.6.3 mongodb
Installation - binary
5.Create a data directory using the below command
mkdir -p /home/data/db
6: Create an user mongo user using the following command
useradd mongod
7.Change the ownership of the files in the source and data directory using the following
command
chown -R mongod.mongod /home/data/new
chown -R mongod.mongod /home/data/db/
7.Create a configuration file in any directory say
vim /etc/mongod.conf
8.Now add the following details as shown below:
dbpath = /home/data/db
logpath = /home/data/mongodb.log
logappend = true
port = 27017
auth = true
9.Open the script file /etc/init.d/mongod
vim /etc/init.d/mongod
10.Make the change path in the CONFIGFILE="/etc/mongod.conf" with your config file
path.
11.Start the mongodb server using the below command:
service mongod start
5.The server is now started and to login to the mongo shell issue the below command:
mongo
6.To check the current status of mongod issue the below command:
service mongod status
7.To stop the running mongod server use the below command:
service mongod stop
Roles in MongoDB
Super roles:
readAnyDatabase-reads any database
readWriteAnyDatabase-reads & writes any database
userAdminAnyDatabase-user admin role to any database
dbAdminAnyDatabase-database admin any database
DB user roles:
read-reads current database
readWrite-reads & writes current databadse
dbAdmin-access to system. collections in current db
userAdmin-create,modify roles & users in current db
dbOwner-readWrite, dbAdmin & userAdmin
Cluster roles:
hostManager-monitor,manage,kill & repair db
clusterMonitor-read only access to monitoring tools
clusterManager-all operations to manage db except drop
clusterAdmin- can drop & combo of clusterManager, clusterMonitor, & hostManager.
Backup roles:
backup-to take bakup
restore-to restore the backup files
Creating a user
Root user:
Provides access to the operations readWriteAnyDatabase,
dbAdminAnyDatabase, userAdminAnyDatabase and clusterAdmin roles combined. It does
not include any access to collections that begin with the system. prefix.Create admin users
in the admin database so that they can access all dbs.
use admin
db.createUser( { user: “root",
pwd: “rootabc",
roles: [“root” ]
} )
So after creating the user it is possible to login only with user beacuse we have enabled
auth.
super user:
use admin
db.createUser( { user: "superuser",
pwd: "admin",
roles: [ "userAdminAnyDatabase",
"dbAdminAnyDatabase",
"readWriteAnyDatabase"
] } )
Read user for a database:
use database
db.createUser(
{
user: "read",
pwd: "password",
roles:
[ {
role: "read",
db: "database"
}
]
}
)
monitoring user:
For the monitoring this user serves best:
db.createUser(
{
user: "monitoring",
pwd: "abc123",
roles: [ "clusterMonitor" ]
}
)
Verify the privilege using:
db.runCommand(
{
usersInfo:"admin",
showPrivileges:true
}
)
Kill a query:
Find the opid using currentop(_) command.
db.killOp(opid)
opid-it is the operation id of a particular query.
Basic commands
A few basic commands that are used in the mongodb client
are listed below:
use new_db -Uses the databasespecified
db -Displays the current database name
show dbs -Displays list of all databases
show collections -Displays list of all collections
db.dropDatabase() -Drops the current database in use
db.collection.drop() -Drops the collection mentioned
Trace Slow queries
Slow queries can be traced using database profiler.Mongodb has
three levels of profiling,each with unique feature.
db.setProfilingLevel(0) ->no profiling
db.setProfilingLevel(1) ->slow queries
db.setProfilingLevel(2) ->all queries
To check the current profiling level use the below command:
db.getProfilingStatus()
All the traced slow queries will be present in predefined collection
system.profile in the local database.To view the queries fire the below
command:
db.system.profile.find()
Important Monitoring commands
Serverstatus - Similar to mysql show processlist
db.serverStatus().uptime
db.serverStatus().connections
db.serverStatus().opcounters
currentop():
Display all the documents that contains information on in-progress operations for the database
instance
To view the current active queries in the database:
db.currentOp(
{
"active" : true
}
)
To view all active read queries:
db.currentOp().inprog.forEach(
function(d){
if(d.active && d.lockType == "read")
printjson(d)
})
To view all active write queries:
db.currentOp().inprog.forEach(
function(d){
if(d.waitingForLock && d.lockType != "write")
printjson(d)
})
To view the queries that are waiting for a lock and not a read:
db.currentOp().inprog.forEach(
function(d){
if(d.waitingForLock && d.lockType != "read")
printjson(d)
})
To view the queries that are running more than ‘x(2)’ seconds in the database:
db.currentOp(
{
"active" : true,
"secs_running" : { "$gt" : 2}
}
)
db.stats()- Statistics about a database
db.collection.stats():
Statistics about a particular collection
rs.status(): Statistics about a replica set
rs.printReplicationInfo()- Replication info such as sync between the replica
rs.printSlaveReplicationInfo()- Slave replication details
db.isMaster() - To check whether a replica is master,true returns its a master
Thank You

More Related Content

What's hot (20)

PPTX
Mongo DB 102
Abhijeet Vaikar
 
PPTX
Basics of MongoDB
HabileLabs
 
PPTX
MongoDB 101
Abhijeet Vaikar
 
PPTX
The Basics of MongoDB
valuebound
 
PPTX
MongoDB presentation
Hyphen Call
 
PPTX
Top 10 frameworks of node js
HabileLabs
 
PPTX
Mongodb introduction and_internal(simple)
Kai Zhao
 
PPTX
MongoDB
nikhil2807
 
PDF
Mongo db dhruba
Dhrubaji Mandal ♛
 
PDF
Introduction to MongoDB
Mike Dirolf
 
PDF
Mongo db
Noman Ellahi
 
DOCX
Mongo db report
Hyphen Call
 
PDF
An introduction to MongoDB
César Trigo
 
PPTX
Mongo db1
VandanaKukreja
 
PDF
Mongodb
Scott Motte
 
PPT
Introduction to MongoDB
Ravi Teja
 
PDF
Introduction to mongo db
Rohit Bishnoi
 
PPT
Introduction to mongodb
neela madheswari
 
Mongo DB 102
Abhijeet Vaikar
 
Basics of MongoDB
HabileLabs
 
MongoDB 101
Abhijeet Vaikar
 
The Basics of MongoDB
valuebound
 
MongoDB presentation
Hyphen Call
 
Top 10 frameworks of node js
HabileLabs
 
Mongodb introduction and_internal(simple)
Kai Zhao
 
MongoDB
nikhil2807
 
Mongo db dhruba
Dhrubaji Mandal ♛
 
Introduction to MongoDB
Mike Dirolf
 
Mongo db
Noman Ellahi
 
Mongo db report
Hyphen Call
 
An introduction to MongoDB
César Trigo
 
Mongo db1
VandanaKukreja
 
Mongodb
Scott Motte
 
Introduction to MongoDB
Ravi Teja
 
Introduction to mongo db
Rohit Bishnoi
 
Introduction to mongodb
neela madheswari
 

Viewers also liked (20)

PPTX
Training MongoDB - Monitoring and Operability
Nicolas Motte
 
PDF
Webinar slides: Become a MongoDB DBA - What to Monitor (if you’re really a My...
Severalnines
 
PDF
MongoDB training for java software engineers
Moshe Kaplan
 
PPTX
Server discovery and monitoring with MongoDB
Joe Drumgoole
 
PDF
Secrets of World Class HR Depts | webinar with PayStream Advisors & docSTAR
docSTAR
 
PPTX
iMIS 20 Overview for Education Associations
iMIS
 
PPT
Feeling words
Embark Business Solutions
 
PPT
Oa presentation1 (1)
Gamut Infosystems Ltd
 
PPTX
HR Software, Payroll & Time Management: Discover Carval in 60 Seconds
Carval Computing Limited
 
PPTX
medez web pesentation 1122015
MedEZ, Integrated Software Solutions
 
PDF
Production management software_to_the_rescue
Argos Software
 
PDF
Is mobile's big promise a farce?
Mobile Roadie
 
PPT
SkillPoint™ VRx Recruiting Software
Platina Software Pvt. Ltd.
 
PDF
Get a 3D view of your workforce
Access Group
 
PDF
Documentation of technology practices using blog: case study of koha geek and...
Mahatma Gandhi University Library
 
PDF
Marketing To Asian Women Conference Singapore
One9Ninety
 
PPT
Cloud Computing - What is it?
Liquid Accounts
 
PDF
Why Are More Australians Shopping Small Business?
Cashflow Manager
 
PPTX
How to use GitHub to Predict the Success of your Application
Grip QA
 
PPSX
4 habilidades de un líder
Safi
 
Training MongoDB - Monitoring and Operability
Nicolas Motte
 
Webinar slides: Become a MongoDB DBA - What to Monitor (if you’re really a My...
Severalnines
 
MongoDB training for java software engineers
Moshe Kaplan
 
Server discovery and monitoring with MongoDB
Joe Drumgoole
 
Secrets of World Class HR Depts | webinar with PayStream Advisors & docSTAR
docSTAR
 
iMIS 20 Overview for Education Associations
iMIS
 
Oa presentation1 (1)
Gamut Infosystems Ltd
 
HR Software, Payroll & Time Management: Discover Carval in 60 Seconds
Carval Computing Limited
 
medez web pesentation 1122015
MedEZ, Integrated Software Solutions
 
Production management software_to_the_rescue
Argos Software
 
Is mobile's big promise a farce?
Mobile Roadie
 
SkillPoint™ VRx Recruiting Software
Platina Software Pvt. Ltd.
 
Get a 3D view of your workforce
Access Group
 
Documentation of technology practices using blog: case study of koha geek and...
Mahatma Gandhi University Library
 
Marketing To Asian Women Conference Singapore
One9Ninety
 
Cloud Computing - What is it?
Liquid Accounts
 
Why Are More Australians Shopping Small Business?
Cashflow Manager
 
How to use GitHub to Predict the Success of your Application
Grip QA
 
4 habilidades de un líder
Safi
 
Ad

Similar to MongoDB basics & Introduction (20)

PDF
MongoDB - An Introduction
sethfloydjr
 
PPTX
Introduction to MongoDB.pptx
Surya937648
 
PDF
Quick & Dirty & MEAN
Troy Miles
 
PPTX
Kalp Corporate MongoDB Tutorials
Kalp Corporate
 
PPTX
MongoDbPpt based on python installation.
jnvcomputerlab2024
 
PDF
MongoDB
wiTTyMinds1
 
PPTX
Mongo db Quick Guide
Sourabh Sahu
 
PDF
Mongodb
ichangbai
 
PPTX
MongoDB introduction features -presentation - 2.pptx
sampathkumar546444
 
PDF
MongoDB: Advantages of an Open Source NoSQL Database
FITC
 
PDF
Philadelphia MongoDB User Group - Your First MongoDB Application
Michael Lynn
 
PDF
Mongodb By Vipin
Vipin Mundayad
 
PPTX
Introduction to MongoDB a brief intro(1).pptx
mehfooz968268
 
PPTX
Mongo db
Gyanendra Yadav
 
PDF
SQL vs NoSQL, an experiment with MongoDB
Marco Segato
 
PDF
The Little MongoDB Book - Karl Seguin
Paulo Fagundes
 
PDF
Introduction to mongo db
Lawrence Mwai
 
PDF
Mongo db halloween party
Andrea Balducci
 
PDF
Mongodb
Paulo Fagundes
 
PDF
MongoDB.pdf54teeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeer
MdRiyad22
 
MongoDB - An Introduction
sethfloydjr
 
Introduction to MongoDB.pptx
Surya937648
 
Quick & Dirty & MEAN
Troy Miles
 
Kalp Corporate MongoDB Tutorials
Kalp Corporate
 
MongoDbPpt based on python installation.
jnvcomputerlab2024
 
MongoDB
wiTTyMinds1
 
Mongo db Quick Guide
Sourabh Sahu
 
Mongodb
ichangbai
 
MongoDB introduction features -presentation - 2.pptx
sampathkumar546444
 
MongoDB: Advantages of an Open Source NoSQL Database
FITC
 
Philadelphia MongoDB User Group - Your First MongoDB Application
Michael Lynn
 
Mongodb By Vipin
Vipin Mundayad
 
Introduction to MongoDB a brief intro(1).pptx
mehfooz968268
 
Mongo db
Gyanendra Yadav
 
SQL vs NoSQL, an experiment with MongoDB
Marco Segato
 
The Little MongoDB Book - Karl Seguin
Paulo Fagundes
 
Introduction to mongo db
Lawrence Mwai
 
Mongo db halloween party
Andrea Balducci
 
MongoDB.pdf54teeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeer
MdRiyad22
 
Ad

Recently uploaded (20)

PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PDF
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pdf
ghjghvhjgc
 
PPTX
Manual Testing for Accessibility Enhancement
Julia Undeutsch
 
PDF
NASA A Researcher’s Guide to International Space Station : Fundamental Physics
Dr. PANKAJ DHUSSA
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PPTX
Securing Model Context Protocol with Keycloak: AuthN/AuthZ for MCP Servers
Hitachi, Ltd. OSS Solution Center.
 
PDF
Survival Models: Proper Scoring Rule and Stochastic Optimization with Competi...
Paris Women in Machine Learning and Data Science
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Digital Circuits, important subject in CS
contactparinay1
 
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pdf
ghjghvhjgc
 
Manual Testing for Accessibility Enhancement
Julia Undeutsch
 
NASA A Researcher’s Guide to International Space Station : Fundamental Physics
Dr. PANKAJ DHUSSA
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Securing Model Context Protocol with Keycloak: AuthN/AuthZ for MCP Servers
Hitachi, Ltd. OSS Solution Center.
 
Survival Models: Proper Scoring Rule and Stochastic Optimization with Competi...
Paris Women in Machine Learning and Data Science
 

MongoDB basics & Introduction

  • 2. Agenda ★ Introduction ★ MongoDB Installation -yum -binary ★ Roles in MongoDb ★ User creation ★ Basic commands ★ Trace Slow queries ★ Important monitoring commands
  • 3. Introduction What is NoSQL database? A NoSQL or Not Only SQL database provides a mechanism for storage and retrieval of data other than the tabular relations used in relational databases. Motivations for this approach include simplicity of design, horizontal scaling and finer control over availability. What is mongodb? MongoDB is a cross-platform, document oriented database that provides, high performance, high availability, and easy scalability. MongoDB works on concept of collection and document. It is also one of the leading NoSQL database.
  • 4. Why should we use MongoDB? ➔ Document Oriented Storage : Data is stored in the form of JSON style documents ➔ Index on any attribute ➔ Replication & High Availability ➔ Auto-Sharding ➔ Rich Queries ➔ Fast In-Place Updates ➔ Map Reduce functions ➔ Professional Support By MongoDB
  • 5. Database Database is a physical container for collections. Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases. Collection Collection is a group of MongoDB documents. It is the equivalent of an RDBMS table. A collection exists within a single database. Collections do not enforce a schema. Documents within a collection can have different fields. Typically, all documents in a collection are of similar or related purpose. Document A document is a set of key-value pairs. Documents have dynamic schema. Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection's documents may hold different types of data.
  • 6. Relationship of RDBMS terminology with MongoDB
  • 7. Installation - yum 1.Create a repo file as below: vim /etc/yum.repos.d/mongodb.repo 2.For 64-bit systems type the below information in repo file and save. [mongodb-org-3.0] name=MongoDB 3.0 Repository baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/ gpgcheck=0 enabled=1
  • 8. 3.To install all the packages of mongodb issue the below command: yum install mongodb-org 4.Start the installed mongodb server using the below command: service mongod start 5.The server is now started and to login to the mongo shell issue the below command: mongo No need to include the port because we are running it on the default port(27017).
  • 9. 6.To check the current status of mongod issue the below command: service mongod status 7.To stop the running mongod server use the below command: service mongod stop 8.To change the data directory with a new one,stop the current running instance and go to the config file(/etc/mongod.conf) make the changes then save. 9.Now start the mongodb,it works with the new data directory.Make sure that the data directory has mongod user permission.
  • 10. 1.The mongodb binary are found in the official page(https://www.mongodb.org/downloads). 2.Download the binary using wget wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.4.tgz 3.Now extract the downloaded file using the below command: tar zxf mongodb-linux-x86_64-3.0.3.tgz 4.Now rename the extracted(mongodb-linux-x86 64-2.6.3) file into mongodb mv mongodb-linux-x86 64-2.6.3 mongodb Installation - binary
  • 11. 5.Create a data directory using the below command mkdir -p /home/data/db 6: Create an user mongo user using the following command useradd mongod 7.Change the ownership of the files in the source and data directory using the following command chown -R mongod.mongod /home/data/new chown -R mongod.mongod /home/data/db/
  • 12. 7.Create a configuration file in any directory say vim /etc/mongod.conf 8.Now add the following details as shown below: dbpath = /home/data/db logpath = /home/data/mongodb.log logappend = true port = 27017 auth = true
  • 13. 9.Open the script file /etc/init.d/mongod vim /etc/init.d/mongod 10.Make the change path in the CONFIGFILE="/etc/mongod.conf" with your config file path. 11.Start the mongodb server using the below command: service mongod start
  • 14. 5.The server is now started and to login to the mongo shell issue the below command: mongo 6.To check the current status of mongod issue the below command: service mongod status 7.To stop the running mongod server use the below command: service mongod stop
  • 15. Roles in MongoDB Super roles: readAnyDatabase-reads any database readWriteAnyDatabase-reads & writes any database userAdminAnyDatabase-user admin role to any database dbAdminAnyDatabase-database admin any database DB user roles: read-reads current database readWrite-reads & writes current databadse dbAdmin-access to system. collections in current db userAdmin-create,modify roles & users in current db dbOwner-readWrite, dbAdmin & userAdmin
  • 16. Cluster roles: hostManager-monitor,manage,kill & repair db clusterMonitor-read only access to monitoring tools clusterManager-all operations to manage db except drop clusterAdmin- can drop & combo of clusterManager, clusterMonitor, & hostManager. Backup roles: backup-to take bakup restore-to restore the backup files
  • 17. Creating a user Root user: Provides access to the operations readWriteAnyDatabase, dbAdminAnyDatabase, userAdminAnyDatabase and clusterAdmin roles combined. It does not include any access to collections that begin with the system. prefix.Create admin users in the admin database so that they can access all dbs. use admin db.createUser( { user: “root", pwd: “rootabc", roles: [“root” ] } )
  • 18. So after creating the user it is possible to login only with user beacuse we have enabled auth. super user: use admin db.createUser( { user: "superuser", pwd: "admin", roles: [ "userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase" ] } )
  • 19. Read user for a database: use database db.createUser( { user: "read", pwd: "password", roles: [ { role: "read", db: "database" } ] } )
  • 20. monitoring user: For the monitoring this user serves best: db.createUser( { user: "monitoring", pwd: "abc123", roles: [ "clusterMonitor" ] } )
  • 21. Verify the privilege using: db.runCommand( { usersInfo:"admin", showPrivileges:true } ) Kill a query: Find the opid using currentop(_) command. db.killOp(opid) opid-it is the operation id of a particular query.
  • 22. Basic commands A few basic commands that are used in the mongodb client are listed below: use new_db -Uses the databasespecified db -Displays the current database name show dbs -Displays list of all databases show collections -Displays list of all collections db.dropDatabase() -Drops the current database in use db.collection.drop() -Drops the collection mentioned
  • 23. Trace Slow queries Slow queries can be traced using database profiler.Mongodb has three levels of profiling,each with unique feature. db.setProfilingLevel(0) ->no profiling db.setProfilingLevel(1) ->slow queries db.setProfilingLevel(2) ->all queries To check the current profiling level use the below command: db.getProfilingStatus() All the traced slow queries will be present in predefined collection system.profile in the local database.To view the queries fire the below command: db.system.profile.find()
  • 24. Important Monitoring commands Serverstatus - Similar to mysql show processlist db.serverStatus().uptime db.serverStatus().connections db.serverStatus().opcounters
  • 25. currentop(): Display all the documents that contains information on in-progress operations for the database instance To view the current active queries in the database: db.currentOp( { "active" : true } ) To view all active read queries: db.currentOp().inprog.forEach( function(d){ if(d.active && d.lockType == "read") printjson(d) })
  • 26. To view all active write queries: db.currentOp().inprog.forEach( function(d){ if(d.waitingForLock && d.lockType != "write") printjson(d) }) To view the queries that are waiting for a lock and not a read: db.currentOp().inprog.forEach( function(d){ if(d.waitingForLock && d.lockType != "read") printjson(d) })
  • 27. To view the queries that are running more than ‘x(2)’ seconds in the database: db.currentOp( { "active" : true, "secs_running" : { "$gt" : 2} } )
  • 31. rs.printReplicationInfo()- Replication info such as sync between the replica rs.printSlaveReplicationInfo()- Slave replication details
  • 32. db.isMaster() - To check whether a replica is master,true returns its a master