美文网首页
配置MongoDB复制集

配置MongoDB复制集

作者: 乔治大叔 | 来源:发表于2019-09-26 10:37 被阅读0次

    环境说明

    [root@MongoDB ~]# cat /etc/redhat-release 
    CentOS release 6.9 (Final)
    [root@MongoDB ~]# uname -r
    2.6.32-696.el6.x86_64
    [root@MongoDB ~]# /etc/init.d/iptables status
    iptables: Firewall is not running.
    [root@MongoDB ~]# getenforce 
    Disabled
    [root@MongoDB ~]# hostname -I
    10.0.0.152 172.16.1.152
    

    本次使用的mongodb版本为:mongodb-linux-x86_64-3.2.8.tgz

    前期准备,在root用户下操作

    #创建mongod用户
        useradd -u800 mongod
        echo 123456|passwd --stdin mongod 
    # 安装mongodb
        mkdir -p /mongodb/bin
       cd  /mongodb
       wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.8.tgz
        tar xf  mongodb-linux-x86_64-3.2.8.tgz
        cd mongodb-linux-x86_64-3.2.8/bin/ &&\
        cp * /mongodb/bin
        chown -R mongod.mongod /mongodb
    # 切换到mongod用户进行后续操作
        su - mongod
    

    创建所需目录

    for  i in 28017 28018 28019 28020
        do 
          mkdir -p /mongodb/$i/conf  
          mkdir -p /mongodb/$i/data  
          mkdir -p /mongodb/$i/log
    done 
    

    配置多实例环境

    cat >>/mongodb/28017/conf/mongod.conf<<'EOF'
    systemLog:
      destination: file
      path: /mongodb/28017/log/mongodb.log
      logAppend: true
    storage:
      journal:
        enabled: true
      dbPath: /mongodb/28017/data
      directoryPerDB: true
      #engine: wiredTiger
      wiredTiger:
        engineConfig:
          # cacheSizeGB: 1
          directoryForIndexes: true
        collectionConfig:
          blockCompressor: zlib
        indexConfig:
          prefixCompression: true
    processManagement:
      fork: true
    net:
      port: 28017
    replication:
      oplogSizeMB: 2048
      replSetName: my_repl
    EOF
    

    复制配置文件

    for i in 28018 28019 28020
      do  
       \cp  /mongodb/28017/conf/mongod.conf  /mongodb/$i/conf/
    done
    

    修改配置文件

    for i in 28018 28019 28020
      do 
        sed  -i  "s#28017#$i#g" /mongodb/$i/conf/mongod.conf
    done
    

    启动服务

    for i in 28017 28018 28019 28020
      do  
        mongod -f /mongodb/$i/conf/mongod.conf 
    done
    

    关闭服务的方法

    for i in 28017 28018 28019 28020
       do  
         mongod --shutdown  -f /mongodb/$i/conf/mongod.conf 
    done
    

    配置复制集

    登陆数据库,配置mongodb复制

    shell> mongo --port 28017
    
    config = {_id: 'my_repl', members: [
                              {_id: 0, host: '10.0.0.152:28017'},
                              {_id: 1, host: '10.0.0.152:28018'},
                              {_id: 2, host: '10.0.0.152:28019'}]
              }
    

    初始化这个配置

    rs.initiate(config)

    相关文章

      网友评论

          本文标题:配置MongoDB复制集

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