美文网首页
mongodb安装

mongodb安装

作者: 山口肅彧 | 来源:发表于2018-08-22 18:13 被阅读0次

    mongodb3.2.8安装步骤:

    1、系统准备

    (1)redhat或cnetos6.2以上系统
    (2)系统开发包完整
    (3)ip地址和hosts文件解析正常
    (4)iptables防火墙&SElinux关闭
    (5)关闭大页内存机制

    root用户下,vim /etc/rc.local最后添加如下代码
        if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
          echo never > /sys/kernel/mm/transparent_hugepage/enabled
        fi
        if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
           echo never > /sys/kernel/mm/transparent_hugepage/defrag
        fi
    

    其他系统关参照官方文档:

    为什么要关闭?
    Transparent Huge Pages (THP) is a Linux memory management system
    that reduces the overhead of Translation Lookaside Buffer (TLB)
    lookups on machines with large amounts of memory by using larger memory pages.
    However, database workloads often perform poorly with THP,
    because they tend to have sparse rather than contiguous memory access patterns.
    You should disable THP on Linux machines to ensure best performance with MongoDB.

    2、mongodb安装

    2.1官方tarball安装

    (1)创建所需用户和组

        groupadd -g 800  mongod&&useradd -u 801 -g mongod  
        echo "123456"|passwd --stdin  mongod
    

    (2)创建mongodb所需目录结构

        mkdir -p /mongodb/{bin,conf,log,data}
    

    (3)上传并解压软件到指定位置

    mongodb-linux-x86_64-3.2.8.tgz 
    解压源码包
    cd mongodb-linux-x86_64-3.2.8/bin/  
    cp * /mongodb/bin
    

    (4)设置目录结构权限

        chown -R mongod:mongod /mongodb
    

    (5)设置用户环境变量

    su - mongod 
    cat >> /home/.bash_profile <<EOF
    export PATH=/mongodb/bin:$PATH
    EOF
    source /home/.bash_profile
    

    (6)启动mongodb

    mongod --dbpath=/mongodb/data --logpath=/mongodb/log/mongodb.log --port=27017 --logappend --fork
    

    (7)登录mongodb

    mongo
    MongoDB shell version: 3.2.8
    connecting to: test
    

    (8)使用配置文件启动实例

    mongod -f /mongodb/config/mongodb.conf
    

    配置文件支持普通格式和yaml格式

    普通格式如下:
    cat >> /mongodb/config/mongodb.conf <<EOF
    logptah=
    dbpath=
    port=
    logappend=
    fork=
    auth=
    EOF


    YAML模式如下:
    NOTE:
    YAML does not support tab characters for indentation: use spaces instead.

    systemLog:  
    destination: file  
      path: "/mongodb/log/mongod.log"  
      logAppend: true  
    storage:  
      journal:  
        enabled: true  
      dbPath: "<PATH>"
    processManagement:
      fork: true
      pidFilePath: <string>
    net:
      bindIp: <ip>
      port: <port>
    setParameter:
      enableLocalhostAuthBypass: false
    security:
      authorization: enabled
      
    replication:
      oplogSizeMB: <NUM>
      replSetName: "<REPSETNAME>"
      secondaryIndexPrefetch: "all"
    sharding:
      clusterRole: <string>
      archiveMovedChunks: <boolean>
    ---for mongos only
    replication:
      localPingThresholdMs: <int>
    sharding:
      configDB: <string>
    

    (9)mongodb的关闭方式

    kill进程形式 kill -2 PID
    原理:-2表示向mongod进程发送SIGINT信号。
    或 kill -4 PID
    原理:-4表示向mognod进程发送SIGTERM信号。


    自带模式
    admin> db.shutdownServer()
    或admin>db.adminCommand({shutdown:1}) 或$ mongod -f mongodb.conf --shutdown
    注:mongod进程收到SIGINT信号或者SIGTERM信号,会做一些处理
    关闭所有打开的连接
    将内存数据强制刷新到磁盘
    当前的操作执行完毕
    安全停止

    切忌kill -9

    数据库直接关闭
    数据丢失
    数据文件损失
    修复数据库(成本高,有风险)

    2.2 使用官方yum源安装

    参考官方文档
    (1)加载官方yum源

    cat > /etc/yum.repos.d/mongodb-org-4.0.repo <<EOF
    [mongodb-org-4.0]
    name=MongoDB Repository
    baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
    gpgcheck=1
    enabled=1
    gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
    EOF
    

    (2)使用yum安装即可

    yum install -y mongodb-org-4.0.1 mongodb-org-server-4.0.1 mongodb-org-shell-4.0.1 mongodb-org-mongos-4.0.1 mongodb-org-tools-4.0.1
    

    3、mongodb控制脚本

        #!/bin/bash
        . /etc/init.d/functions
        Bin=/mongodb/bin
        port=28017
        start () {
          a=`whoami`
          if [ $a == root ];then
            for i in $port;do 
                su - mongod -c  "$Bin/mongod -f /mongodb/$i/conf/mongodb.conf"  &>/dev/null
                [ $? == 0 ] && action "${1} ${i}" /bin/true || action  "${1} ${i}" /bin/false
            done
          elif [ $a == mongod ];then
            for i in $port;do
                $Bin/mongod -f /mongodb/$i/conf/mongodb.conf  &>/dev/null
                [ $? == 0 ] && action "${1} ${i}" /bin/true || action  "${1} ${i}" /bin/false
            done
          else
            echo "permission denied!Only root & mongod can run this command!"
          fi
        }
        stop () {
          a=`whoami`
          if [ $a == root ];then
            for i in $port;do 
                su - mongod -c "$Bin/mongos -f /mongodb/$i/conf/mongodb.conf --shutdown" &>/dev/null
                [ $? == 0 ] && action "${1} ${i}" /bin/true || action "${1} ${i}" /bin/false
            done
          elif [ $a == mongod ];then
            for i in $port;do
                $Bin/mongos -f /mongodb/$i/conf/mongodb.conf --shutdown &>/dev/null
                [ $? == 0 ] && action "${1} ${i}" /bin/true || action "${1} ${i}" /bin/false
            done
          else
            echo "permission denied!Only root & mongod can run this command!"
          fi  
        }
        case $1 in 
          start)
          start
          ;;
          stop)
          stop
          ;;
          restart)
          stop
          sleep 2
          start
          ;;
          *)
          echo "Usage: $0 {start|stop|restart}"
          exit 1
        esac
    

    相关文章

      网友评论

          本文标题:mongodb安装

          本文链接:https://www.haomeiwen.com/subject/vewyiftx.html