Skip to content

MongoDB 副本集

🏷️ MongoDB

创建三个目录用于存放数据。

bash
cd /mongodb/
mkdir ./mongo1 ./mongo2 ./mongo3

打开三个控制台,分别启动一个 mongo 服务器。

bash
cd /mongodb/
./bin/mongod --replSet book --dbpath ./mongo1 --port 27011 --rest

cd /mongodb/
./bin/mongod --replSet book --dbpath ./mongo2 --port 27012 --rest

cd /mongodb/
./bin/mongod --replSet book --dbpath ./mongo3 --port 27013 --rest

连接一个服务器:

bash
[liujj@localhost ~]$ cd /mongodb/
[liujj@localhost mongodb]$ ./bin/mongo localhost:27011
MongoDB shell version: 3.2.4
connecting to: localhost:27011/test
Server has startup warnings: 
2016-04-06T05:56:40.572-0700 I CONTROL  [main] ** WARNING: --rest is specified without --httpinterface,
2016-04-06T05:56:40.572-0700 I CONTROL  [main] **          enabling http interface
>

执行 rs.initiate()rs.status() 命令

点击查看命令执行结果
js
> rs.initiate({
...     _id: 'book',
...     members: [
...         {_id: 1, host: 'localhost:27011'},
...         {_id: 2, host: 'localhost:27012'},
...         {_id: 3, host: 'localhost:27013'}
...     ]
... })
{ "ok" : 1 }
book:OTHER> rs.status()
{
    "set" : "book",
    "date" : ISODate("2016-04-06T13:01:21.975Z"),
    "myState" : 2,
    "term" : NumberLong(0),
    "heartbeatIntervalMillis" : NumberLong(2000),
    "members" : [
        {
            "_id" : 1,
            "name" : "localhost:27011",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 281,
            "optime" : {
                "ts" : Timestamp(1459947676, 1),
                "t" : NumberLong(-1)
            },
            "optimeDate" : ISODate("2016-04-06T13:01:16Z"),
            "infoMessage" : "could not find member to sync from",
            "configVersion" : 1,
            "self" : true
        },
        {
            "_id" : 2,
            "name" : "localhost:27012",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 5,
            "optime" : {
                "ts" : Timestamp(1459947676, 1),
                "t" : NumberLong(-1)
            },
            "optimeDate" : ISODate("2016-04-06T13:01:16Z"),
            "lastHeartbeat" : ISODate("2016-04-06T13:01:21.362Z"),
            "lastHeartbeatRecv" : ISODate("2016-04-06T13:01:19.381Z"),
            "pingMs" : NumberLong(0),
            "configVersion" : 1
        },
        {
            "_id" : 3,
            "name" : "localhost:27013",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 5,
            "optime" : {
                "ts" : Timestamp(1459947676, 1),
                "t" : NumberLong(-1)
            },
            "optimeDate" : ISODate("2016-04-06T13:01:16Z"),
            "lastHeartbeat" : ISODate("2016-04-06T13:01:21.362Z"),
            "lastHeartbeatRecv" : ISODate("2016-04-06T13:01:19.382Z"),
            "pingMs" : NumberLong(0),
            "configVersion" : 1
        }
    ],
    "ok" : 1
}

此时连接另外两个服务器,可以看到另两个是从服务器(book:SECONDARY)。连接主服务器插入一条记录。

js
book:PRIMARY> db.echo.insert({ say : 'HELLO!' })
WriteResult({ "nInserted" : 1 })
book:PRIMARY> db.echo.find()
{ "_id" : ObjectId("570509c0cf335091707f4905"), "say" : "HELLO!" }

此时在从服务器中执行插入或者查询,会发生错误。

js
book:SECONDARY> db.echo.insert({ say : 'HELLO!' })
WriteResult({ "writeError" : { "code" : 10107, "errmsg" : "not master" } })
book:SECONDARY> db.echo.find()
Error: error: { "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435 }

手动关闭主服务器,此时另外两个从服务器中的一台会自动变成主服务器。

可以使用 db.isMaster() 确认那个是主服务器。

点击查看命令执行结果
js
book:PRIMARY> db.isMaster()
{
    "hosts" : [
        "localhost:27011",
        "localhost:27012",
        "localhost:27013"
    ],
    "setName" : "book",
    "setVersion" : 1,
    "ismaster" : true,
    "secondary" : false,
    "primary" : "localhost:27013",
    "me" : "localhost:27013",
    "electionId" : ObjectId("7fffffff0000000000000002"),
    "maxBsonObjectSize" : 16777216,
    "maxMessageSizeBytes" : 48000000,
    "maxWriteBatchSize" : 1000,
    "localTime" : ISODate("2016-04-06T13:21:24.192Z"),
    "maxWireVersion" : 4,
    "minWireVersion" : 0,
    "ok" : 1
}

可以看出 27013 变成了主服务器。在该服务器中查询之前插入的数据。

js
book:PRIMARY> db.echo.find()
{ "_id" : ObjectId("570509c0cf335091707f4905"), "say" : "HELLO!" }