首页 > 技术文章 > linux centos 安装mongoDB

zincredible 2018-10-08 16:30 原文

1.下载mongoDB 例如下载3.2.19 64位版本 https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.2.19.tgz

2.上传到linux临时目录 /home/soft/mongodb-linux-x86_64-rhel70-3.2.19.tgz

3.解压后移动到指定目录

cd /home/soft

tar -zxvf mongodb-linux-x86_64-rhel70-3.2.19.tgz

cd /usr/local

mkdir mongodb

mv /home/soft/mongodb-linux-x86_64-rhel70-3.2.19 /usr/local/mongodb

 

3.配置环境变量

   把环境变量添加到/etc/profile文件后面

   export MONGODB_HOME=/usr/local/mongodb/mongodb-linux-x86_64-rhel70-3.2.19
   export PATH=$PATH:$MONGODB_HOME/bin

   保存后 执行 source /etc/profile 命令使之立即生效(或退出终端再次登录户生效)

   测试:

   

4.配置mongodb

 

 创建 /usr/local/mongodb/mongo.conf 配置文件

 在mongo.conf写入以下类容

 port=27017
 dbpath=/usr/local/mongodb/data/db
 logpath=/usr/local/mongodb/data/log/mongod.log
 logappend=true
 fork=true
 # auth=true

 创建所需文件夹和文件

 mkdir -p /usr/local/mongodb/data/db

 mkdir -p /usr/local/mongodb/data/log

 touch /usr/local/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=/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

chmod 754 mongodb.service

 

5.停止启动mongoDB服务

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

 

 

 

推荐阅读