In the world of information technology applications, there are more than a dozen commonly used database management systems. First and foremost, the most common is MySQL – an open source database management system that is fast, stable and easy to use, compatible with many operating systems. Because of its high security, it is suitable for applications that use internet access. Besides, there are other database management systems with different capabilities and applications. Today in this article will introduce to readers of a NoSQL database management system, MongoDB.
What is MongoDB?
MongoDB is an open source database, uses a document-oriented data model, and is an unstructured query language. MongoDB stores data in the direction of the document (the document), the data is stored in the JSON style document so the query will be very fast. MongoDB’s data model is a highly resilient model that allows you to combine and store data of multivariate types without having to compromise with powerful indexing, data access, and authentication rules.
Install Mongodb Ubuntu 18.04
Step 1: Import “MongoDB public GPG Key”
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -
- Install gnupg
sudo apt-get install gnupg
- After installing gnupg, import the key again
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -
Step 2: Create a list of MongoDB files
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list
Note: To know what version of Ubuntu you are using, run the command:
lsb_release -dc
Step 3: Update packages again
sudo apt-get update
Step 4: Install MongoDB packages
sudo apt-get install -y mongodb-org
To avoid the mechanism of automatically upgrading newer packages of apt-get, we can pin packages at the current installation record.
echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections
Check if MongoDB has installed successfully
sudo systemctl status mongod
Has the following output:
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset:
Active: inactive (dead)
Docs: https://docs.mongodb.org/manual
lines 1-4/4 (END)
Start MongoDB
sudo systemctl start mongod
Has the following output:
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset:
Active: active (running) since Wed 2020-05-20 08:50:25 +07; 5s ago
Docs: https://docs.mongodb.org/manual
Main PID: 12270 (mongod)
CGroup: /system.slice/mongod.service
└─12270 /usr/bin/mongod --config /etc/mongod.conf
Thg 5 20 08:50:25 Admin-pc systemd[1]: Started MongoDB Database Server.
lines 1-9/9 (END)
Turn off MongoDB
sudo systemctl stop mongod
Has the following output:
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset:
Active: inactive (dead)
Docs: https://docs.mongodb.org/manual
Thg 5 20 08:50:25 admin-pc systemd[1]: Started MongoDB Database Server.
Thg 5 20 08:55:56 admin-pc systemd[1]: Stopping MongoDB Database Server...
Thg 5 20 08:55:56 admin-pc systemd[1]: Stopped MongoDB Database Server.
Restart MongoDB
Bạn cũng có thể restart nó như những service khác.
sudo systemctl restart mongod
Use basic MongoDB
Create a new Database
Move to the MongoDB console area.
When mongoDB is active.
We use the command:
mongo
Has the following output:
MongoDB shell version v4.2.6
connectingto:mongodb://127.0.0.1:27017/compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("c412302c-ec34-4420-9ac2-238b5d9efb89") }
MongoDB server version: 4.2.6
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
---
>
Create Database
> use [database_name]
For example: > use testlocal
Check db is running current:
> db
testlocal
Insert record into the newly created db.
> db.testlocal.insert({ nameBlog: "cài đặt mongodb trên ubuntu" })
Output insert successful:
WriteResult({ "nInserted" : 1 })
Show all databases, but this command only displays the default of mongo and the databases that I created already have data. We use the command.
> show dbs
The output is as follows:
admin 0.000GB
config 0.000GB
local 0.000GB
testlocal 0.000GB
Create a new user
By default, MongoDB does not have a default administrator account. Instead it creates different users for each database. However, you also need to create users with certain permissions for each database.
The db.createUser function, like every other function in MongoDB, takes parameters in JSON. This function needs a name, a password, a database and its permissions.
To set permissions for a user to access a database created. We can create a new_user into the testlocal database whose roles are readWrite on this db, the command is as follows:
> db.createUser(
{
user: "new_user",
pwd: "123456",
roles: [ { role: "readWrite", db: "testlocal" } ]
}
)
Create new user successfully, we have the following output:
Successfully added user: {
"user" : "new_user",
"roles" : [
{
"role" : "readWrite",
"db" : "testlocal"
}
]
}
Once inside the MongoDB console, you can view the instructions using its help command.
> help
Show all users
This command helps display all users who have access to a database.
> show users
Its output:
{
"_id" : "testlocal.new_user",
"userId" : UUID("854932c1-ec91-4ba4-9d25-fa82858b3b90"),
"user" : "new_user",
"db" : "testlocal",
"roles" : [
{
"role" : "readWrite",
"db" : "testlocal"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
Delete a database
> db.dropDatabase()
Output when deleted successfully.
{ "dropped" : "testlocal", "ok" : 1 }
Exit console mongoDB
> exit
Connect Database by username and password.
mongo -u username -p password host:port/database_name
For example: mongo -u new_user -p 123456 localhost:27017/admin
summary
Above is how to install mongodb on ubuntu 18.04 and basic usage when starting.
If anything goes wrong expect everyone’s contribution.
Thank you
Here are the reference links:
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/
https://vinasupport.com/mongodb-la-gi-huong-dan-cai-dat-mongodb-tren-ubuntu/
Link for reference on roles in mongoDB:
https://stackjava.com/mongodb/cac-loai-roles-vai-tro-quyen-trong-mongodb.html