共計(jì) 6879 個(gè)字符,預(yù)計(jì)需要花費(fèi) 18 分鐘才能閱讀完成。
本篇內(nèi)容介紹了“如何使用 Yum 安裝 MongoDB Linux 版”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓丸趣 TV 小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
1、環(huán)境說明
本文使用紅帽 6.5 進(jìn)行實(shí)驗(yàn)。
[root@oracle-test ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.5 (Santiago)
[root@oracle-test ~]# uname -r
2.6.32-431.el6.x86_64
安裝目標(biāo)版本為 MongoDB 最新版本 3.4。
2、使用 yum 進(jìn)行安裝
從 MongoDB 官方網(wǎng)站上,獲取 yum 資源庫地址。創(chuàng)建對(duì)應(yīng)的 Repo 文件。
[root@oracle-test ~]# cd /etc/yum.repos.d/
[root@oracle-test yum.repos.d]# ls -l
total 12
-rw-r–r–. 1 root root 74 Dec 28 2016 localyum.repo
-rw-r–r–. 1 root root 198 Jul 16 13:02 mongodb-org-3.4.repo
-rw-r–r–. 1 root root 636 Sep 14 2016 zabbix.repo
對(duì)應(yīng)信息:
[root@oracle-test yum.repos.d]# cat mongodb-org-3.4.repo
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64
gpgcheck=1
enable=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
調(diào)用 yum 進(jìn)行安裝。注意:在 yum 安裝對(duì)象上,包括幾個(gè)對(duì)象,分別表示不同的安裝內(nèi)容:
ü mongodb-org:MongoDB 全集信息,包括 Server、Client(Mongo Shell)、各類型工具;
ü mongodb-org-server:服務(wù)器組件;
ü mongodb-org-shell:MongoDB Shell 組件,類似于 sqlplus;
ü mongodb-org-tools:備份還原工具、數(shù)據(jù)導(dǎo)入導(dǎo)出等;
[root@oracle-test yum.repos.d]# yum install -y mongodb-org
Loaded plugins: product-id, refresh-packagekit, security, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Setting up Install Process
Resolving Dependencies
— Running transaction check
— Package mongodb-org.x86_64 0:3.4.6-1.el6 will be installed
— Processing Dependency: mongodb-org-tools = 3.4.6 for package: mongodb-org-3.4.6-1.el6.x86_64
— Processing Dependency: mongodb-org-shell = 3.4.6 for package: mongodb-org-3.4.6-1.el6.x86_64
— Processing Dependency: mongodb-org-server = 3.4.6 for package: mongodb-org-3.4.6-1.el6.x86_64
— Processing Dependency: mongodb-org-mongos = 3.4.6 for package: mongodb-org-3.4.6-1.el6.x86_64
— Running transaction check
— Package mongodb-org-mongos.x86_64 0:3.4.6-1.el6 will be installed
— Package mongodb-org-server.x86_64 0:3.4.6-1.el6 will be installed
— Package mongodb-org-shell.x86_64 0:3.4.6-1.el6 will be installed
— Package mongodb-org-tools.x86_64 0:3.4.6-1.el6 will be installed
(篇幅原因,有省略 ……)
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Dependency Installed:
mongodb-org-mongos.x86_64 0:3.4.6-1.el6
mongodb-org-server.x86_64 0:3.4.6-1.el6
mongodb-org-shell.x86_64 0:3.4.6-1.el6
mongodb-org-tools.x86_64 0:3.4.6-1.el6
Complete!
[root@oracle-test yum.repos.d]#
另外,在國內(nèi)的服務(wù)器上,由于網(wǎng)絡(luò)的原因,可能會(huì)有下載中間超時(shí)中斷的情況。可以重試幾次,或者知道下載地址之后,單獨(dú)通過支持?jǐn)帱c(diǎn)程序進(jìn)行下載。
3、后續(xù)配置
使用 yum 方式若干好處,一個(gè)是默認(rèn)就有配置文件進(jìn)行程序控制,另一個(gè)就是自動(dòng)以操作系統(tǒng)服務(wù) Service 的方式組織。
同 Oracle 一樣,我們建議將 Selinux 關(guān)閉。
[root@oracle-test yum.repos.d]# vi /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing – SELinux security policy is enforced.
# permissive – SELinux prints warnings instead of enforcing.
# disabled – No SELinux policy is loaded.
SELINUX=disabled
以操作系統(tǒng)服務(wù)方式運(yùn)行,避免出現(xiàn)單獨(dú) shell 執(zhí)行的情況。
[root@oracle-test ~]# chkconfig | grep mongod
mongod 0:off 1:off 2:off 3:on 4:off 5:on 6:off
[root@oracle-test ~]# service mongod status
mongod (pid 2078) is running…
[root@oracle-test log]# ps -ef | grep mongo
mongod 2078 1 0 14:25 ? 00:00:01 /usr/bin/mongod -f /etc/mongod.conf
root 2419 2336 0 14:29 pts/0 00:00:00 grep mongo
[root@oracle-test log]# id mongod
uid=495(mongod) gid=490(mongod) groups=490(mongod)
觀察幾個(gè)方面問題:
首先,rpm 安裝程序創(chuàng)建了單獨(dú)用戶 mongod 來作為運(yùn)行程序主體。其次,mongod 啟動(dòng)參數(shù)以配置文件 /etc/mongod.conf 的方式保存在操作系統(tǒng)上,可以編輯運(yùn)行。第三,chkconfig 中可以看到開機(jī)自動(dòng)啟動(dòng)程序服務(wù)。
日志和數(shù)據(jù)文件上,tarball 安裝比較“簡陋”,都是 /data/db 目錄和直接輸入到屏幕上。使用 mongod.conf 配置方式后,這種靈活性要好很多。
日志文件:
[root@oracle-test ~]# cd /var/log/
[root@oracle-test log]# ls -l | grep mongo
drwxr-xr-x. 2 mongod mongod 4096 Jul 16 14:16 mongodb
[root@oracle-test mongodb]# pwd
/var/log/mongodb
[root@oracle-test mongodb]# ls -l
total 4
-rw-r—–. 1 mongod mongod 4062 Jul 16 14:25 mongod.log
初始配置文件項(xiàng)目:
[root@oracle-test mongodb]# cd /etc
[root@oracle-test etc]# ls -l | grep mongod.conf
-rw-r–r–. 1 root root 768 Jul 6 02:55 mongod.conf
[root@oracle-test etc]# cat mongod.conf
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log –日志文件位置
# Where and how to store data.
storage:
dbPath: /var/lib/mongo – 存儲(chǔ)數(shù)據(jù)位置
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1 # Listen to local interface only, comment to listen on all interfaces.
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
各種配置項(xiàng)目,可以參見 Mongodb 官方文檔信息:https://docs.mongodb.com/manual/reference/configuration-options/。
4、參數(shù)修改實(shí)驗(yàn)
嘗試在 mongod.conf 文件上進(jìn)行簡單的修改。對(duì)于 MongoDB,除了 27017 端口之外,還會(huì)有一個(gè) 28017 的 Web 方式訪問接口。默認(rèn)是不開放的,需要使用配置文件開啟。
net:
port: 27017
bindIp: 127.0.0.1 # Listen to local interface only, comment to listen on all interfaces.
http:
enabled: true
JSONPEnabled: true
RESTInterfaceEnabled: true
mongod.conf 47L, 847C written
重新啟動(dòng) mongod 服務(wù)。
[root@oracle-test etc]# service mongod restart
Stopping mongod: [ OK ]
Starting mongod: [ OK ]
2017-07-16T14:44:12.957+0800 I CONTROL [initandlisten]
2017-07-16T14:44:12.960+0800 I NETWORK [websvr] admin web console waiting for connections on port 28017
2017-07-16T14:44:12.961+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory /var/lib/mongo/diagnostic.data
2017-07-16T14:44:12.961+0800 I NETWORK [thread1] waiting for connections on port 27017
注意:這個(gè)時(shí)候,只能進(jìn)行本地登錄,從遠(yuǎn)程客戶端使用 shell 或者網(wǎng)頁都不能訪問。
C:\Users\admin mongo –host 172.xx.xx.xxx
MongoDB shell version v3.4.5
connecting to: mongodb://172.xx.xx.xxx:27017/
2017-07-16T15:01:45.913+0800 W NETWORK [thread1] Failed to connect to 172.16.19
.143:27017 after 5000ms milliseconds, giving up.
2017-07-16T15:01:45.914+0800 E QUERY [thread1] Error: couldn t connect to ser
ver 172.xx.xx.xxx:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:237:13
@(connect):1:6
exception: connect failed
原因在于 net 參數(shù)的 bindIP 內(nèi)容,默認(rèn)情況為:
net:
port: 27017
bindIp: 127.0.0.1 # Listen to local interface only, comment to listen on all interfaces.
需要修改為 0.0.0.0,否則只能在本地項(xiàng)目。
net:
port: 27017
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.
[root@oracle-test etc]# service mongod restart
Stopping mongod: [ OK ]
Starting mongod: [ OK ]
C:\Users\admin mongo –host 172.xx.xx.xxx
MongoDB shell version v3.4.5
connecting to: mongodb://172.xx.xx.xxx:27017/
MongoDB server version: 3.4.6
Server has startup warnings:
(篇幅原因,有省略……)
2017-07-16T15:12:19.433+0800 I CONTROL [initandlisten]
2017-07-16T15:12:19.433+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits
too low. rlimits set to 1024 processes, 64000 files. Number of processes should
be at least 32000 : 0.5 times number of files.
2017-07-16T15:12:19.433+0800 I CONTROL [initandlisten]
對(duì)于出現(xiàn)的 soft rlimits 提示,可以修改 limits.conf 文件,設(shè)置額外的限制數(shù)目。
[root@oracle-test ~]# vi /etc/security/limits.conf
#@student – maxlogins 4
mongod soft nofile 64000
mongod hard nofile 64000
mongod soft nproc 32000
mongod hard nproc 32000
[root@oracle-test ~]# service mongod restart
Stopping mongod: [ OK ]
Starting mongod: [ OK ]
5、結(jié)論
本文介紹了使用 yum 方式安裝 mongodb 的方法。這種方法下,系統(tǒng)出錯(cuò)的情況會(huì)更少一些,配置項(xiàng)目也更加清晰。
“如何使用 Yum 安裝 MongoDB Linux 版”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注丸趣 TV 網(wǎng)站,丸趣 TV 小編將為大家輸出更多高質(zhì)量的實(shí)用文章!