`
ihuangweiwei
  • 浏览: 266585 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

MongoDB副本集

阅读更多

一、节点类型

standard:常规节点,它存储一份完整的数据副本,参与选举投票,有可能成为活跃节点。
passive:存储了完整的数据副本,参与投票,不能成为活跃节点。
arbiter:仲裁节点,只参与投票,不接收复制的数据,也不能成为活跃节点。

 

二、参数说明

--dbpath   数据文件路径
--logpath  日志文件路径
--port        端口号,默认是27017.我这里使用的也是这个端口号.
--replSet   复制集的名字,一个replica sets中的每个节点的这个参数都要用一个复制集名字,这里是test.
--replSet   这个后面跟的是其他standard节点的ip和端口
--maxConns   最大连接数
--fork       后台运行
--logappend   日志文件循环使用,如果日志文件已满,那么新日志覆盖最久日志。

 

三、创建副本集

环境说明:

ip:10.0.0.3
ip:10.0.0.4
ip:10.0.0.5

 启动第一个standard节点(ip:10.0.0.3)

 

hww@ubuntu:~$ mkdir -p ~/mongo/db ~/mongo/log
hww@ubuntu:~$ mongod -dbpath ~/mongo/db -logpath ~/mongo/log/mongod.log -port 27037 -replSet test -maxConns=2000 -fork -logappend

 

启动第一个standard节点(ip:10.0.0.4)

hww@ubuntu:~$ mkdir -p ~/mongo/db ~/mongo/log
hww@ubuntu:~$ mongod -dbpath ~/mongo/db -logpath ~/mongo/log/mongod.log -port 27037 -replSet test -maxConns=2000 -fork -logappend

 

启动arbiter节点,也就是仲裁节点 (ip:10.0.0.5)

hww@ubuntu:~$ mkdir -p ~/mongo/db ~/mongo/log
hww@ubuntu:~$ mongod -dbpath ~/mongo/db -logpath ~/mongo/log/mongod.log -port 27037 -replSet test -maxConns=2000 -fork -logappend

 

启动了以上服务器后,日志告诉你副本集没有初始化。因为还差最后一步:在shell中初始化副本集。

连接其中一台standard节点服务器。初始化命令只能执行一次

hww@ubuntu:~$ mongo 10.0.0.3:27037/admin
MongoDB shell version: 2.0.1
connecting to: 10.0.0.3:27037/admin
> db.runCommand({"replSetInitiate" : {
...     "_id" : "test",
...     "members" : [
...     {
...     "_id" : 0,
...     "host" : "10.0.0.3:27037"
...     },
...     {
...     "_id" : 1,
...     "host" : "10.0.0.4:27037"
...     }
...     ]}})
{
        "info" : "Config now saved locally.  Should come online in about a minute.",
        "ok" : 1
}




> 

执行以下shell命令后,如果看见红色字体标记的内容,则表示初始化成功




  shell命令:

db.runCommand({"replSetInitiate" : {
    "_id" : "test",
    "members" : [
    {
    "_id" : 0,
    "host" : "10.0.0.3:27037"
    },
    {
    "_id" : 1,
    "host" : "10.0.0.4:27037"
    }
    ]}})

你也可以通过命令查看效果:

> rs.status()
{
        "set" : "test",
        "date" : ISODate("2011-12-06T07:44:29Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "10.0.0.3:27037",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "optime" : {
                                "t" : 1323157008000,
                                "i" : 1
                        },
                        "optimeDate" : ISODate("2011-12-06T07:36:48Z"),
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "10.0.0.4:27037",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 455,
                        "optime" : {
                                "t" : 1323157008000,
                                "i" : 1
                        },
                        "optimeDate" : ISODate("2011-12-06T07:36:48Z"),
                        "lastHeartbeat" : ISODate("2011-12-06T07:44:28Z"),
                        "pingMs" : 2
                }
        ],
        "ok" : 1
}

 

加入仲裁节点

执行以下命令:

 

PRIMARY> rs.addArb("10.0.0.5:27037");
{ "ok" : 1 }
 

我们可以再次查看当前状态:

PRIMARY> rs.status()
{
        "set" : "test",
        "date" : ISODate("2011-12-06T07:46:21Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "10.0.0.3:27037",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "optime" : {
                                "t" : 1323157549000,
                                "i" : 1
                        },
                        "optimeDate" : ISODate("2011-12-06T07:45:49Z"),
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "10.0.0.4:27037",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 567,
                        "optime" : {PRIMARY> db.isMaster();
{
        "setName" : "test",
        "ismaster" : true,
        "secondary" : false,
        "hosts" : [
                "10.0.0.3:27037",
                "10.0.0.4:27037"
        ],
        "arbiters" : [
                "10.0.0.5:27037"
        ],
        "primary" : "10.0.0.3:27037",
        "me" : "10.0.0.3:27037",
        "maxBsonObjectSize" : 16777216,
        "ok" : 1
}
 
                                "t" : 1323157549000,
                                "i" : 1
                        },
                        "optimeDate" : ISODate("2011-12-06T07:45:49Z"),
                        "lastHeartbeat" : ISODate("2011-12-06T07:46:20Z"),
                        "pingMs" : 0
                },
                {
                        "_id" : 2,
                        "name" : "10.0.0.5:27037",
                        "health" : 1,
                        "state" : 7,
                        "stateStr" : "ARBITER",
                        "uptime" : 22,
                        "optime" : {
                                "t" : 0,
                                "i" : 0
                        },
                        "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
                        "lastHeartbeat" : ISODate("2011-12-06T07:46:19Z"),
                        "pingMs" : 0
                }
        ],
        "ok" : 1
}

   rs.status()通过这个命令,可以查看各个节点的ip、角色已经是否正常

   我们看到已经成功配置。

 

查看活跃节点:

PRIMARY> db.isMaster();
{
        "setName" : "test",
        "ismaster" : true,
        "secondary" : false,
        "hosts" : [
                "10.0.0.3:27037",
                "10.0.0.4:27037"
        ],
        "arbiters" : [
                "10.0.0.5:27037"
        ],
        "primary" : "10.0.0.3:27037",
        "me" : "10.0.0.3:27037",
        "maxBsonObjectSize" : 16777216,
        "ok" : 1
}
 

   可以看到现在10.0.0.3:27037为活跃节点。

 

检测是否配置成功

   可以强制primary和standard节点角色互换,从而验证是否能够实现副本集功能

 

rs.stepDown()

 

   执行完成后:

 

SECONDARY> db.isMaster();
{
        "setName" : "test",
        "ismaster" : false,
        "secondary" : true,
        "hosts" : [
                "10.0.0.3:27037",
                "10.0.0.4:27037"
        ],
        "arbiters" : [
                "10.0.0.5:27037"
        ],
        "primary" : "10.0.0.4:27037",
        "me" : "10.0.0.3:27037",
        "maxBsonObjectSize" : 16777216,
        "ok" : 1
}

   可以看到现在主节点已经修改为10.0.0.4:27037了。

 

 我们也可以动态扩展增加节点(ip:10.0.0.5:27047)

 

hww@ubuntu:~$ mkdir -p ~/mongo1/db ~/mongo1/log
hww@ubuntu:~$ mongod -dbpath ~/mongo1/db -logpath ~/mongo1/log/mongod.log -port 27037 -replSet test -maxConns=2000 -fork -logappend
 

  进入活跃节点的服务器

 

hww@ubuntu4:~$ mongo 10.0.0.4:27037/admin
MongoDB shell version: 2.0.1
connecting to: 10.0.0.4:27037/admin
PRIMARY> rs.add("10.0.0.5:27047");
{ "ok" : 1 }
PRIMARY> 
 
PRIMARY> rs.status()
{
        "set" : "test",
        "date" : ISODate("2011-12-06T07:59:36Z"),
        "myState" : 1,
        "syncingTo" : "10.0.0.3:27037",
        "members" : [
                {
                        "_id" : 0,
                        "name" : "10.0.0.3:27037",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 1263,
                        "optime" : {
                                "t" : 1323158245000,
                                "i" : 1
                        },
                        "optimeDate" : ISODate("2011-12-06T07:57:25Z"),
                        "lastHeartbeat" : ISODate("2011-12-06T07:59:35Z"),
                        "pingMs" : 0
                },
                {
                        "_id" : 1,
                        "name" : "10.0.0.4:27037",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "optime" : {
                                "t" : 1323158245000,
                                "i" : 1
                        },
                        "optimeDate" : ISODate("2011-12-06T07:57:25Z"),
                        "self" : true
                },
                {
                        "_id" : 2,
                        "name" : "10.0.0.5:27037",
                        "health" : 1,
                        "state" : 7,
                        "stateStr" : "ARBITER",
                        "uptime" : 717,
                        "optime" : {
                                "t" : 0,
                                "i" : 0
                        },
                        "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
                        "lastHeartbeat" : ISODate("2011-12-06T07:59:35Z"),
                        "pingMs" : 0
                },
                {
                        "_id" : 3,
                        "name" : "10.0.0.5:27047",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 125,
                        "optime" : {
                                "t" : 1323158245000,
                                "i" : 1
                        },
                        "optimeDate" : ISODate("2011-12-06T07:57:25Z"),
                        "lastHeartbeat" : ISODate("2011-12-06T07:59:35Z"),
                        "pingMs" : 0
                }
        ],
        "ok" : 1
}

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics