Cài đặt MongoDB 4 trên CentOS 7

Cài đặt MongoDB 4 trên CentOS 7Cuongquach.com | MongoDB là một hệ quản trị cơ sở dữ liệu mã nguồn mở thuộc họ NoSQL. Nó được thiết kế theo kiểu hướng đối tượng, các bảng trong MongoDB được cấu trúc rất linh hoạt, cho phép các dữ liệu lưu trữ trên bảng không cần tuân theo một cấu trúc nhất định nào cả (điều này rất thích hợp để làm big data). MongoDB lưu trữ dữ liệu theo hướng tài liệu (document), các dữ liệu được lưu trữ trong document kiểu JSON nên truy vấn sẽ rất nhanh. Hôm nay qua bài viết này các bạn sẽ biết cách cài đặt dịch vụ MongoDB 4 trên CentOS 7.

Trang chủ : https://www.mongodb.com
Phiên bản : 4.x

cai-dat-mongodb-4-tren-centos-7

1. Tạo MongoDB 4 Repository

Giờ bạn sẽ tạo một file cấu hình Repository dành cho MongoDB version 4 .

# vi /etc/yum.repos.d/mongodb.repo
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc

Cập nhật thông tin Repository mới.

# yum repolist

2. Cài đặt MongoDB 4 trên CentOS 7

Giờ ta tiến hành cài đặt gói chương trình ‘mongodb-org‘ phiên bản 4. Khi bạn cài đặt gói này sẽ bao gồm các thành phần sau được cài đặt theo:

  • mongodb-org-server : chương trình dịch vụ MongoDB (mongod) với các file cấu hình, init scripts,..
  • mongodb-org-mongos : dịch vụ MongoDB Shard.
  • mongodb-org-shell : MongoDB shell, môi trường console dịch vụ MongoDB.
  • mongodb-org-tools : chứa các chương trình tiện ích hỗ trợ MongoDB như import, export, restore, dump,…

Thực hiện cài bằng lệnh ‘yum‘, sau khi cài đặt chương trình dịch vụ MongoDB 4 sẽ có tên trên hệ thống là ‘mongod’.

# yum -y install mongodb-org

Kiểm tra phiên bản của chương trình MongoDB.

# mongod --version
db version v4.0.6
git version: caa42a1f75a56c7643d0b68d3880444375ec42e3
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

Một số file hoạt động của dịch vụ mongod :

  • /etc/mongod.conf : file cấu hình dịch vụ mongod .
  • /var/log/mongodb/mongod.log : file log dịch vụ mongod .

3. Thiết lập giá trị ulimit cho mongod

Trên hệ thống Linux, thường quy định giá trị cấu hình ‘ulimit‘ nhằm giới hạn số lượng tiến trình (process), file được mở (open file), file descriptor được tạo hay share memory có thể tạo,… nếu giá trị thấp thường sẽ gây các vấn đề nhất định cho hoạt động dịch vụ MongoDB. Vì vậy ta nên tăng giới hạn cấu hình ulimit lên.

# vi /etc/security/limits.d/99-mongodb-nproc.conf
mongod soft nofile 64000
mongod hard nofile 64000
mongod soft nproc 64000
mongod hard nproc 64000

4. Khởi động dịch vụ MongoDB và kiểm tra dịch vụ

Giờ ta sẽ khởi động dịch vụ MongoDB và thiết lập dịch vụ mongod 4 khởi động cùng hệ thống CentOS 7.

# systemctl start mongod
# systemctl enable mongod

Kiểm tra trạng thái dịch vụ MongoDB.

# systemctl status mongod
● mongod.service - MongoDB Database Server
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2019-02-15 13:59:25 +07; 50s ago
Docs: https://docs.mongodb.org/manual
Main PID: 6962 (mongod)
CGroup: /system.slice/mongod.service
└─6962 /usr/bin/mongod -f /etc/mongod.conf

Feb 15 13:59:23 tos-viettelhcm-mongodb-master systemd[1]: Starting MongoDB Database Server...
Feb 15 13:59:23 tos-viettelhcm-mongodb-master mongod[6959]: about to fork child process, waiting until server is ready for connections.
Feb 15 13:59:23 tos-viettelhcm-mongodb-master mongod[6959]: forked process: 6962
Feb 15 13:59:25 tos-viettelhcm-mongodb-master mongod[6959]: child process started successfully, parent exiting
Feb 15 13:59:25 tos-viettelhcm-mongodb-master systemd[1]: Started MongoDB Database Server.

Kiểm tra xem đã có tiến trình dịch vụ mongod đang chạy chưa.

# ps axu | grep mongod
mongod 6962 1.1 0.6 1079432 53256 ? Sl 13:59 0:00 /usr/bin/mongod -f /etc/mongod.conf

Kiểm tra xem dịch vụ MongoDB đã lắng nghe port 27017 mặc định của mongod chưa nhé.

# netstat -alnpt | grep "27017"
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 6962/mongod

Thực hiện truy cập dịch vụ mongod bằng giao diện console MongoDB.

# mongo
MongoDB shell version v4.0.6
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("7a700a0a-a612-405a-ac8a-083583282231") }
MongoDB server version: 4.0.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
Server has startup warnings:
2019-02-15T14:04:57.946+0700 I STORAGE [initandlisten]
2019-02-15T14:04:57.946+0700 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-02-15T14:04:57.946+0700 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-02-15T14:04:58.609+0700 I CONTROL [initandlisten]
2019-02-15T14:04:58.609+0700 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-02-15T14:04:58.609+0700 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-02-15T14:04:58.609+0700 I CONTROL [initandlisten]
2019-02-15T14:04:58.609+0700 I CONTROL [initandlisten]
2019-02-15T14:04:58.609+0700 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-02-15T14:04:58.609+0700 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-02-15T14:04:58.609+0700 I CONTROL [initandlisten]
2019-02-15T14:04:58.609+0700 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-02-15T14:04:58.609+0700 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-02-15T14:04:58.609+0700 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()

5. Thiết lập firewall rule cho dịch vụ MongoDB

Như đã biết mặc định dịch vụ MongoDB lắng nghe trên port 27017 TCP, vì vậy nếu bạn có sử dụng các chương trình firewall khác nhau thì hãy allow kết nối đến port 27017 . Ví dụ, tường lửa ‘iptables’.

# iptables -A INPUT -p tcp --dport 27017 -j ACCEPT

Vậy là bạn đã biết cách cài đặt MongoDB 4 trên CentOS 7 rồi phải không nào. Nếu có thắc mắc gì đừng ngại comment trao đổi cùng ‘Cuongquach.com‘ nhé.

Nguồn : https://cuongquach.com/

Previous articleCài đặt Git Server truy cập qua SSH trên CentOS/Ubuntu
Next articleEbook Data Structures and Algorithms Made Easy 5th Edition (PDF)
Bạn đang theo dõi website "https://cuongquach.com/" nơi lưu trữ những kiến thức tổng hợp và chia sẻ cá nhân về Quản Trị Hệ Thống Dịch Vụ & Mạng, được xây dựng lại dưới nền tảng kinh nghiệm của bản thân mình, Quách Chí Cường. Hy vọng bạn sẽ thích nơi này !