mongo 101 - basics · 2018. 4. 25. · how different is mongodb from mysql/rdbs nosql and sql are...
TRANSCRIPT
![Page 1: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/1.jpg)
Mongo 101 - Basics
![Page 2: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/2.jpg)
2
Who are we?
Adamo Tonete
● Senior Support Engineer● Joined Percona in 2015● 10+ years as a DBA● 5+ years working with NoSQL
products
Rick Golba
● Product Marketing Manager● Joined Percona in 2014 as a
Solutions Engineer● 20+ years as a SQL trainer● 3+ years working with NoSQL
products
![Page 3: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/3.jpg)
● What is MongoDB?● How different is MongoDB from MySQL?● Common MongoDB topologies● CRUD: data management● Aggregations, Import/Export, and Backups● Schema design patterns● Replica-sets and Upgrades● Securing your setup - Demo● Common issues: How to detect, verify and address them using logs, Percona
Toolkit, and Percona Monitoring and Management (PMM)
Agenda
![Page 4: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/4.jpg)
● In order to install the software you should have a laptop with at least:○ 2 cores○ 4 gb ram○ 500 MB disk○ Internet connection○ Git client installed
We strongly suggest using Linux but Windows machines will work as well
Requirements
![Page 5: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/5.jpg)
Installing MongoDB
![Page 6: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/6.jpg)
Now that we already know what MongoDB is, let's install the database
For MacOS and Linux
Go to mongodb.com/downloads
git clone https://github.com/adamobr/MongoDB3XLabs
cd MongoDB3XLabs
./run_single.sh
Installing MongoDB - Single instance
![Page 7: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/7.jpg)
First commands:
show databases
use percona
db.collection.insert({today : new Date()})
db.collection.find()
Installing MongoDB - Single instance
![Page 8: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/8.jpg)
First let's cleanup the previous instance
./reset_lab
./run_replicaset
./3.6/bin/mongo # to connect to the replica-set.
Installing MongoDB - Replica-set
![Page 9: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/9.jpg)
Installing MongoDB - Replica-set
Let's run some commands to describe your environment:
rs.status()
rs.config()
db.serverStatus()
![Page 10: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/10.jpg)
What is MongoDB?
![Page 11: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/11.jpg)
What is MongoDB
● NoSQL● Document-oriented Database● Built for fast delivery and development● Easy-to-scale database● ...and a LOT more!
![Page 12: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/12.jpg)
How different is MongoDB from MySQL?
![Page 13: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/13.jpg)
How different is MongoDB from MySQL/RDBS
● Some features we will compare
○ Normalization○ Transactions○ Query language○ Data are stored○ Special indexes○ How to distribute and scale
![Page 14: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/14.jpg)
How different is MongoDB from MySQL/RDBS
● NoSQL and SQL are not enemies; they are made to complement each other● While MongoDB is a young NoSQL database, MySQL has been in the market for a couple of
years as a mature relational database system● In some cases, using MongoDB as the main database is not the best thing to do● However, MongoDB can provide a very fast-growing environment without too much effort
![Page 15: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/15.jpg)
How different is MongoDB from MySQL/RDBS
● Comparing data distribution
○ MongoDB expects data to grow beyond machine limitations○ MySQL does have a few add-ons that allow data distribution among instances, but they
were created later by a 3rd-party company○ MySQL expects to work in a single machine with full ACID, while MongoDB doesn't
expect ACID, MongoDB is limited to the CAP theorem
![Page 16: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/16.jpg)
What is ACID?
Atomicity: single document level & no snapshotting for readsConsistency: primary = strong | secondaries = your choiceIsolation: not really, $isolated can helpDurability: configurable w:majority and/or j:true
How different is MongoDB from MySQL/RDBS
![Page 17: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/17.jpg)
How different is MongoDB from MySQL/RDBS
● The CAP theorem was proposed by Eric Allen in 2000
● A distributed system can't have the 3 guarantees at the same time. One of them must be sacrificed.
![Page 18: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/18.jpg)
How different is MongoDB from MySQL/RDBS
● Consistency● Availability● Partition Tolerance
Anyone will get the same response, data is consistent among instances
A
PC
![Page 19: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/19.jpg)
How different is MongoDB from MySQL/RDBS
● Consistency● Availability● Partition Tolerance
System will always respond to requests, no downtime
A
PC
![Page 20: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/20.jpg)
How different is MongoDB from MySQL/RDBS
● Consistence● Availability● Partition Tolerance
System can handle errors (network, hardware failure)
A
PC
![Page 21: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/21.jpg)
How different is MongoDB from MySQL/RDBS
A
PC
Relational Databases
MySQL
PostgreSQL
Cassandra
Riak
MongoDB
Redis
![Page 22: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/22.jpg)
How different is MongoDB from MySQL/RDBS
![Page 23: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/23.jpg)
How different is MongoDB from MySQL/RDBS
At each intersection is a
single scalar value
![Page 24: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/24.jpg)
{
"_id" : ObjectId("507f1f77bcf86cd799439011"),
"studentID" : 100,
"firstName" : "Jonathan",
"middleName" : "Eli",
"lastName" : "Tobin",
"classes" : [
{ "courseID" : "PHY101",
"grade" : "B",
"courseName" : "Physics 101",
"credits" : 3 },
{ "courseID" : "BUS101",
"grade" : "B+",
"courseName" : "Business 101",
"credits" : 3 }
]
How different is MongoDB from MySQL/RDBS
![Page 25: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/25.jpg)
● Unlike MySQL, MongoDB doesn't have a predefined schema● Documents can have different fields with different data types, for example
{x : 1, y : ['test']}
and
{x : 'percona', y : ISODate('2018-01-01')}
are both valid MongoDB documents
How different is MongoDB from MySQL/RDBS
![Page 26: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/26.jpg)
● No joins● Rich Geo Indexing● Schema-free
How different is MongoDB from MySQL/RDBS
![Page 27: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/27.jpg)
● MongoDB doesn't use 3rd-form normalization● All documents must have as much information as necessary
○ Linked documents are acceptable but not recommended
How different is MongoDB from MySQL/RDBS
![Page 28: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/28.jpg)
● High availability by default in MongoDB● Replica sets is the minimum suggested way to go to production● Shards can be used to increase read/write throughput - we will discuss that in
Topology
How different is MongoDB from MySQL/RDBS
![Page 29: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/29.jpg)
● Machine costs● If we want to scale MongoDB, we can simply add more machines
○ This is not always true for MySQL
How different is MongoDB from MySQL/RDBS
![Page 30: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/30.jpg)
How Similar is MongoDB to MySQL?
![Page 31: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/31.jpg)
● … but these databases are not completely different
● They share
○ Security○ Indexing○ Multi-user access - 3.6 (session)○ Multi table○ Concurrency○ Several other database concepts
How Similar MongoDB is to MySQL
![Page 32: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/32.jpg)
Database terms and concepts
How Similar MongoDB is to MySQL
MongoDB MySQL
Database Database
Collection Table
Document Row
Key : value pair Field
![Page 33: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/33.jpg)
● Indexes are the fast way to find a specific row or document they are very similar in both databases
How Similar MongoDB is to MySQL
![Page 34: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/34.jpg)
● Multi-user ● Concurrent operations
How Similar MongoDB is to MySQL
![Page 35: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/35.jpg)
MongoDB Topologies
![Page 36: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/36.jpg)
MongoDB Topologies
It is possible to deploy MongoDB using
● Single instance● Replica set● Sharded cluster
![Page 37: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/37.jpg)
MongoDB Topologies - Single Instance
![Page 38: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/38.jpg)
MongoDB Topologies
Single instance
● Commonly used for testing purposes● Percona doesn't recommend using single instances for production
![Page 39: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/39.jpg)
MongoDB Topologies - Replica Set
![Page 40: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/40.jpg)
Replica set
● Very common for small/medium environments● Asynchronous replication● Easy-to-scale reads● Doesn't scale writes● Can have delayed members● Rely on oplog
MongoDB Topologies
![Page 41: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/41.jpg)
MongoDB Topologies - Sharded Cluster
![Page 42: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/42.jpg)
MongoDB Topologies
Sharded Cluster
● Act very similarly to replica-sets, but they are used to scale reads/writes among shards
● Data is divided in shards● Data can migrate among shards● If not using the right shard key, they don’t scale well● Rely on oplogs + config servers
![Page 43: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/43.jpg)
Installing MongoDB
![Page 44: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/44.jpg)
Now that we already know what MongoDB is, let's install the database
For MacOS and Linux
Go to mongodb.com/downloads
git clone https://github.com/adamobr/MongoDB3XLabs
cd MongoDB3XLabs
./run_single.sh
Installing MongoDB - Single instance
![Page 45: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/45.jpg)
First commands:
show databases
use percona
db.collection.insert({today : new Date()})
db.collection.find()
Installing MongoDB - Single instance
![Page 46: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/46.jpg)
First let's cleanup the previous instance
./reset_lab
./run_replicaset
./3.6/bin/mongo # to connect to the replica-set.
Installing MongoDB - Replica-set
![Page 47: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/47.jpg)
Installing MongoDB - Replica-set
Let's run some commands to describe your environment:
rs.status()
rs.config()
db.serverStatus()
![Page 48: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/48.jpg)
MongoDB Operations
![Page 49: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/49.jpg)
MongoDB Operations
● Creating and using a database● CRUD Operations - Create, Read, Update, Delete● Create - insert
• Collections• Documents
● Read - find• Using operators
● Update - update vs upsert• Write concern considerations
● Delete - remove
![Page 50: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/50.jpg)
Connecting to the Mongo Shell
●When connecting to a mongo instance, you connect to the test database by default
●You will likely use the --port option to connect to a specific mongod instance. In this case we are connecting via localhost.
● If connecting remotely, specify a --host option where your mongod is running
![Page 51: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/51.jpg)
Creating a Database➔ This command shows which databases exist
➔ This command creates and connects to the percona database
➔ This command shows which db you are currently connected to
➔ Why is the database we created not showing?
![Page 52: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/52.jpg)
Creating a Database
●The database isn’t actually created until we write data to it
● If you disconnect from the shell after using a database, but writing no data it will be gone
●Let’s write some data so it stays
![Page 53: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/53.jpg)
Inserting Documents
●db.collection_name.insert() is the basic syntax● In this case, we inserted a simple key : value pair●We used java syntax to create a random number between 0 and 1 with our
Math.random() function●To see our insert, we use the find() function- Because this collection only has 1 document inserted, we only see one result
![Page 54: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/54.jpg)
Inserting Documents
● It is important to note that we did not actually create a collection with the previous command explicitly. The collection was created when we inserted a document into it.
●We can see the collections in the current database using the show collections command
●The system.indexes collection contains information about indexes in the database (percona)
![Page 55: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/55.jpg)
Reading
●Before we explore find commands in the mongo shell, let’s insert some test data into our new collection
●MongoDB allows us to write a short script to generate test data directly into the shell
●This script generates 25 random numbers from 0-1 and inserts them into our new_collection collection
![Page 56: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/56.jpg)
Reading
●Our results show 26 documents in our new_collection●The mongo shell caps to 20 results by default
- You can iterate more by typing it●The _id field is auto-generated if not specified. It is unique like a foreign key in
SQL.
![Page 57: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/57.jpg)
57
Full Results
![Page 58: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/58.jpg)
Reading
●This is a search specifically by _id
●An index was created by default on this when we created our document
![Page 59: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/59.jpg)
Reading with Operators
●There are many operators to use when querying data in MongoDB, but we will focus on these
![Page 60: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/60.jpg)
Reading with Operators
●How would we find numbers greater than 0.9 in our collection that we added random numbers to?
A.db.new_collection.find( "random_number" : $gt : .9 )
B.db.new_collection.find( { "random_number" : { $gt : .9 } } )
C.db.new_collection.find( { "random_number" > .9 } )
D.db.new_collection.find( { "random_number" : { $gt : .9 } )
![Page 61: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/61.jpg)
Reading with Operators
●B is the correct answer
A.db.new_collection.find( "random_number" : $gt : .9 )
B.db.new_collection.find( { "random_number" : { $gt : .9 } } )
C.db.new_collection.find( { "random_number" > .9 } )
D.db.new_collection.find( { "random_number" : { $gt : .9 } )
![Page 62: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/62.jpg)
Reading with Operators
●Here’s what it looks like in practice
●Now that we know how to find documents, let’s update some
![Page 63: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/63.jpg)
Updating
●The general syntax for an update is:• db.collection_name.update( query , update , options )
●By default, updating only updates a single document. Setting the multi parameter allows modifying all documents found in the query.
●Let’s update our documents with a random number value greater than .9 to 1
![Page 64: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/64.jpg)
Updating
![Page 65: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/65.jpg)
Updating - Write Concern
●Write concerns can be customized per operation if specified. Default behavior is used otherwise.
●w:0 = Fire and forget, no acknowledgement
●w:1 = Acknowledgement from primary only
●w: "majority" = Acknowledgement from majority of nodes with data
Application
Primary
Secondary
Secondary
![Page 66: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/66.jpg)
Deleting
●Remove all documents in a collection: db.collection.remove()●Partial removal, you must specify your criteria as shown below
![Page 67: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/67.jpg)
Deleting
What is the correct syntax for deleting all records in our collection with a value less than .2 ?
A.db.new_collection.remove ( { "random_number" : { $lt : .2 } } )B.db.remove( { "random_number" : { $lt : .2 } } )C.db.new_collection.remove ( { $lt : .2 } )D.db.new_collection.remove ( { "random_number" < .2 } )
![Page 68: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/68.jpg)
Deleting
A is the correct answer
A.db.new_collection.remove ( { "random_number" : { $lt : .2 } } )B.db.remove( { "random_number" : { $lt : .2 } } )C.db.new_collection.remove ( { $lt : .2 } )D.db.new_collection.remove ( { "random_number" < .2 } )
![Page 69: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/69.jpg)
Deleting
●Dropping an entire collection can also be done with the drop command
![Page 70: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/70.jpg)
Deleting
●Databases can also be dropped with the dropDatabase command
![Page 71: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/71.jpg)
Aggregation, Export/Import and Backups
![Page 72: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/72.jpg)
MongoDB Aggregations
MongoDB features aggregations to help us to write complex queries as some calculations, grouping can not be done with standard querying
So what is the aggregation framework?
Similar to OLAP more used for analytics
![Page 73: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/73.jpg)
MongoDB Aggregations
Aggregation framework works with a pipeline and the most common case is
Match > Project > Sort/Group > Final
Each process is dependent of the previous one and the next process depend on the previous one
The output can be a cursor or a collection
![Page 74: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/74.jpg)
MongoDB Aggregations
An aggregation example:
db.new_collection.aggregate([{$match : {random_number : {$gte : 0.5} }}, {$project : {_id : '$random_number'}}])
Values $gte 0.5
![Page 75: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/75.jpg)
MongoDB import and export
Like other databases there are some tools to help us to export and import data
It is possible to export a single collection, with or without a filter
With some flags it is possible to generate a CSV file to load in a different database
![Page 76: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/76.jpg)
MongoDB backup and restore
There are couple of methods to backup and restore a MongoDB instance
But mongodump is the most common method to save a copy of the data
● Disk snapshot● Hot backup
![Page 77: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/77.jpg)
./mongodump -d percona -c test -o perconabackup
2018-04-22T20:26:59.665-0300 writing percona.test to
2018-04-22T20:26:59.666-0300 done dumping percona.test (1 document)
MongoDB mongodump
![Page 78: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/78.jpg)
MongoDB mongorestore
MacPro13:bin adamo$ ./mongorestore -d perconabackup perconabackup/percona/
2018-04-22T20:28:12.109-0300 the --db and --collection args should only be used when restoring from a
BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2018-04-22T20:28:12.110-0300 building a list of collections to restore from perconabackup/percona dir
2018-04-22T20:28:12.111-0300 reading metadata for perconabackup.test from
perconabackup/percona/test.metadata.json
2018-04-22T20:28:12.168-0300 restoring perconabackup.test from perconabackup/percona/test.bson
2018-04-22T20:28:12.261-0300 no indexes to restore
2018-04-22T20:28:12.261-0300 finished restoring perconabackup.test (1 document)
2018-04-22T20:28:12.261-0300 done
![Page 79: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/79.jpg)
Schema design
![Page 80: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/80.jpg)
Schema design
Although MongoDB is a schema free database there are good practices that need
to be followed
● Use suggestive collection names
● Avoid "joins"
● Keep the document pretty simple
● Keep field names short (mmap)
![Page 81: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/81.jpg)
Schema design
● One to some (< 16MB)
{
_id : ObjectId('8123ad324723ds9fd83453'),
text : 'this is a really simple blog post'
comments : [
{_id : 1, comment : "I really liked your post"},
{_id : 2, comment : "too simple!"}
]
}
![Page 82: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/82.jpg)
Schema design
● One to Thousands (maybe > 16MB)
{
_id : ObjectId('8123ad324723ds9fd83453'),
brand : 'Lemon'
parts : [
ObjectId(), ObjectId(), ObjectId(), ObjectId(), ObjectId()... ObjectId() …]
}
![Page 83: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/83.jpg)
Schema design
Think about denormalization, how to transform a couple of tables in a single
document
![Page 84: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/84.jpg)
Installing MongoDB
![Page 85: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/85.jpg)
Now that we already know what MongoDB is, let's install the database
For MacOS and Linux
Go to mongodb.com/downloads
git clone https://github.com/adamobr/MongoDB3XLabs
cd MongoDB3XLabs
./run_single.sh
Installing MongoDB - Single instance
![Page 86: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/86.jpg)
First commands:
show databases
use percona
db.collection.insert({today : new Date()})
db.collection.find()
Installing MongoDB - Single instance
![Page 87: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/87.jpg)
First let's cleanup the previous instance
./reset_lab
./run_replicaset
./3.6/bin/mongo # to connect to the replica-set.
Installing MongoDB - Replica-set
![Page 88: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/88.jpg)
Installing MongoDB - Replica-set
Let's run some commands to describe your environment:
rs.status()
rs.config()
db.serverStatus()
![Page 89: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/89.jpg)
Replicaset Upgrades
![Page 90: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/90.jpg)
Replica-sets and Upgrades
● Upgrades can be done without downtime
● Drivers help, it doesn't depend only on the instances
● New versions usually can be a member of an old replica-set
![Page 91: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/91.jpg)
Replica-sets and Upgrades
In order to upgrade a replica-set, we will take advantage of high availability
![Page 92: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/92.jpg)
Replica-sets and Upgrades
Removing a secondary or setting the instance as hidden
![Page 93: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/93.jpg)
Replica-sets and Upgrades
Then drivers will see this configuration
![Page 94: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/94.jpg)
Replica-sets and Upgrades
Repeat the process in the remaining secondaries
![Page 95: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/95.jpg)
Replica-sets and Upgrades
Step the primary down and replace the remaining old instance
![Page 96: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/96.jpg)
Let's talk about security
![Page 97: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/97.jpg)
MongoDB Operations
● Default roles
● Enabling authentication and creating a root and a standard user
● Starting a replica-set environment with authentication
![Page 98: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/98.jpg)
Default Roles
● All the roles listed below come by default in the MongoDB database serverhttps://docs.mongodb.com/manual/reference/built-in-roles/
read readWrite dbAdmin dbOwner userAdmin
clusterAdmin clusterManager clusterMonitor hostManager backup
restore readAnyDatabase readWriteAnyDatabase userAdminAnyDatabase
dbAdminAnyDatabase root __system
![Page 99: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/99.jpg)
Enabling authentication
● Creating a root user and restarting the mongod process
mongo
use admin
> db.createUser({user : 'administrator', pwd : '123321', roles : ['root']})
Successfully added user: { "user" : "administrator", "roles" : [ "root" ] }
-- mongod.conf --
#security
security
authorization : enabled
-- service restart ---
./mongod --auth
![Page 100: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/100.jpg)
Enabling authentication
● Checking access
use admin
db.auth('administrator','123321')
1
mongo -u administrator -p --authenticationDatabase admin
password:
> show dbs
local
percona
![Page 101: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/101.jpg)
Creating a standard user
$ mongo
db.createUser({user: 'percona_user', pwd: '123', roles : [{ role :'read', db:
'percona'}]})
Successfully added user: {
"user" : "percona_user",
"roles" : [
{
"role" : "read",
"db" : "percona"
}
]
}
![Page 102: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/102.jpg)
Starting a replica-set with keyfile
![Page 103: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/103.jpg)
Starting a replica-set using keyfile
Empty
Secondary
Primary
I know your secret...
● Pre existing data instance with users in the admin database
![Page 104: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/104.jpg)
Starting a replica-set with key file
sync'edPrimary
Ok, i can trust you.
Data...__system
client
![Page 105: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/105.jpg)
Demo
./reset_lab.sh
./get_mongod_downloads.sh
cd 3.6/bin
mkdir data1 data2 data3
./mongod --dbpath data1 --logpath data/log.log --auth --fork --replSet myrs
./mongod --dbpath data2 --logpath data2/log.log --auth --fork --replSet myrs --port 27018
./mongod --dbpath data3 --logpath data3/log.log --auth --fork --replSet myrs --port 27019
./mongo
rs.initiate()
use admin
db.createUser({user : 'admin',pwd : '123', roles : ["root"]})
Use admin
db.auth('admin','123')
rs.add({_id : 2, host : 'localhost:27018', hidden : true, priority : 0, votes : 0})
![Page 106: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/106.jpg)
Demo
./reset_lab.sh
./get_mongod_downloads.sh
cd 3.6/bin
mkdir data1 data2 data3
openssl rand -base64 756 > mykey.key && chmod 600 mykey.key
./mongod --dbpath data1 --logpath data1/log.log --auth --fork --replSet myrs --keyFile mykey.key
./mongod --dbpath data2 --logpath data2/log.log --auth --fork --replSet myrs --port 27018 --keyFile mykey.key
./mongod --dbpath data3 --logpath data3/log.log --auth --fork --replSet myrs --port 27019 --keyFile mykey.key
./mongo
rs.initiate()
use admin
db.createUser({user : 'admin',pwd : '123', roles : ["root"]})
use admin
db.auth('admin','123')
rs.add({_id : 2, host : 'localhost:27018'})
![Page 107: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/107.jpg)
Common issues
![Page 108: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/108.jpg)
Common issues
As all other databases we need to work proactively to keep the environment running as expected
The most common used commands to investigate the database health are
db.serverStatus()
db.currentOp()
rs.status()
rs.printSlaveReplicationInfo()
![Page 109: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/109.jpg)
Common issues
![Page 110: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/110.jpg)
Common issues
![Page 111: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/111.jpg)
11
2
Rate Our Session
![Page 112: Mongo 101 - Basics · 2018. 4. 25. · How different is MongoDB from MySQL/RDBS NoSQL and SQL are not enemies; they are made to complement each other While MongoDB is a young NoSQL](https://reader035.vdocuments.us/reader035/viewer/2022071107/5fe12339f8816940695b1c0d/html5/thumbnails/112.jpg)
Thank You!