Hướng dẫn cài đặt MongoDB Community Edition trên Linux

MongoDB là một cơ sở dữ liệu hướng tài liệu NoSQL miễn phí, đa nền tảng, mã nguồn mở. MongoDB có sẵn dưới dạng hai phiên bản, phiên bản Community và Enterprise. Phiên bản Community MongoDB miễn phí cho việc sử dụng cá nhân, trong khi phiên bản Enterprise là phiên bản trả phí có nhiều tính năng và hỗ trợ chính thức hơn so với phiên bản cộng đồng. Trong hướng dẫn ngắn gọn này, chúng ta sẽ xem cách cài đặt phiên bản cộng đồng MongoDB trong Linux.

In Rhel / CentOS / Scientific Linux:

$ sudo vi /etc/selinux/config

Tắt SELINUX

SELINUX=disabled

Khởi động lại hệ thống.

Thêm MongoDB repository và cài đặt với yum.

Tạo /etc/yum.repos.d/mongodb.repo :

$ sudo vi /etc/yum.repos.d/mongodb.repo

Thêm đoạn bên dưới:

[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc

Lưu lại và thoát rồi cập nhập repository bằng command:

$ sudo yum update

Cài MongoDB bằng command:

$ sudo yum install mongodb-org

Khởi động MongoDB service dùng command:

$ sudo systemctl start mongod

Kiểm tra service đã khởi động chưa dùng:

$ sudo systemctl status mongod

Restart hoăc stop service dùng:

$ sudo systemctl restart mongod
$ sudo systemctl stop mongod

Khởi động MongoDB service khi reboot dùng:

$ sudo systemctl enable mongod

In Debian:

Import MongoDB public key:

$ wget -qO - https://www.mongodb.org/static/pgp/server-4.0.asc | sudo apt-key add -

Tạo file /etc/apt/sources.list.d/mongodb.list:

$ sudo touch /etc/apt/sources.list.d/mongodb.list

Trên Debian 8, chạy command và thêm MongoDB repository:

echo "deb http://repo.mongodb.org/apt/debian jessie/mongodb-org/4.0 main" | sudo tee /etc/apt/sources.list.d/mongodb.list

Trên Debian 9:

echo "deb http://repo.mongodb.org/apt/debian stretch/mongodb-org/4.0 main" | sudo tee /etc/apt/sources.list.d/mongodb.list

Cập nhập repository list:

$ sudo apt-get update

Cài đặt Mongodb dùng command:

$ sudo apt-get install mongodb-org

Khởi động MongoDB service dùng command:

$ sudo service mongod start

Restart/stop dùng command:

$ sudo service mongod restart
$ sudo service mongod stop

In Ubuntu:

Import the MongoDB public key:

$ wget -qO - https://www.mongodb.org/static/pgp/server-4.0.asc | sudo apt-key add -

Thêm MongoDB repository trên Ubuntu.

Tạo repository file dùng command:

$ sudo touch /etc/apt/sources.list.d/mongodb.list

Chạy command sau để thêm MongoDB repository URL in mongodb repository file:

Trên Ubuntu 18.04 LTS:

$ echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb.list

Trên Ubuntu 16.04 LTS:

$ echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb.list

Trên Ubuntu 14.04 LTS:

$ echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb.list

Cập nhập repository dùng command:

$ sudo apt-get update

Cài đặt MongoDB dùng command:

$ sudo apt-get install mongodb-org

Khởi động MongoDB service:

$ sudo systemctl start mongod

Kiểm tra status mongodb service:

$ sudo systemctl status mongod
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: en
Active: active (running) since Wed 2019-03-13 11:00:27 UTC; 1s ago
Docs: https://docs.mongodb.org/manual
Main PID: 2230 (mongod)
CGroup: /system.slice/mongod.service
└─2230 /usr/bin/mongod --config /etc/mongod.conf

Mar 13 11:00:27 ubuntuserver systemd[1]: Started MongoDB Database Server

Restart/stop service dùng:

$ sudo systemctl restart mongod
$ sudo systemctl stop mongod

Dùng command để start service tự động khi reboot.

$ sudo systemctl enable mongod

On openSUSE:

Import MongoDB key:

$ sudo rpm --import https://www.mongodb.org/static/pgp/server-4.0.asc

Thêm MongoDB dùng command:

$ sudo zypper addrepo --gpgcheck "https://repo.mongodb.org/zypper/suse/12/mongodb-org/4.0/x86_64/" mongodb

Cài đặt MongoDB:

$ sudo zypper -n install mongodb-org

Khởi động MongoDB service:

$ sudo service mongod start

Restart the service dùng:

$ sudo service mongod restart

Để stop dùng:

$ sudo service mongod stop

Cho phép service khởi động cùng hệ thống khi reboot:

$ sudo chkconfig mongod on

Sau khi cài MongoDB, chạy command để log in to mongo shell.

mongo

Thoát dùng:

exit

Uninstall MongoDB

Để remove MongoDB từ hệ thống.

First, stop the service:

$ sudo systemctl stop mongod

Or,

$ sudo service mongod stop

Next, remove the mongodb packages using command:

On RHEL / CentOS:

$ sudo yum erase $(rpm -qa | grep mongodb-org)

On Debian:

$ sudo apt-get purge mongodb-org*

On openSUSE:

$ sudo zypper remove $(rpm -qa | grep mongodb-org)

Finally, delete database và log files.

$ sudo rm -r /var/log/mongodb
$ sudo rm -r /var/lib/mongo

Chúc các bạn thành công!

Bài viết liên quan

Leave a Reply

Your email address will not be published. Required fields are marked *