前言
以前自己沒有用過 mongoDB 恩商,這次有個(gè)遷移環(huán)境的工作,所以需要安裝 mongoDB 振愿,在這個(gè)過程中有一些坑毅糟,所以記錄一下红选,已備后期查詢
下載文件
當(dāng)前最新的版本是 4.0.5 ,所以我們就使用這個(gè)版本留特。以下命令下載必要的文件
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/RPMS/mongodb-org-server-4.0.5-1.el7.x86_64.rpm
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/RPMS/mongodb-org-shell-4.0.5-1.el7.x86_64.rpm
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/RPMS/mongodb-org-tools-4.0.5-1.el7.x86_64.rpm
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/RPMS/mongodb-org-mongos-4.0.5-1.el7.x86_64.rpm
安裝程序
執(zhí)行如下命令安裝 mongoDB 的服務(wù)端程序
[root@localhost u1]# rpm -ivh mongodb-org-server-4.0.5-1.el7.x86_64.rpm
警告:mongodb-org-server-4.0.5-1.el7.x86_64.rpm: 頭V3 RSA/SHA1 Signature, 密鑰 ID e52529d4: NOKEY
準(zhǔn)備中... ################################# [100%]
正在升級(jí)/安裝...
1:mongodb-org-server-4.0.5-1.el7 ################################# [100%]
Created symlink from /etc/systemd/system/multi-user.target.wants/mongod.service to /usr/lib/systemd/system/mongod.service.
這僅僅安裝了 mongoDB 的服務(wù)端程序纠脾,如果還需要用命令行連接 mongoDB玛瘸,則需要安裝 shell 程序包
rpm -ivh mongodb-org-shell-4.0.5-1.el7.x86_64.rpm
如果需要一些附加工具,例如數(shù)據(jù)導(dǎo)入導(dǎo)出苟蹈,則需要安裝 tool 程序包
rpm -ivh mongodb-org-tools-4.0.5-1.el7.x86_64.rpm
如果要部署集群糊渊,則還需要安裝 mongos 程序包
rpm -ivh mongodb-org-mongos-4.0.5-1.el7.x86_64.rpm
配置
安裝好了以后,會(huì)生成一個(gè) /etc/mongod.conf 的配置文件慧脱,配置了 mogoDB 默認(rèn)的配置渺绒。默認(rèn)的配置文件如下所示
# 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
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
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
這是一個(gè) yaml 格式的配置文件,其中 systemLog.path 指明了 mongoDB 的系統(tǒng)運(yùn)行日志的路徑菱鸥, storage.dbPath 指定了 mongoDB 數(shù)據(jù)文件存放路徑宗兼,processManagement.pidFilePath 指明了 mongoDB 運(yùn)行時(shí)進(jìn)程文件存放路徑, net.port 指明了 mongodDB 服務(wù)的網(wǎng)絡(luò)端口號(hào)氮采, net.bindIp 則指定了 mongoDB 網(wǎng)絡(luò)服務(wù)綁定的 ip 地址殷绍。直接使用這些默認(rèn)配置,mongoDB 就可以運(yùn)行起來鹊漠。但是主到,會(huì)存在以下的問題
- 上面這些路徑都是散落在操作系統(tǒng)的各個(gè)目錄中,在實(shí)際情況中我們想在一個(gè)存儲(chǔ)空間比較大的路徑下來存儲(chǔ) mongoDB 的數(shù)據(jù)躯概,同時(shí)將相關(guān)的一些文件都放在同一的目錄下登钥,例如這個(gè)路徑是 /u1/mongodb 。/u1/mongodb/data 存儲(chǔ) mongoDB 存儲(chǔ)的數(shù)據(jù)娶靡,/u1/mongodb/logs/目錄存儲(chǔ)日志文件牧牢。
- 默認(rèn)配置的網(wǎng)絡(luò)服務(wù)綁定地址為 127.0.0.1 ,也就意味著默認(rèn)情況下只能本機(jī)連接 mongoDB姿锭。
- 采用的默認(rèn)端口 27017 在一些情況下可能會(huì)遭到外部的攻擊塔鳍。需要改一個(gè)不太常用的端口。
所以艾凯,我們修改了這個(gè)默認(rèn)的配置文件內(nèi)容如下
# 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: /u1/mongodb/logs/mongod.log
# Where and how to store data.
storage:
dbPath: /u1/mongodb/data
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /u1/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 32937
# bindIp: 127.0.0.1 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
bindIp: 0.0.0.0 #,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
然后執(zhí)行下面的命令準(zhǔn)備好環(huán)境
mkdir /u1/mongodb
mkdir /u1/mongodb/data
mkdir /u1/mongodb/logs
chown -R mongod:mongod /u1/mongodb
啟動(dòng)
如果沒有使用 mongoDB 默認(rèn)的文件目錄和默認(rèn)的端口献幔,那么在啟動(dòng)之前,我們需要將 selinux 的狀態(tài)設(shè)置為 Permissive
setenforce 0
執(zhí)行下面的命令啟動(dòng) mongoDB 服務(wù)
systemctl start mongod
這里有一個(gè)坑趾诗,我們會(huì)發(fā)現(xiàn)經(jīng)過一個(gè)比較長時(shí)間的等待后,中間有一段過程 mongoDB 的服務(wù)已經(jīng)起來了蹬蚁,但是過了一段時(shí)間又停止了恃泪。查看 mongoDB 的日志文件 /u1/mongodb/logs/mongod.log 的內(nèi)容如下所示
2019-01-12T14:57:27.749+0800 I STORAGE [initandlisten] createCollection: admin.system.version with provided UUID: 60038dc7-8626-41fe-b6f7-beee1a0d073d
2019-01-12T14:57:27.983+0800 I COMMAND [initandlisten] setting featureCompatibilityVersion to 4.0
2019-01-12T14:57:27.993+0800 I STORAGE [initandlisten] createCollection: local.startup_log with generated UUID: 6a42e867-7f19-429d-99a4-7f051bf84208
2019-01-12T14:57:28.109+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/u1/mongodb/data/diagnostic.data'
2019-01-12T14:57:28.134+0800 I NETWORK [initandlisten] waiting for connections on port 32937
2019-01-12T14:57:28.172+0800 I STORAGE [LogicalSessionCacheRefresh] createCollection: config.system.sessions with generated UUID: ab2d4939-2403-42f2-a0b3-afd825144421
2019-01-12T14:57:28.407+0800 I INDEX [LogicalSessionCacheRefresh] build index on: config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: "lsidTTLIndex", ns: "
config.system.sessions", expireAfterSeconds: 1800 }
2019-01-12T14:57:28.407+0800 I INDEX [LogicalSessionCacheRefresh] building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2019-01-12T14:57:28.409+0800 I INDEX [LogicalSessionCacheRefresh] build index done. scanned 0 total records. 0 secs
2019-01-12T14:57:28.409+0800 I COMMAND [LogicalSessionCacheRefresh] command config.$cmd command: createIndexes { createIndexes: "system.sessions", indexes: [ { key: { lastUse:
1 }, name: "lsidTTLIndex", expireAfterSeconds: 1800 } ], $db: "config" } numYields:0 reslen:114 locks:{ Global: { acquireCount: { r: 2, w: 2 } }, Database: { acquireCount: { w
: 2, W: 1 } }, Collection: { acquireCount: { w: 2 } } } protocol:op_msg 236ms
2019-01-12T14:58:55.458+0800 I CONTROL [signalProcessingThread] got signal 15 (Terminated), will terminate after current cmd ends
2019-01-12T14:58:55.458+0800 I NETWORK [signalProcessingThread] shutdown: going to close listening sockets...
2019-01-12T14:58:55.459+0800 I NETWORK [signalProcessingThread] removing socket file: /tmp/mongodb-32937.sock
2019-01-12T14:58:55.459+0800 I CONTROL [signalProcessingThread] Shutting down free monitoring
2019-01-12T14:58:55.460+0800 I FTDC [signalProcessingThread] Shutting down full-time diagnostic data capture
2019-01-12T14:58:55.464+0800 I STORAGE [signalProcessingThread] WiredTigerKVEngine shutting down
2019-01-12T14:58:55.909+0800 I STORAGE [signalProcessingThread] shutdown: removing fs lock...
2019-01-12T14:58:55.909+0800 I CONTROL [signalProcessingThread] now exiting
2019-01-12T14:58:55.909+0800 I CONTROL [signalProcessingThread] shutting down with code:0
服務(wù)啟動(dòng)后又收到一個(gè)停止信號(hào),然后關(guān)閉了犀斋。找了很久才發(fā)現(xiàn)贝乎,在他生成的服務(wù)配置文件 /usr/lib/systemd/system/mongod.service 中指定的 pid 文件還是原來默認(rèn)的。大概這個(gè)服務(wù)腳本會(huì)在服務(wù)啟動(dòng)后根據(jù)這個(gè)文件來做連接測試叽粹,連接不上后就將服務(wù)停止了览效。將這個(gè)配置更改為和 /etc/mongod.conf 中配置的值 却舀,然后執(zhí)行
systemctl daemon-reload
systemctl start mongod
連接
可以在服務(wù)器上通過 shell 命令連接
mongo --port 32937
即可進(jìn)入 mongoDB 的交互式界面對(duì) mongoDB 進(jìn)行控制了。