一、rpm安装
1.1、配置MongoDB Enterprise的yum 源文件

bash
[mongodb-enterprise]
name=MongoDB Enterprise Repository
baseurl=https://repo.mongodb.com/yum/redhat/$releasever/mongodb-enterprise/3.4/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
1.2、安装
bash
yum install -y mongodb-enterprise

1.3 启动
bash
systemctl start mongod

1.4 检查

=========================================================================
二、源码安装
2.1 下载软件包
bash
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.4.7.tgz

2.2 解压
bash
tar xf mongodb-linux-x86_64-rhel70-3.4.7.tgz -C /usr/local

2.3 创建数据目录
bash
mkdir -p /data/db

2.4 启动
bash
ln -sv mongodb-linux-x86_64-rhel70-3.4.7/ mongodb
echo "export PATH=$PATH:/usr/local/mongodb/bin" > /etc/profile.d/mongo.sh
source /etc/profile.d/mongo.sh
mongod --dbpath /data/db/ &

2.5 检查
bash
ps -ef | grep mongod
netstat -lnupt | grep 27017
lsof -i tcp:27017

2.6 以系统服务方式启动

在以上图片可以看到此时是不可以用systemctl 命令去开启我们的mongodb服务
如果需要用systemctl 命令去控制mongodb服务的开启、停止等操作,就需要进行下面的操作。
bash
vim /usr/local/mongodb/bin/mongod.conf
#按配置文件设置创建日志和数据文件存放目录
mkdir -p /usr/local/mongodb/{data,log}
#配置mongodb.service文件
vim /usr/lib/systemd/system/mongodb.service
#保存mongodb.service文件后,需要输入命令进行重新加载.
systemctl daemon-reload


/usr/local/mongodb/bin/mongod.conf 文件内容:
bash
systemLog:
destination: file
path: /usr/local/mongodb/log/mongodb.log
logAppend: true
storage:
dbPath: /usr/local/mongodb/data
processManagement:
fork: true
注意:文件内容缩进问题。
/usr/lib/systemd/system/mongodb.service 文件内容:
bash
[Unit]
Description=mongodb service daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/mongod.conf
ExecStop=/usr/local/mongodb/bin/mongod --shutdown -f /usr/local/mongodb/bin/mongod.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target