安装
下载安装包
依次选择On-Premises——>MongoDB Community Server
在右侧可以选择下载版本,选择好之后点击download
安装步骤
tgz源码安装
1.解压缩
cd /usr/local/mongodb
tar -zxvf mongodb-linux-x86_64-rhel70-4.0.20.tgz
mv /usr/local/mongodb/mongodb-linux-x86_64-rhel70-4.0.20 /usr/local/mongodb/mongdb-4.0.20
2.配置环境变量
vim /etc/profile
添加
export MONGODB_HOME=/usr/local/mongodb/mongdb-4.0.20
export PATH=$PATH:$MONGODB_HOME/bin
source /etc/profile 使配置生效
3.配置mongodb
创建mongo.conf文件
touch /usr/local/mongodb/mongdb-4.0.20/mongo.conf
在mongo.conf文件中写入如下内容
port=27017
dbpath=/usr/local/mongodb/mongdb-4.0.20/data/db
logpath=/usr/local/mongodb/mongdb-4.0.20/data/log/mongod.log
logappend=true
fork=true
# auth=true
# 白名单ip
bind_ip=0.0.0.0
创建如下文件和文件夹
mkdir -p /usr/local/mongodb/mongdb-4.0.20/data/db
mkdir -p /usr/local/mongodb/mongdb-4.0.20/data/log
touch /usr/local/mongodb/mongdb-4.0.20/data/log/mongod.log
4.将mongodb包装成Linux服务
cd /lib/systemd/system
touch mongodb.service
在mongodb.service文件写入
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/mongodb/mongodb-linux-x86_64-rhel70-3.2.19/bin/mongod --config /usr/local/mongodb/mongo.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/mongodb-linux-x86_64-rhel70-3.2.19/bin/mongod --shutdown --config /usr/local/mongodb/mongo.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
mongodb.service权限
chmod 754 mongodb.service
5.停止启动mongoDB服务
#查看状态
service mongodb.service status
#启动服务
systemctl start mongodb.service
#关闭服务
systemctl stop mongodb.service
#开机启动
systemctl enable mongodb.service