美文网首页
如何在单机部署MongoDB集群

如何在单机部署MongoDB集群

作者: 叶迎宪 | 来源:发表于2018-08-25 17:55 被阅读0次

    在单机部署集群主要是为了在开发的小环境里面也能够用上集群。操作系统为Ubuntu 16.04。

    一、下载MongoDB二进制执行文件,而不是使用apt-get方式安装。使用这种方式安装是为了更方便在单机启动多个MongoDB实例。

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

    在 ~/.bashrc 添加
    export PATH=<mongodb-install-directory>/bin:$PATH

    二、创建三个db实例的logpath、dbpath
    mkdir data1 data2 data3
    mkdir log1 log2 log3

    三、创建三个配置文件mongo1.conf、mongo2.conf、mongo3.conf
    内容

    systemLog:
      destination: file
      path: "/home/mongodb-linux-x86_64-3.6.4/log1/mongod.log"
      logAppend: true
    storage:
      dbPath: "/home/mongodb-linux-x86_64-3.6.4/data1"
      journal:
        enabled: true
    processManagement:
      fork: true
    net:
    #  bindIp: 192.168.11.52 
      port: 27017
    setParameter:
      enableLocalhostAuthBypass: true
    

    不同配置文件注意修改
    systemLog.path、storage.dbPath、net.port

    启动的方法
    mongod --config mongo1.conf

    先启动mongo1

    四、在mongo1的上创建集群的超级用户

    use admin
    db.createUser(
       {
         user: "admin",
         pwd: "admin",
         roles: [ "userAdminAnyDatabase","clusterAdmin" ]
       }
    )
    

    五、创建随机数key文件
    openssl rand -base64 126 -out key.txt

    将key.txt设置非所属用户组不能有读写的权限
    chmod 600 key.txt

    mongodb配置文件mongo1.conf增加集群的配置

    security:
      keyFile: "/home/mongodb-linux-x86_64-3.6.4/key.txt"
      clusterAuthMode: "keyFile"
      authorization: "disabled"
    replication:
      oplogSizeMB: 50
      replSetName: "repl_test"
      secondaryIndexPrefetch: "all"
    

    重启mongo1的数据库

    六、用超级用户登录admin库,初始化集群
    use admin
    db.auth("admin", "admin")
    rs.initiate()

    七、启动mongo2 mongo3,它们的配置文件中同样需要加入集群的配置
    mongod --config mongo2.conf
    mongod --config mongo3.conf

    登录mongo1,登录admin用户,在集群中添加服务器
    rs.add("localhost:27018")
    rs.addArb("localhost:27019")

    相关文章

      网友评论

          本文标题:如何在单机部署MongoDB集群

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