(一)基礎環境設置
操作系統版本 :centos-7.4
MongoDB版本:MongoDB 4.2 社區版
(1)關閉防火牆
# 關閉防火牆 [root@mongodbenterprise lib]# systemctl stop firewalld.service # 禁止firewall開機啟動 [root@mongodbenterprise lib]# systemctl disable firewalld.service # 確認防火牆為not running狀態 [root@mongodbenterprise lib]# firewall-cmd --state not running
(2)關閉selinux
[root@mongodbenterprise lib]# vim /etc/selinux/config
SELINUX=disabled
(3)安裝依賴包
yum install -y libcurl openssl
(二)安裝MongoDB
安裝路徑規划:
安裝路徑:/opt/mongo-4.2/
數據文件路徑:/mongo/data/
錯誤日志路徑:/mongo/log/mongodb.log
配置文件:/mongo/mongodb.conf
(1)下載安裝包
需要注意的是,redhat/centos是類似的Linux系統,可以簡單地理解為:centos是redhat的社區版。因此直接下載os為redhat7的tar包即可。
(2)解壓安裝包
[root@mongoserver ~]# ls anaconda-ks.cfg mongodb-linux-x86_64-rhel70-4.2.7.tgz [root@mongoserver ~]# tar -xzvf mongodb-linux-x86_64-rhel70-4.2.7.tgz [root@mongoserver ~]# ls anaconda-ks.cfg mongodb-linux-x86_64-rhel70-4.2.7 mongodb-linux-x86_64-rhel70-4.2.7.tgz
(3)安裝MongoDB
tar包是不需要安裝的,解壓到安裝位置即可,我的安裝位置是/opt/mongo-4.2
[root@mongoserver ~]# ls anaconda-ks.cfg mongodb-linux-x86_64-rhel70-4.2.7 mongodb-linux-x86_64-rhel70-4.2.7.tgz [root@mongoserver ~]# mv mongodb-linux-x86_64-rhel70-4.2.7 /opt/ [root@mongoserver ~]# cd /opt [root@mongoserver opt]# ls mongodb-linux-x86_64-rhel70-4.2.7 [root@mongoserver opt]# mv mongodb-linux-x86_64-rhel70-4.2.7/ mongodb-4.2 [root@mongoserver opt]# ls mongodb-4.2
(4)添加配置文件/mongo/mongodb.conf
[root@mongoserver ~]# vim /mongo/mongodb.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: /mongo/log/mongodb.log # Where and how to store data. storage: dbPath: /mongo/data journal: enabled: true # engine: # wiredTiger: # how the process runs processManagement: fork: true # fork and run in background pidFilePath: /mongo/mongod.pid # location of pidfile timeZoneInfo: /usr/share/zoneinfo # network interfaces net: port: 27017 bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
創建相關路徑:
mkdir -p /mongo/log/ mkdir -p /mongo/data/
(5)將mongo的目錄添加到PATH中,以便於操作系統能識別到mongo命令
[root@mongoserver ~]# vim /etc/profile # 在文件末尾添加 PATH=$PATH:$HOME/bin:/opt/mongodb-4.2/bin # 使profile中的參數生效 [root@mongoserver ~]# source /etc/profile
(6)創建運行用戶mongod
[root@mongoserver ~]# groupadd mongod [root@mongoserver ~]# useradd -g mongod mongod 授權: [root@mongoserver ~]# chown -R mongod:mongod /mongo
(7)運行MongoDB
[root@mongoserver log]# mongod -config /mongo/mongodb.conf about to fork child process, waiting until server is ready for connections. forked process: 2137 child process started successfully, parent exiting # 或下面的方式 mongod -f /mongo/mongodb.conf
(8)查看運行狀態
[root@mongoserver log]# ps -ef|grep mongo root 2036 1 8 01:03 ? 00:00:00 mongod -config /mongo/mongodb.conf root 2072 1309 0 01:03 pts/0 00:00:00 grep --color=auto mongo
(9)關閉MongoDB
[root@mongoserver log]# mongod --shutdown --config /mongo/mongodb.conf killing process with pid: 2082
【完】