一、安装
下载地址:https://www.mongodb.com/try/download/community
下载的包名为:mongodb-linux-x86_64-4.0.21.tgz
解压:
[root@master ~]# tar -zxf mongodb-linux-x86_64-4.0.21.tgz
创建运行MongoDB的普通用户:
[root@master ~]# useradd mongod
[root@master ~]# passwd mongod
创建需要的目录:
[root@master ~]# mkdir -p /usr/local/mongodb/{conf,data,log}
将解压出来的bin目录,移动到/usr/local/mongodb目录下:
[root@master ~]# mv mongodb-linux-x86_64-4.0.21/bin/ /usr/local/mongodb/
root@master ~]# ls /usr/local/mongodb/
bin conf data log
修改目录权限:
[root@master ~]# chown -R mongod:mongod /usr/local/mongodb/
设置环境变量:
[root@master ~]# vim /etc/profile.d/mongod.sh
export PATH=/usr/local/mongodb/bin:$PATH
[root@master ~]# source /etc/profile
二、配置文件
MongoDB的配置文件,遵循YAML格式:
[root@master ~]# vim /usr/local/mongodb/conf/mongodb.conf
# 日志相关
systemLog:
destination: file
logAppend: true
path: /usr/local/mongodb/log/mongodb.log
# 数据存储相关
storage:
dbPath: /usr/local/mongodb/data
journal:
enabled: true
# 进程控制
processManagement:
fork: true
pidFilePath: /usr/local/mongodb/data/mongodb.pid
timeZoneInfo: /usr/share/zoneinfo
# 网络配置相关
net:
port: 27017
bindIp: 0.0.0.0
官方推荐使用普通用户运行MongoDB,启动MongoDB:
[root@master ~]# su - mongod
[mongod@master ~]$ mongod -f /usr/local/mongodb/conf/mongodb.conf
停止MongoDB:
[mongod@master ~]$ mongod -f /usr/local/mongodb/conf/mongodb.conf --shutdown
三、使用systemd管理MongoDB
[root@master ~]# vim /usr/lib/systemd/system/mongod.service
[Unit]
Description=MongoDB 4.0.21
[Service]
User=mongod
Restart=on-failure
Type=forking
ExecStart=/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb.conf
ExecStop=/usr/local/mongodb