Centos7上搭建MongoDB

1. 下载Mongodb

https://www.mongodb.com/try/download/community

2.解压并移动到指定目录

tar -zxf mongodb-linux-x86_64-rhel70-3.6.23.tgz
mv mongodb-linux-x86_64-rhel70-3.6.23 soft/mongodb

3.配置环境变量并测试

#mongodb 修改/etc/profile
export MONGODB_HOME=/opt/soft/mongodb
export PATH=$PATH:$MONGODB_HOME/bin #激活配置文件后测试 mongod -version

4.配置mongodb

#在/opt/soft/mongodb下创建mongo.conf
port=27017
dbpath=/opt/soft/mongodb/data/db
logpath=/opt/soft/mongodb/data/log/mongod.log
logappend=true
fork=true
#创建文件夹和日志文件
mkdir -p /opt/soft/mongodb/data/db
mkdir -p /opt/soft/mongodb/data/log
touch /opt/soft/mongodb/data/log/mongod.log

5.将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=/opt/soft/mongodb/bin/mongod --config /opt/soft/mongodb/mongo.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/opt/soft/mongodb/bin/mongod --shutdown --config /opt/soft/mongodb/mongo.conf
PrivateTmp=true

[Install]
WantedBy=multi-user.target

文件授权:

chmod 754 mongodb.service

6.启动停止开机启动命令

#查看状态
service mongodb.service status
#启动服务
systemctl start mongodb.service
#关闭服务
systemctl stop mongodb.service
#开机启动
systemctl enable mongodb.service

 

 

 

posted on 2021-12-29 13:39  理想三旬_z  阅读(129)  评论(0编辑  收藏  举报

导航