下載路徑
當(dāng)前最新的版本是 4.0.5 搜吧,所以我們就使用這個版本蛤织。以下命令下載必要的文件
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%]
正在升級/安裝...
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
配置
安裝好了以后,會生成一個 /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:
這是一個 yaml 格式的配置文件,其中 systemLog.path 指明了 mongoDB 的系統(tǒng)運(yùn)行日志的路徑猴抹, storage.dbPath 指定了 mongoDB 數(shù)據(jù)文件存放路徑带族,processManagement.pidFilePath 指明了 mongoDB 運(yùn)行時進(jìn)程文件存放路徑, net.port 指明了 mongodDB 服務(wù)的網(wǎng)絡(luò)端口號蟀给, net.bindIp 則指定了 mongoDB 網(wǎng)絡(luò)服務(wù)綁定的 ip 地址蝙砌。直接使用這些默認(rèn)配置,mongoDB 就可以運(yùn)行起來跋理。但是择克,會存在以下的問題
1.上面這些路徑都是散落在操作系統(tǒng)的各個目錄中,在實(shí)際情況中我們想在一個存儲空間比較大的路徑下來存儲 mongoDB 的數(shù)據(jù)前普,同時將相關(guān)的一些文件都放在同一的目錄下肚邢,例如這個路徑是 /u1/mongodb 。/u1/mongodb/data 存儲 mongoDB 存儲的數(shù)據(jù)拭卿,/u1/mongodb/logs/目錄存儲日志文件骡湖。
2.默認(rèn)配置的網(wǎng)絡(luò)服務(wù)綁定地址為 127.0.0.1 ,也就意味著默認(rèn)情況下只能本機(jī)連接 mongoDB峻厚。
采用的默認(rèn)端口 27017 在一些情況下可能會遭到外部的攻擊响蕴。需要改一個不太常用的端口。
所以惠桃,我們修改了這個默認(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
啟動
如果沒有使用 mongoDB 默認(rèn)的文件目錄和默認(rèn)的端口换途,那么在啟動之前,我們需要將 selinux 的狀態(tài)設(shè)置為 Permissive
setenforce 0
執(zhí)行下面的命令啟動 mongoDB 服務(wù)
systemctl start mongod
這里有一個坑刽射,我們會發(fā)現(xiàn)經(jīng)過一個比較長時間的等待后,中間有一段過程 mongoDB 的服務(wù)已經(jīng)起來了剃执,但是過了一段時間又停止了誓禁。查看 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ù)啟動后又收到一個停止信號,然后關(guān)閉了肾档。找了很久才發(fā)現(xiàn)摹恰,在他生成的服務(wù)配置文件 /usr/lib/systemd/system/mongod.service 中指定的 pid 文件還是原來默認(rèn)的辫继。大概這個服務(wù)腳本會在服務(wù)啟動后根據(jù)這個文件來做連接測試,連接不上后就將服務(wù)停止了俗慈。將這個配置更改為和 /etc/mongod.conf 中配置的值 姑宽,然后執(zhí)行
systemctl daemon-reload
systemctl start mongod
連接
可以在服務(wù)器上通過 shell 命令連接
mongo --port 32937
修改用戶名密碼
mongo --port 32937
// 進(jìn)入admin庫
use admin
// 創(chuàng)建新的用戶
db.createUser({"user":"root",pwd:'root',"roles":[{"role":"root","db":"admin"}]})