linux安装mongodb

目录

linux安装mongodb

启动mongodb服务

mongodb集成到asp .net Core Web Api项目(源码可下载)

 

1.安装依赖

yum install libcurl openssl

 

2.下载,解压

wget -c https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.5.tgz
tar -zxvf mongodb-linux-x86_64-4.0.5.tgz

3.移动mongodb

mkdir -p /usr/local/mongodb
mv mongodb-linux-x86_64-4.0.5/* /usr/local/mongodb

4.加入环境变量

vim /etc/profile
#下面代码加入文件末尾
PATH=$PATH:/usr/local/mongodb/bin
export PATH
#立刻生效
source /etc/profile
#查看版本
[root@jmsite ~]# mongod --version
db version v4.0.5
git version: 3739429dd92b92d1b0ab120911a23d50bf03c412
allocator: tcmalloc
modules: none
build environment:
    distarch: x86_64
    target_arch: x86_64

 

5.创建数据目录,日志目录,配置文件目录,进程文件目录

mkdir -p /usr/local/mongodb/data
mkdir -p /usr/local/mongodb/log
mkdir -p /usr/local/mongodb/conf
mkdir -p /var/run/mongodb

6.创建配置文件

vim /usr/local/mongodb/conf/mongodb.conf

写入以下配置

#监听端口
port = 27017
#数据库存文件存放目录
dbpath = /usr/local/mongodb/data
#日志文件存放路径
logpath = /usr/local/mongodb/log/mongodb.log
#进程文件存放路径
pidfilepath = /var/run/mongodb/mongodb.pid
#使用追加的方式写日志
logappend = true
#以守护进程的方式运行,创建服务器进程
fork = true
#最大同时连接数
maxConns = 100
#每次写入会记录一条操作日志
journal = true
#不绑定ip,这样就可外部访问了
bind_ip = 0.0.0.0
#校验权限
auth = true

7.创建一个不能登录的mongodb用户并设置权限

useradd mongodb -M -s /sbin/nologin
chown -R mongodb.mongodb /usr/local/mongodb
chown -R mongodb.mongodb /var/run/mongodb

8.创建启动脚本

vim /etc/init.d/mongod

写入以下配置

#!/bin/sh
#
# mongodb      init file for starting up the MongoDB server
#
# chkconfig:   - 90 10
# description: Starts and stops the MongDB daemon that handles all \
#              database requests.

# Source function library.
. /etc/rc.d/init.d/functions

exec="/usr/local/mongodb/bin/mongod"
prog="mongod"
logfile="/usr/local/mongodb/log/mongodb.log"

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
OPTIONS="--quiet -f /usr/local/mongodb/conf/mongodb.conf"
pidfile=${PIDFILE-/var/run/mongodb/mongodb.pid}
options="$MONGODB_OPTIONS $OPTIONS"
lockfile="/var/lock/subsys/mongod"

# Nicer version of killproc that does not kill mongodb when it takes
# a long time to shut down and does not hang for a long time when mongo
# shuts down quickly
killproc_nice() {
    local RC base pid pid_file= delay i

    RC=0; delay=3
    # Test syntax.
    if [ "$#" -eq 0 ]; then
        echo $"Usage: killproc [-p pidfile] [ -d delay] {program} [-signal]"
        return 1
    fi
    if [ "$1" = "-p" ]; then
        pid_file=$2
        shift 2
    fi
    if [ "$1" = "-d" ]; then
        delay=$2
        shift 2
    fi

    # Save basename.
    base=${1##*/}

    # Find pid.
    __pids_var_run "$1" "$pid_file"
    RC=$?
    if [ -z "$pid" ]; then
        if [ -z "$pid_file" ]; then
            pid="$(__pids_pidof "$1")"
        else
            [ "$RC" = "4" ] && { failure $"$base shutdown" ; return $RC ;}
        fi
    fi

    # Kill it.
    if [ -n "$pid" ] ; then
        [ "$BOOTUP" = "verbose" -a -z "${LSB:-}" ] && echo -n "$base "
        if checkpid $pid 2>&1; then
            # TERM first, then KILL if not dead
            kill -TERM $pid >/dev/null 2>&1
            usleep 100000

            # Check every one second if the program is stopped.
            # Do so for a maximum of $delay seconds
            for ((i = 0 ; i < $delay; i++))
            do
                if checkpid $pid; then
                    sleep 1
                else
                    break
                fi
            done

            # If the program is not stopped, kill it
            if checkpid $pid ; then
                kill -KILL $pid >/dev/null 2>&1
                usleep 100000
            fi
        fi
        checkpid $pid
        RC=$?
        [ "$RC" -eq 0 ] && failure $"$base shutdown" || success $"$base shutdown"
        RC=$((! $RC))
    else
        failure $"$base shutdown"
        RC=0
    fi

    # Remove pid file if any.
    rm -f "${pid_file:-/var/run/$base.pid}"
    return $RC
}

start() {
    [ -x $exec ] || exit 5
    echo -n $"Starting $prog: "
    daemon --pidfile=${pidfile} --user mongodb "$exec $options run >> $logfile 2>&1"
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc_nice -p ${pidfile} -d 300 $prog
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    stop
    start
}

reload() {
    restart
}

force_reload() {
    restart
}

rh_status() {
    # run checks to determine if the service is running or use generic status
    status -p ${pidfile} $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}


case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
exit $?

 

设置权限

chmod 755 /etc/init.d/mongod

加入服务

chkconfig --add mongod

开机启动

chkconfig mongod on

posted on 2021-07-27 17:28  辛勤的代码搬运工  阅读(763)  评论(0编辑  收藏  举报

导航