• docker——mongodb副本集


    docker mongodb 副本集

     

     

    docker network create net-mongoset

    docker network ls

     

    docker run -d -p27001:27017 --name docker_mongo1 --net net-mongoset  mongo:3.4.24 --replSet yinSet

    docker run -d -p27002:27017 --name docker_mongo2 --net net-mongoset  mongo:3.4.24 --replSet yinSet

    docker run -d -p27003:27017 --name docker_mongo3 --net net-mongoset  mongo:3.4.24 --replSet yinSet

     

    mongo --port 27001

     

    >rs.status()

     

    > rs.initiate()

    {

    "info2" : "no configuration specified. Using a default configuration for the set",

    "me" : "218f50912c40:27017",

    "ok" : 1

    }

     

    >rs.add("docker_mongo2:27017")

    >rs.add("docker_mongo3:27017")

     

    >rs.status()

     

    主节点插入数据,查看副节点同步

    mongo --port 27001

    yinSet:PRIMARY> use mytest

    switched to db mytest

    yinSet:PRIMARY> 

    yinSet:PRIMARY> db.mytest.insert({"name":"axboy"})

    WriteResult({ "nInserted" : 1 })

    yinSet:PRIMARY> show dbs;

    yinSet:PRIMARY> use mytest

    switched to db mytest

     

    yinSet:PRIMARY> db2 = (new Mongo('127.0.0.1:27002')).getDB('mytest')

    mytest

    yinSet:PRIMARY> db2.mytest.find()

    Error: error: {

    "ok" : 0,

    "errmsg" : "not master and slaveOk=false",

    "code" : 13435,

    "codeName" : "NotMasterNoSlaveOk"

    }

    yinSet:PRIMARY> db2.setSlaveOk()

    yinSet:PRIMARY> 

    yinSet:PRIMARY> db2.mytest.find()

    { "_id" : ObjectId("5ee5aff0d779793f66bc376f"), "name" : "axboy" }

    yinSet:PRIMARY> 

    yinSet:PRIMARY> 

     

     

     

     

    原因: mongodb默认是从主节点读写数据,副本节点上不允许读,设置副本节点可读。

    db.getMongo().setSlaveOk();

    注意:这条命令要在副节点上运行

    然后就可以查询复制过来的数据了

     

     

     初始化副本集

     

    config = {
      _id : "config_repl",
      members : [
        {_id : 0, host : "172.31.140.157:27101" },
        {_id : 1, host : "172.31.140.158:27101" },
        {_id : 2, host : "172.31.140.159:27101" }
      ]
    }

     配置3节点副本集


    config = {
    _id : "shard1_repl",
    members : [
    {_id : 0, host : "172.31.140.157:27201" },
    {_id : 1, host : "172.31.140.158:27201" },
    {_id : 2, host : "172.31.140.159:27201" }
    ]
    }

    为mongos添加副本集类型的分片

    sh.addShard("shard1_repl/172.31.140.157:27201,172.31.140.158:27201,172.31.140.159:27201")

     

     

     

     

     

     

  • 相关阅读:
    HTTP 03 HTTP 报文头
    HTTP 02 HTTP1.1 协议
    HTML 01 请求过程
    Java_异常_04_ OutOfMemoryError系列
    mq_学习_01_消息中间件的概述
    mq_学习_00_资源帖
    Java钉钉开发_异常_01_error code:50002, error message:请求的员工userid不在授权范围内
    Java_JS_01_java调用js
    JavaUtil_09_email_使用 commons-email 发送邮件
    JavaUtil_08_StringUtil_commons-lang3 之 StringUtils
  • 原文地址:https://www.cnblogs.com/xingchong/p/13124426.html
Copyright © 2020-2023  润新知