Linux 安装MongoDB并设为开机启动

环境

  1. centos 7
  2. mongo3.4.9

安装启动

1.解压

tar -zxvf mongodb-linux-x86_64-3.4.9.tgz -C /opt/mongodb

2. 创建配置文件

cd /opt/mongodb/mongodb-linux-x86_64-3.4.9

vi config/mongodb.conf

dbpath = /opt/mongodb/mongodb-linux-x86_64-3.4.9/data/db #数据文件存放目录  
logpath = /opt/mongodb/mongodb-linux-x86_64-3.4.9/logs/mongodb.log #日志文件存放目录  
bind_ip = 0.0.0.0  #远程访问
port = 27017  #端口  
fork = true  #以守护程序的方式启用,即在后台运行  
nohttpinterface = true

3. 启动

bin/mongod --config config/mongodb.conf

4. 创建数据库和用户

-- 进入命令行
mongo
-- 创建data数据库
use data
-- 创建用户
db.createUser(
    {
      user: "test",
      pwd: "123456",
      roles: ["readWrite"]
    }
 ) 

设为开机启动

1. 创建配置文件

vi /etc/init.d/mongod

#!/bin/bash

export MONGO_HOME=/opt/mongodb/mongodb-linux-x86_64-3.4.9
#chkconfig:2345 20 90
#description:mongod
#processname:mongod
case $1 in
          start) 
              $MONGO_HOME/bin/mongod --config $MONGO_HOME/config/mongodb.conf
              ;;
          stop)
              $MONGO_HOME/bin/mongod  --shutdown --config $MONGO_HOME/config/mongodb.conf
              ;;
          status)
              ps -ef | grep mongod
              ;;
          restart)
              $MONGO_HOME/bin/mongod  --shutdown --config $MONGO_HOME/config/mongodb.conf
              $MONGO_HOME/bin/mongod --config $MONGO_HOME/config/mongodb.conf
              ;;
          *)
              echo "require start|stop|status|restart"
              ;;
esac

添加服务并设置为开机启动

可执行权限

chmod 755 /etc/init.d/mongod

添加服务

chkconfig --add mongod

设置开机启动

chkconfig mongod on

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值