美文网首页
同一主机创建Mongodb4.2副本集

同一主机创建Mongodb4.2副本集

作者: e652d1fb12eb | 来源:发表于2020-05-17 00:05 被阅读0次

    本文主要记录在同一台服务器上安装3个节点的过程,在同时添加三个节点(同一主机不同端口)时报如下错误:

        "errmsg" : "Found two member configurations with same _id field, members.1._id == members.2._id == MemberId(1)",
    

    环境为CentOS8, Mongodb4.2.1

    1.安装Mongodb4.2.1 参考官网

    Configure the package management system (yum).
    Create a /etc/yum.repos.d/mongodb-org-4.2.repo file so that you can install MongoDB directly using yum:

    [mongodb-org-4.2]
    name=MongoDB Repository
    baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/
    gpgcheck=1
    enabled=1
    gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
    

    To install the latest stable version of MongoDB, issue the following command:

    sudo yum install -y mongodb-org
    

    Alternatively, to install a specific release of MongoDB, specify each component package individually and append the version number to the package name, as in the following example:

    sudo yum install -y mongodb-org-4.2.6 mongodb-org-server-4.2.6 mongodb-org-shell-4.2.6 mongodb-org-mongos-4.2.6 mongodb-org-tools-4.2.6
    

    2. 创建不同实例的目录和文件

    [root@centos8]#cd /var/lib/mongo
    [root@centos8 mongo]# mkdir data1
    [root@centos8 mongo]# mkdir data2
    [root@centos8 mongo]# mkdir data3
    [root@centos8 mongo]# vi /etc/mongod.conf
    

    内容如下:

    [root@centos8 ~]# more /etc/mongod.conf 
    # mongod.conf
    
    # for documentation of all options, see:
    #   http://docs.mongodb.org/manual/reference/configuration-options/
    
    # where to write logging data.
    systemLog:
      destination: file
      logAppend: true
      path: /var/log/mongodb/mongod1.log
    
    # Where and how to store data.
    storage:
      dbPath: /var/lib/mongo/data1
      journal:
        enabled: true
    #  engine:
    #  wiredTiger:
    
    # how the process runs
    processManagement:
      fork: true  # fork and run in background
      pidFilePath: /var/run/mongodb/mongod1.pid  # location of pidfile
      timeZoneInfo: /usr/share/zoneinfo
    
    # network interfaces
    net:
      port: 27017
      bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
    
    
    #security:
    
    #operationProfiling:
    
    replication:
      replSetName: replset
      oplogSizeMB: 10
    #sharding:
    
    ## Enterprise-Only Options
    
    #auditLog:
    
    #snmp:
    

    分别复制为mongdb2.conf,和mongodb3.conf
    分别修改其中的内容,举例如下:

      path: /var/log/mongodb/mongod2.log
      dbPath: /var/lib/mongo/data2
      pidFilePath: /var/run/mongodb/mongod2.pid
    

    3.启动服务

    [root@centos8 mongo]# mongod -f /etc/mongod.conf
    [root@centos8 mongo]# mongod -f /etc/mongod2.conf 
    [root@centos8 mongo]# mongod -f /etc/mongod3.conf
    

    4.登录一台、并配置复本集

    [root@centos8 etc]# mongo
    MongoDB shell version v4.2.1
    rs.initiate()
    replset:PRIMARY> rs.status()
    replset:PRIMARY> rs.add('192.168.56.112:27018')
    replset:PRIMARY> rs.add('192.168.56.112:27019')
    从库执行:
    命令:rs.slaveOk() ----在从节点上执行
    
    

    5.验证:

    主上建一个新库,并插入记录,在从库上查看

    replset:PRIMARY> use test;
    switched to db test
    replset:PRIMARY> db.master.insert({"Ip":"192.168.0.254","node":"master"});
    WriteResult({ "nInserted" : 1 })
    replset:PRIMARY> show dbs;
    admin   0.000GB
    config  0.000GB
    local   0.000GB
    test    0.000GB
    ![image.png](https://img.haomeiwen.com/i9482651/c337062958a27aae.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    

    在从节点执行以下操作进行数据查询:
    命令:

    show dbs;
    use test;
    db.master.find();
    
    image.png

    相关文章

      网友评论

          本文标题:同一主机创建Mongodb4.2副本集

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