文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

MongoDB中怎么实现副本集

2024-04-02 19:55

关注

MongoDB中怎么实现副本集,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

1、环境

节点IP操作系统MongoDB
数据节点110.163.91.15RHLE6.5_x64mongodb-linux-x86_64-rhel62-3.4.7.tgz
数据节点210.163.91.16RHLE6.5_x64mongodb-linux-x86_64-rhel62-3.4.7.tgz
数据节点310.163.91.17RHLE6.5_x64mongodb-linux-x86_64-rhel62-3.4.7.tgz



2、分别在3个节点安装配置和启动MongoDB
[root@D2-POMS15 ~]# rpm -qa | grep openssl
openssl098e-0.9.8e-17.el6_2.2.x86_64
openssl-1.0.1e-15.el6.x86_64
[root@D2-POMS15 ~]# mkdir /usr/local/mongodb
[root@D2-POMS15 ~]# tar -xvzf mongodb-linux-x86_64-rhel62-3.4.7.tgz
[root@D2-POMS15 ~]# mv mongodb-linux-x86_64-rhel62-3.4.7/* /usr/local/mongodb/
[root@D2-POMS15 ~]# vim .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH=$PATH:/usr/local/mongodb/bin/
export PATH
[root@D2-POMS15 ~]# . .bash_profile
[root@D2-POMS15 ~]# mkdir /usr/local/mongodb/db
[root@D2-POMS15 ~]# mkdir /usr/local/mongodb/log
[root@D2-POMS15 ~]# touch /usr/local/mongodb/log/mongodb.log
[root@D2-POMS15 ~]# vim /usr/local/mongodb/mongodb.conf
dbpath=/usr/local/mongodb/db
logpath=/usr/local/mongodb/log/mongodb.log
logappend=true
noauth=true
port=27017
fork=true
replSet=stoners
[root@D2-POMS15 ~]# mongod -f /usr/local/mongodb/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 11580
child process started successfully, parent exiting
[root@D2-POMS15 ~]# mongo
MongoDB shell version v3.4.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.7
Welcome to the MongoDB shell.

[root@D2-POMS15 ~]# mongod -f /usr/local/mongodb/mongodb.conf
[root@D2-POMS16 ~]# mongod -f /usr/local/mongodb/mongodb.conf
[root@D2-POMS17 ~]# mongod -f /usr/local/mongodb/mongodb.conf


3、在任一节点进行副本集配置
初始化副本集配置。
语法:
{
    _id : <setname>,
     members : [
         {_id : 0, host : <host0>},
         {_id : 1, host : <host1>},
         {_id : 2, host : <host2>},
     ]
}
配置其中一个节点为仲裁的语法:
{
    _id : "my_replica_set",
     members : [
         {_id : 0, host : "rs1.example.net:27017"},
         {_id : 1, host : "rs2.example.net:27017"},
         {_id : 2, host : "rs3.example.net", arbiterOnly: true},
     ]
}

> rs.initiate({_id:"stoners",members:[{_id:0,host:"10.163.97.15:27017"}]})
{ "ok" : 1 }
stoners:SECONDARY> rs.conf()
{
        "_id" : "stoners",
        "version" : 1,
        "protocolVersion" : NumberLong(1),
        "members" : [
                {
                        "_id" : 0,
                        "host" : "10.163.97.15:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : 60000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("5996b49a33625594149428f2")
        }
}

添加节点:
stoners:PRIMARY> rs.add("10.163.97.16")
{ "ok" : 1 }
stoners:PRIMARY> rs.add("10.163.97.17")
{ "ok" : 1 }

查看状态:
stoners:PRIMARY> rs.status()
{
        "set" : "stoners",
        "date" : ISODate("2017-08-18T09:49:18.307Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1503049755, 1),
                        "t" : NumberLong(1)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1503049755, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1503049755, 1),
                        "t" : NumberLong(1)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "10.163.97.15:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 144,
                        "optime" : {
                                "ts" : Timestamp(1503049755, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2017-08-18T09:49:15Z"),
                        "infoMessage" : "could not find member to sync from",
                        "electionTime" : Timestamp(1503049643, 2),
                        "electionDate" : ISODate("2017-08-18T09:47:23Z"),
                        "configVersion" : 3,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "10.163.97.16:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 86,
                        "optime" : {
                                "ts" : Timestamp(1503049755, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1503049755, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2017-08-18T09:49:15Z"),
                        "optimeDurableDate" : ISODate("2017-08-18T09:49:15Z"),
                        "lastHeartbeat" : ISODate("2017-08-18T09:49:17.787Z"),
                        "lastHeartbeatRecv" : ISODate("2017-08-18T09:49:16.774Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "10.163.97.15:27017",
                        "configVersion" : 3
                },
                {
                        "_id" : 2,
                        "name" : "10.163.97.17:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 52,
                        "optime" : {
                                "ts" : Timestamp(1503049755, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1503049755, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2017-08-18T09:49:15Z"),
                        "optimeDurableDate" : ISODate("2017-08-18T09:49:15Z"),
                        "lastHeartbeat" : ISODate("2017-08-18T09:49:17.787Z"),
                        "lastHeartbeatRecv" : ISODate("2017-08-18T09:49:16.883Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "10.163.97.15:27017",
                        "configVersion" : 3
                }
        ],
        "ok" : 1
}


4、测试数据写入
主节点写入数据:
stoners:PRIMARY> use hr
switched to db hr
stoners:PRIMARY> db.emp.insert({"num":1})
WriteResult({ "nInserted" : 1 })
stoners:PRIMARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }

直接查询副本节点报错:
stoners:SECONDARY> use hr
switched to db hr
stoners:SECONDARY> db.emp.find();
Error: error: {
        "ok" : 0,
        "errmsg" : "not master and slaveOk=false",
        "code" : 13435,
        "codeName" : "NotMasterNoSlaveOk"
}

设置副本节点可读:
stoners:SECONDARY> db.getMongo().setSlaveOk();
stoners:SECONDARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }

另一个副本节点:
stoners:SECONDARY> db.getMongo().setSlaveOk();
stoners:SECONDARY> use hr
switched to db hr
stoners:SECONDARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }


5、测试故障切换
先关闭主节点:
stoners:PRIMARY> use admin
switched to db admin
stoners:PRIMARY> db.shutdownServer();
server should be down...

其中一个副本节点变为了主节点:
stoners:SECONDARY>
stoners:PRIMARY>
stoners:PRIMARY> rs.status()
{
        "set" : "stoners",
        "date" : ISODate("2017-08-18T10:03:49.050Z"),
        "myState" : 1,
        "term" : NumberLong(2),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1503050624, 1),
                        "t" : NumberLong(2)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1503050624, 1),
                        "t" : NumberLong(2)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1503050624, 1),
                        "t" : NumberLong(2)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "10.163.97.15:27017",
                        "health" : 0,
                        "state" : 8,
                        "stateStr" : "(not reachable/healthy)",
                        "uptime" : 0,
                        "optime" : {
                                "ts" : Timestamp(0, 0),
                                "t" : NumberLong(-1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(0, 0),
                                "t" : NumberLong(-1)
                        },
                        "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
                        "optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
                        "lastHeartbeat" : ISODate("2017-08-18T10:03:47.314Z"),
                        "lastHeartbeatRecv" : ISODate("2017-08-18T10:01:03.792Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "Connection refused",
                        "configVersion" : -1
                },
                {
                        "_id" : 1,
                        "name" : "10.163.97.16:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 929,
                        "optime" : {
                                "ts" : Timestamp(1503050624, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2017-08-18T10:03:44Z"),
                        "electionTime" : Timestamp(1503050473, 1),
                        "electionDate" : ISODate("2017-08-18T10:01:13Z"),
                        "configVersion" : 3,
                        "self" : true
                },
                {
                        "_id" : 2,
                        "name" : "10.163.97.17:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 845,
                        "optime" : {
                                "ts" : Timestamp(1503050624, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1503050624, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2017-08-18T10:03:44Z"),
                        "optimeDurableDate" : ISODate("2017-08-18T10:03:44Z"),
                        "lastHeartbeat" : ISODate("2017-08-18T10:03:47.252Z"),
                        "lastHeartbeatRecv" : ISODate("2017-08-18T10:03:47.998Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "10.163.97.16:27017",
                        "configVersion" : 3
                }
        ],
        "ok" : 1
}

在主节点插入数据:
stoners:PRIMARY> use hr
switched to db hr
stoners:PRIMARY> db.emp.insert({"num":2})
WriteResult({ "nInserted" : 1 })
stoners:PRIMARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
{ "_id" : ObjectId("5996bbcb5945237d11f3052d"), "num" : 2 }

在另一个副本节点查看:
stoners:SECONDARY> use hr
switched to db hr
stoners:SECONDARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
{ "_id" : ObjectId("5996bbcb5945237d11f3052d"), "num" : 2 }

现在启动关闭的节点:
[root@D2-POMS15 ~]# mongod -f /usr/local/mongodb/mongodb.conf
启动后自动成为副本节点:
stoners:SECONDARY> rs.status()
{
        "set" : "stoners",
        "date" : ISODate("2017-08-18T10:08:25.401Z"),
        "myState" : 2,
        "term" : NumberLong(2),
        "syncingTo" : "10.163.97.17:27017",
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1503050974, 1),
                        "t" : NumberLong(2)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1503050974, 1),
                        "t" : NumberLong(2)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1503050974, 1),
                        "t" : NumberLong(2)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "10.163.97.15:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 49,
                        "optime" : {
                                "ts" : Timestamp(1503050974, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2017-08-18T10:09:34Z"),
                        "syncingTo" : "10.163.97.17:27017",
                        "configVersion" : 3,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "10.163.97.16:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 48,
                        "optime" : {
                                "ts" : Timestamp(1503050974, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1503050974, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2017-08-18T10:09:34Z"),
                        "optimeDurableDate" : ISODate("2017-08-18T10:09:34Z"),
                        "lastHeartbeat" : ISODate("2017-08-18T10:08:24.973Z"),
                        "lastHeartbeatRecv" : ISODate("2017-08-18T10:08:23.753Z"),
                        "pingMs" : NumberLong(0),
                        "electionTime" : Timestamp(1503050473, 1),
                        "electionDate" : ISODate("2017-08-18T10:01:13Z"),
                        "configVersion" : 3
                },
                {
                        "_id" : 2,
                        "name" : "10.163.97.17:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 48,
                        "optime" : {
                                "ts" : Timestamp(1503050974, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1503050974, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2017-08-18T10:09:34Z"),
                        "optimeDurableDate" : ISODate("2017-08-18T10:09:34Z"),
                        "lastHeartbeat" : ISODate("2017-08-18T10:08:24.973Z"),
                        "lastHeartbeatRecv" : ISODate("2017-08-18T10:08:23.520Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "10.163.97.16:27017",
                        "configVersion" : 3
                }
        ],
        "ok" : 1
}

数据也同步过来了。
stoners:SECONDARY> db.getMongo().setSlaveOk();
stoners:SECONDARY> use hr
switched to db hr
stoners:SECONDARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
{ "_id" : ObjectId("5996bbcb5945237d11f3052d"), "num" : 2 }

2、分别在3个节点安装配置和启动MongoDB
[root@D2-POMS15 ~]# rpm -qa | grep openssl
openssl098e-0.9.8e-17.el6_2.2.x86_64
openssl-1.0.1e-15.el6.x86_64
[root@D2-POMS15 ~]# mkdir /usr/local/mongodb
[root@D2-POMS15 ~]# tar -xvzf mongodb-linux-x86_64-rhel62-3.4.7.tgz
[root@D2-POMS15 ~]# mv mongodb-linux-x86_64-rhel62-3.4.7/* /usr/local/mongodb/
[root@D2-POMS15 ~]# vim .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH=$PATH:/usr/local/mongodb/bin/
export PATH
[root@D2-POMS15 ~]# . .bash_profile
[root@D2-POMS15 ~]# mkdir /usr/local/mongodb/db
[root@D2-POMS15 ~]# mkdir /usr/local/mongodb/log
[root@D2-POMS15 ~]# touch /usr/local/mongodb/log/mongodb.log
[root@D2-POMS15 ~]# vim /usr/local/mongodb/mongodb.conf
dbpath=/usr/local/mongodb/db
logpath=/usr/local/mongodb/log/mongodb.log
logappend=true
noauth=true
port=27017
fork=true
replSet=stoners
[root@D2-POMS15 ~]# mongod -f /usr/local/mongodb/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 11580
child process started successfully, parent exiting
[root@D2-POMS15 ~]# mongo
MongoDB shell version v3.4.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.7
Welcome to the MongoDB shell.

[root@D2-POMS15 ~]# mongod -f /usr/local/mongodb/mongodb.conf
[root@D2-POMS16 ~]# mongod -f /usr/local/mongodb/mongodb.conf
[root@D2-POMS17 ~]# mongod -f /usr/local/mongodb/mongodb.conf


3、在任一节点进行副本集配置
初始化副本集配置。
语法:
{
    _id : <setname>,
     members : [
         {_id : 0, host : <host0>},
         {_id : 1, host : <host1>},
         {_id : 2, host : <host2>},
     ]
}
配置其中一个节点为仲裁的语法:
{
    _id : "my_replica_set",
     members : [
         {_id : 0, host : "rs1.example.net:27017"},
         {_id : 1, host : "rs2.example.net:27017"},
         {_id : 2, host : "rs3.example.net", arbiterOnly: true},
     ]
}

> rs.initiate({_id:"stoners",members:[{_id:0,host:"10.163.97.15:27017"}]})
{ "ok" : 1 }
stoners:SECONDARY> rs.conf()
{
        "_id" : "stoners",
        "version" : 1,
        "protocolVersion" : NumberLong(1),
        "members" : [
                {
                        "_id" : 0,
                        "host" : "10.163.97.15:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : 60000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("5996b49a33625594149428f2")
        }
}

添加节点:
stoners:PRIMARY> rs.add("10.163.97.16")
{ "ok" : 1 }
stoners:PRIMARY> rs.add("10.163.97.17")
{ "ok" : 1 }

查看状态:
stoners:PRIMARY> rs.status()
{
        "set" : "stoners",
        "date" : ISODate("2017-08-18T09:49:18.307Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1503049755, 1),
                        "t" : NumberLong(1)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1503049755, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1503049755, 1),
                        "t" : NumberLong(1)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "10.163.97.15:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 144,
                        "optime" : {
                                "ts" : Timestamp(1503049755, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2017-08-18T09:49:15Z"),
                        "infoMessage" : "could not find member to sync from",
                        "electionTime" : Timestamp(1503049643, 2),
                        "electionDate" : ISODate("2017-08-18T09:47:23Z"),
                        "configVersion" : 3,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "10.163.97.16:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 86,
                        "optime" : {
                                "ts" : Timestamp(1503049755, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1503049755, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2017-08-18T09:49:15Z"),
                        "optimeDurableDate" : ISODate("2017-08-18T09:49:15Z"),
                        "lastHeartbeat" : ISODate("2017-08-18T09:49:17.787Z"),
                        "lastHeartbeatRecv" : ISODate("2017-08-18T09:49:16.774Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "10.163.97.15:27017",
                        "configVersion" : 3
                },
                {
                        "_id" : 2,
                        "name" : "10.163.97.17:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 52,
                        "optime" : {
                                "ts" : Timestamp(1503049755, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1503049755, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2017-08-18T09:49:15Z"),
                        "optimeDurableDate" : ISODate("2017-08-18T09:49:15Z"),
                        "lastHeartbeat" : ISODate("2017-08-18T09:49:17.787Z"),
                        "lastHeartbeatRecv" : ISODate("2017-08-18T09:49:16.883Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "10.163.97.15:27017",
                        "configVersion" : 3
                }
        ],
        "ok" : 1
}


4、测试数据写入
主节点写入数据:
stoners:PRIMARY> use hr
switched to db hr
stoners:PRIMARY> db.emp.insert({"num":1})
WriteResult({ "nInserted" : 1 })
stoners:PRIMARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }

直接查询副本节点报错:
stoners:SECONDARY> use hr
switched to db hr
stoners:SECONDARY> db.emp.find();
Error: error: {
        "ok" : 0,
        "errmsg" : "not master and slaveOk=false",
        "code" : 13435,
        "codeName" : "NotMasterNoSlaveOk"
}

设置副本节点可读:
stoners:SECONDARY> db.getMongo().setSlaveOk();
stoners:SECONDARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }

另一个副本节点:
stoners:SECONDARY> db.getMongo().setSlaveOk();
stoners:SECONDARY> use hr
switched to db hr
stoners:SECONDARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }


5、测试故障切换
先关闭主节点:
stoners:PRIMARY> use admin
switched to db admin
stoners:PRIMARY> db.shutdownServer();
server should be down...

其中一个副本节点变为了主节点:
stoners:SECONDARY>
stoners:PRIMARY>
stoners:PRIMARY> rs.status()
{
        "set" : "stoners",
        "date" : ISODate("2017-08-18T10:03:49.050Z"),
        "myState" : 1,
        "term" : NumberLong(2),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1503050624, 1),
                        "t" : NumberLong(2)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1503050624, 1),
                        "t" : NumberLong(2)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1503050624, 1),
                        "t" : NumberLong(2)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "10.163.97.15:27017",
                        "health" : 0,
                        "state" : 8,
                        "stateStr" : "(not reachable/healthy)",
                        "uptime" : 0,
                        "optime" : {
                                "ts" : Timestamp(0, 0),
                                "t" : NumberLong(-1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(0, 0),
                                "t" : NumberLong(-1)
                        },
                        "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
                        "optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
                        "lastHeartbeat" : ISODate("2017-08-18T10:03:47.314Z"),
                        "lastHeartbeatRecv" : ISODate("2017-08-18T10:01:03.792Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "Connection refused",
                        "configVersion" : -1
                },
                {
                        "_id" : 1,
                        "name" : "10.163.97.16:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 929,
                        "optime" : {
                                "ts" : Timestamp(1503050624, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2017-08-18T10:03:44Z"),
                        "electionTime" : Timestamp(1503050473, 1),
                        "electionDate" : ISODate("2017-08-18T10:01:13Z"),
                        "configVersion" : 3,
                        "self" : true
                },
                {
                        "_id" : 2,
                        "name" : "10.163.97.17:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 845,
                        "optime" : {
                                "ts" : Timestamp(1503050624, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1503050624, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2017-08-18T10:03:44Z"),
                        "optimeDurableDate" : ISODate("2017-08-18T10:03:44Z"),
                        "lastHeartbeat" : ISODate("2017-08-18T10:03:47.252Z"),
                        "lastHeartbeatRecv" : ISODate("2017-08-18T10:03:47.998Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "10.163.97.16:27017",
                        "configVersion" : 3
                }
        ],
        "ok" : 1
}

在主节点插入数据:
stoners:PRIMARY> use hr
switched to db hr
stoners:PRIMARY> db.emp.insert({"num":2})
WriteResult({ "nInserted" : 1 })
stoners:PRIMARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
{ "_id" : ObjectId("5996bbcb5945237d11f3052d"), "num" : 2 }

在另一个副本节点查看:
stoners:SECONDARY> use hr
switched to db hr
stoners:SECONDARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
{ "_id" : ObjectId("5996bbcb5945237d11f3052d"), "num" : 2 }

现在启动关闭的节点:
[root@D2-POMS15 ~]# mongod -f /usr/local/mongodb/mongodb.conf
启动后自动成为副本节点:
stoners:SECONDARY> rs.status()
{
        "set" : "stoners",
        "date" : ISODate("2017-08-18T10:08:25.401Z"),
        "myState" : 2,
        "term" : NumberLong(2),
        "syncingTo" : "10.163.97.17:27017",
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1503050974, 1),
                        "t" : NumberLong(2)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1503050974, 1),
                        "t" : NumberLong(2)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1503050974, 1),
                        "t" : NumberLong(2)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "10.163.97.15:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 49,
                        "optime" : {
                                "ts" : Timestamp(1503050974, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2017-08-18T10:09:34Z"),
                        "syncingTo" : "10.163.97.17:27017",
                        "configVersion" : 3,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "10.163.97.16:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 48,
                        "optime" : {
                                "ts" : Timestamp(1503050974, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1503050974, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2017-08-18T10:09:34Z"),
                        "optimeDurableDate" : ISODate("2017-08-18T10:09:34Z"),
                        "lastHeartbeat" : ISODate("2017-08-18T10:08:24.973Z"),
                        "lastHeartbeatRecv" : ISODate("2017-08-18T10:08:23.753Z"),
                        "pingMs" : NumberLong(0),
                        "electionTime" : Timestamp(1503050473, 1),
                        "electionDate" : ISODate("2017-08-18T10:01:13Z"),
                        "configVersion" : 3
                },
                {
                        "_id" : 2,
                        "name" : "10.163.97.17:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 48,
                        "optime" : {
                                "ts" : Timestamp(1503050974, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1503050974, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2017-08-18T10:09:34Z"),
                        "optimeDurableDate" : ISODate("2017-08-18T10:09:34Z"),
                        "lastHeartbeat" : ISODate("2017-08-18T10:08:24.973Z"),
                        "lastHeartbeatRecv" : ISODate("2017-08-18T10:08:23.520Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "10.163.97.16:27017",
                        "configVersion" : 3
                }
        ],
        "ok" : 1
}

数据也同步过来了。
stoners:SECONDARY> db.getMongo().setSlaveOk();
stoners:SECONDARY> use hr
switched to db hr
stoners:SECONDARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
{ "_id" : ObjectId("5996bbcb5945237d11f3052d"), "num" : 2 }

关于MongoDB中怎么实现副本集问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯