[Practice Labs] Cài đặt MongoDB 3.x lên Ubuntu

Thảo luận về NoSQL và các sản phẩm NoSQL phổ biến như: MongoDB, CouchDB, Cassandra, Oracle NoSQL, ...
Post Tue Apr 19, 2016 10:55 am
User avatar

nhsang

Administrator
Administrator
Posts: 3693
Joined: Mon Jul 16, 2007 1:38 pm
Location: Sài Gòn

[Practice Labs] Cài đặt MongoDB 3.x lên Ubuntu

Chào các bạn,

Với tinh thần vừa học vừa làm, mình đã ghi lại các bước cài đặt (trong vai trò 1 beginner) để bạn nào mới tìm hiểu MongoDB có thể làm theo dễ dàng mà không cần phải "google" nhiều. Đồng thời cũng muốn nhờ những chuyên gia NoSQL chỉ ra những điểm cần chỉnh sửa để tốt hơn.


1. Môi trường cài đặt:

- Máy ảo: VirtualBox
- OS: Ubuntu 14.04.4 x64
- MongoDB 3.2: cài đặt qua gói .deb

Ở đây mặc nhiên là các bạn biết cách download VirtualBox + Ubuntu, sau đó cài đặt Ubuntu lên máy ảo VirtualBox nhé.
Lưu ý là cấu hình Network của máy ảo có thể ra Internet được, ở đây mình dùng Bridged Adapter.

2. Cài đặt MongoDB

- Login vào máy ảo Ubuntu.
- Mở cửa sổ Terminal, dùng lệnh sudo -i để chuyển sang root.
sangnguyen@UbuntuVM:~$ sudo -i 
[sudo] password for sangnguyen:
root@UbuntuVM:~#
root@UbuntuVM:~#

- Xem IP của máy, đặt luôn vào /etc/hosts
Trong trường hợp máy dùng làm server thì đặt luôn Static IP nhé.
root@UbuntuVM:~# ifconfig
eth0 Link encap:Ethernet HWaddr 08:00:27:b5:5c:24
inet addr:192.168.56.101 Bcast:192.168.56.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:feb5:5c24/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:21 errors:0 dropped:0 overruns:0 frame:0
TX packets:66 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:4360 (4.3 KB) TX bytes:10364 (10.3 KB)

root@UbuntuVM:~# cat /etc/hosts
127.0.0.1 localhost
192.168.56.101 UbuntuVM
192.168.56.102 UbuntuVM-RS1


- Tạo file /etc/apt/sources.list.d/mongodb-org-3.2.list, thêm vào một dòng như bên dưới:
root@UbuntuVM:~#  vi /etc/apt/sources.list.d/mongodb-org-3.2.list 
deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse


- Tiếp theo chạy lần lượt 3 lệnh bên dưới

apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
apt-get update
apt-get install -y mongodb-org


Kết quả trông như thế này:

Code: Select all
root@UbuntuVM:~# apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927

Executing: gpg --ignore-time-conflict --no-options --no-default-keyring --homedir /tmp/tmp.GQRx1N78LM --no-auto-check-trustdb --trust-model always --keyring /etc/apt/trusted.gpg --primary-keyring /etc/apt/trusted.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
gpg: requesting key EA312927 from hkp server keyserver.ubuntu.com
gpg: key EA312927: "MongoDB 3.2 Release Signing Key <packaging@mongodb.com>" not changed
gpg: Total number processed: 1
gpg:              unchanged: 1

root@UbuntuVM:~# apt-get update
Ign http://repo.mongodb.org trusty/mongodb-org/3.2 InRelease
Hit http://repo.mongodb.org trusty/mongodb-org/3.2 Release.gpg
Hit http://repo.mongodb.org trusty/mongodb-org/3.2 Release
Hit http://repo.mongodb.org trusty/mongodb-org/3.2/multiverse amd64 Packages
Ign http://extras.ubuntu.com trusty InRelease
Hit http://repo.mongodb.org trusty/mongodb-org/3.2/multiverse i386 Packages
Get:1 http://security.ubuntu.com trusty-security InRelease [65,9 kB]
Ign http://vn.archive.ubuntu.com trusty InRelease
Get:2 http://extras.ubuntu.com trusty Release.gpg [72 B]
Ign http://repo.mongodb.org trusty/mongodb-org/3.2/multiverse Translation-en_US
Hit http://extras.ubuntu.com trusty Release
Ign http://repo.mongodb.org trusty/mongodb-org/3.2/multiverse Translation-en
Get:3 http://vn.archive.ubuntu.com trusty-updates InRelease [65,9 kB]
Hit http://extras.ubuntu.com trusty/main Sources
Hit http://extras.ubuntu.com trusty/main amd64 Packages
Get:4 http://security.ubuntu.com trusty-security/main Sources [110 kB]
Hit http://extras.ubuntu.com trusty/main i386 Packages
Get:5 http://vn.archive.ubuntu.com trusty-backports InRelease [65,9 kB]
Get:6 http://security.ubuntu.com trusty-security/restricted Sources [4.035 B]
Hit http://vn.archive.ubuntu.com trusty Release.gpg
Get:7 http://security.ubuntu.com trusty-security/universe Sources [35,2 kB]
Get:8 http://vn.archive.ubuntu.com trusty-updates/main Sources [271 kB]
Get:9 http://security.ubuntu.com trusty-security/multiverse Sources [2.764 B]
Get:10 http://security.ubuntu.com trusty-security/main amd64 Packages [455 kB]
Ign http://extras.ubuntu.com trusty/main Translation-en_US
Get:11 http://vn.archive.ubuntu.com trusty-updates/restricted Sources [5.352 B]
Ign http://extras.ubuntu.com trusty/main Translation-en
Get:12 http://vn.archive.ubuntu.com trusty-updates/universe Sources [152 kB]
Get:13 http://vn.archive.ubuntu.com trusty-updates/multiverse Sources [5.928 B]
Ign http://dl.google.com stable InRelease
Get:14 http://dl.google.com stable Release.gpg [181 B]
Get:15 http://dl.google.com stable Release [1.189 B]
Get:16 http://dl.google.com stable/main amd64 Packages [1.378 B]
Get:17 http://vn.archive.ubuntu.com trusty-updates/main amd64 Packages [752 kB]
Ign http://dl.google.com stable/main Translation-en_US
Ign http://dl.google.com stable/main Translation-en
Get:18 http://security.ubuntu.com trusty-security/restricted amd64 Packages [13,0 kB]
Get:19 http://security.ubuntu.com trusty-security/universe amd64 Packages [126 kB]
Get:20 http://security.ubuntu.com trusty-security/multiverse amd64 Packages [4.982 B]
Get:21 http://security.ubuntu.com trusty-security/main i386 Packages [429 kB]
Get:22 http://vn.archive.ubuntu.com trusty-updates/restricted amd64 Packages [15,9 kB]
Get:23 http://vn.archive.ubuntu.com trusty-updates/universe amd64 Packages [358 kB]
Get:24 http://security.ubuntu.com trusty-security/restricted i386 Packages [12,7 kB]
Get:25 http://vn.archive.ubuntu.com trusty-updates/multiverse amd64 Packages [13,2 kB]
Get:26 http://security.ubuntu.com trusty-security/universe i386 Packages [126 kB]
Get:27 http://vn.archive.ubuntu.com trusty-updates/main i386 Packages [718 kB]
Get:28 http://security.ubuntu.com trusty-security/multiverse i386 Packages [5.172 B]
Get:29 http://security.ubuntu.com trusty-security/main Translation-en [250 kB]
Get:30 http://security.ubuntu.com trusty-security/multiverse Translation-en [2.570 B]
Get:31 http://security.ubuntu.com trusty-security/restricted Translation-en [3.206 B]
Get:32 http://security.ubuntu.com trusty-security/universe Translation-en [74,5 kB]
Get:33 http://vn.archive.ubuntu.com trusty-updates/restricted i386 Packages [15,6 kB]
Get:34 http://vn.archive.ubuntu.com trusty-updates/universe i386 Packages [358 kB]
Get:35 http://vn.archive.ubuntu.com trusty-updates/multiverse i386 Packages [13,6 kB]
Get:36 http://vn.archive.ubuntu.com trusty-updates/main Translation-en [376 kB]
Get:37 http://vn.archive.ubuntu.com trusty-updates/multiverse Translation-en [7.227 B]
Get:38 http://vn.archive.ubuntu.com trusty-updates/restricted Translation-en [3.699 B]
Get:39 http://vn.archive.ubuntu.com trusty-updates/universe Translation-en [187 kB]
Get:40 http://vn.archive.ubuntu.com trusty-backports/main Sources [8.908 B]
Get:41 http://vn.archive.ubuntu.com trusty-backports/restricted Sources [28 B]
Get:42 http://vn.archive.ubuntu.com trusty-backports/universe Sources [34,5 kB]
Get:43 http://vn.archive.ubuntu.com trusty-backports/multiverse Sources [1.898 B]
Get:44 http://vn.archive.ubuntu.com trusty-backports/main amd64 Packages [9.988 B]
Get:45 http://vn.archive.ubuntu.com trusty-backports/restricted amd64 Packages [28 B]
Get:46 http://vn.archive.ubuntu.com trusty-backports/universe amd64 Packages [41,5 kB]
Get:47 http://vn.archive.ubuntu.com trusty-backports/multiverse amd64 Packages [1.571 B]
Get:48 http://vn.archive.ubuntu.com trusty-backports/main i386 Packages [9.986 B]
Get:49 http://vn.archive.ubuntu.com trusty-backports/restricted i386 Packages [28 B]
Get:50 http://vn.archive.ubuntu.com trusty-backports/universe i386 Packages [41,5 kB]
Get:51 http://vn.archive.ubuntu.com trusty-backports/multiverse i386 Packages [1.552 B]
Get:52 http://vn.archive.ubuntu.com trusty-backports/main Translation-en [5.986 B]
Get:53 http://vn.archive.ubuntu.com trusty-backports/multiverse Translation-en [1.215 B]
Get:54 http://vn.archive.ubuntu.com trusty-backports/restricted Translation-en [28 B]
Get:55 http://vn.archive.ubuntu.com trusty-backports/universe Translation-en [36,0 kB]
Hit http://vn.archive.ubuntu.com trusty Release
Hit http://vn.archive.ubuntu.com trusty/main Sources
Hit http://vn.archive.ubuntu.com trusty/restricted Sources
Hit http://vn.archive.ubuntu.com trusty/universe Sources
Hit http://vn.archive.ubuntu.com trusty/multiverse Sources
Hit http://vn.archive.ubuntu.com trusty/main amd64 Packages
Hit http://vn.archive.ubuntu.com trusty/restricted amd64 Packages
Hit http://vn.archive.ubuntu.com trusty/universe amd64 Packages
Hit http://vn.archive.ubuntu.com trusty/multiverse amd64 Packages
Hit http://vn.archive.ubuntu.com trusty/main i386 Packages
Hit http://vn.archive.ubuntu.com trusty/restricted i386 Packages
Hit http://vn.archive.ubuntu.com trusty/universe i386 Packages
Hit http://vn.archive.ubuntu.com trusty/multiverse i386 Packages
Hit http://vn.archive.ubuntu.com trusty/main Translation-en
Hit http://vn.archive.ubuntu.com trusty/multiverse Translation-en
Hit http://vn.archive.ubuntu.com trusty/restricted Translation-en
Hit http://vn.archive.ubuntu.com trusty/universe Translation-en
Ign http://vn.archive.ubuntu.com trusty/main Translation-en_US
Ign http://vn.archive.ubuntu.com trusty/multiverse Translation-en_US
Ign http://vn.archive.ubuntu.com trusty/restricted Translation-en_US
Ign http://vn.archive.ubuntu.com trusty/universe Translation-en_US
Reading package lists... Done

root@UbuntuVM:~# apt-get install -y mongodb-org
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools
The following NEW packages will be installed:
  mongodb-org mongodb-org-mongos mongodb-org-server mongodb-org-shell
  mongodb-org-tools
0 upgraded, 5 newly installed, 0 to remove and 79 not upgraded.
Need to get 0 B/46,0 MB of archives.
After this operation, 212 MB of additional disk space will be used.
Selecting previously unselected package mongodb-org-shell.
(Reading database ... 165656 files and directories currently installed.)
Preparing to unpack .../mongodb-org-shell_3.2.4_amd64.deb ...
Unpacking mongodb-org-shell (3.2.4) ...
Selecting previously unselected package mongodb-org-server.
Preparing to unpack .../mongodb-org-server_3.2.4_amd64.deb ...
Unpacking mongodb-org-server (3.2.4) ...
Selecting previously unselected package mongodb-org-mongos.
Preparing to unpack .../mongodb-org-mongos_3.2.4_amd64.deb ...
Unpacking mongodb-org-mongos (3.2.4) ...
Selecting previously unselected package mongodb-org-tools.
Preparing to unpack .../mongodb-org-tools_3.2.4_amd64.deb ...
Unpacking mongodb-org-tools (3.2.4) ...
Selecting previously unselected package mongodb-org.
Preparing to unpack .../mongodb-org_3.2.4_amd64.deb ...
Unpacking mongodb-org (3.2.4) ...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
Processing triggers for ureadahead (0.100.0-16) ...
Setting up mongodb-org-shell (3.2.4) ...
Setting up mongodb-org-server (3.2.4) ...
mongod start/running, process 4957
Setting up mongodb-org-mongos (3.2.4) ...
Setting up mongodb-org-tools (3.2.4) ...
Processing triggers for ureadahead (0.100.0-16) ...
Setting up mongodb-org (3.2.4) ...
root@UbuntuVM:~#


Như vậy việc cài đặt xem như đã xong, tiếp theo chúng ta cần cấu chỉnh sửa cấu hình một chút theo ý mình.

3. Cấu hình

MongoDB lưu cấu hình trong file /etc/mongod.conf theo định dạng YAML, vì vậy khi chỉnh sửa lưu ý theoc ấu trúc này nhé.

Ở đây chúng ta sẽ chỉnh sử "nhẹ" một chút để mongod có thể listen được các request từ bên ngoài (mặc nhiên chỉ listen ở localhost)
root@UbuntuVM:~# vi /etc/mongod.conf

Add IP vào dòng bindIP
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1,192.168.56.101


Restart lại mongod service.
root@UbuntuVM:~# service mongod restart
mongod stop/waiting
mongod start/running, process 5208


Mặc nhiên, MongoDB sẽ listen ở port 27017 (trong file mongo.conf), nên có thể kiểm tra xem nó đã open chưa:
root@UbuntuVM:~# netstat -na| grep 27017
tcp 0 0 192.168.56.101:27017 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN
unix 2 [ ACC ] STREAM LISTENING 22824 /tmp/mongodb-27017.sock
root@UbuntuVM:~#


4. Kết nối
Có nhiều ứng dụng để kết nối với MongoDB, ở đây tôi dùng NoSQL Manager for MongoDB for Windows

Image

Thành quả:
Image

Nhìn chung việc cài đặt cũng không phức tạp lắm.

Nếu AE có hứng thú, tiếp theo sẽ làm một số cấu hình tiêu biểu như REPLICATION và SHARDING. :K




Nguyễn Hoàng Sang
sangnh.oravn@gmail.com


Post Tue Apr 19, 2016 4:47 pm
User avatar

nhsang

Administrator
Administrator
Posts: 3693
Joined: Mon Jul 16, 2007 1:38 pm
Location: Sài Gòn

Fix cái vụ báo lổi lằng nhằng khi start mongo.

Warning như bên dưới khi khởi động:
2016-04-11T11:59:31.154+0700 I CONTROL  [initandlisten] 
2016-04-11T11:59:31.154+0700 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always
'.
2016-04-11T11:59:31.154+0700 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-04-11T11:59:31.154+0700 I CONTROL [initandlisten]
2016-04-11T11:59:31.154+0700 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'
.
2016-04-11T11:59:31.154+0700 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-04-11T11:59:31.154+0700 I CONTROL [initandlisten]
......


Xử lý bằng cách thêm đọan sau vào file: /etc/rc.local (trên lệnh exit 0)
Sau đó restart máy.

if test -f /sys/kernel/mm/transparent_hugepage/khugepaged/defrag; then
echo 0 > /sys/kernel/mm/transparent_hugepage/khugepaged/defrag
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi




Nguyễn Hoàng Sang
sangnh.oravn@gmail.com



Return to NoSQL

Who is online

Users browsing this forum: No registered users and 0 guests

Youtube Channel
cron