MongoDB:搭建三节点 Replica Set 环境

今天学习了搭建 MongDB 复制环境,实验环境是在虚拟机上同一系统,并搭建三节点 Replica Set,
根据文档上的描述,mongodb 复制配置简单,并且能够自动 failover,这些高级特性以后再实验,这里仅
描述配置过程:

一 环境信息
虚拟机:         Red Hat Enterprise Linux Server release 6.2
主机名:       redhatB.example.com
MongoDB 版本:v2.2.1

  端口 数据目录
 主结点  27018  /mongodb/data01
 从节点一  27019  /mongodb/data02
 从节点二  27020  /mongodb/data03

二 搭建步骤
--2.1 创建数据目录

 [mongo@redhatB mongodb]$ mkdir -p /mongodb/data01
[mongo@redhatB mongodb]$ mkdir -p /mongodb/data02
[mongo@redhatB mongodb]$ mkdir -p /mongodb/data03

备注:三个目录分别为三个节点数据目录,并设置好目录权限。

--2.2 创建配置文件
--2.2.1 主节点配置文件

 touch /mongodb/data01/mongodb_27018.conf, 写入以下:

fork = true
port = 27018
dbpath = /mongodb/data01
logpath = /mongodb/data01/mongo.log
logappend = true
replSet = rs0

--2.2.2 从节点一配置

 touch /mongodb/data02/mongodb_27019.conf, 写入以下:

fork = true
port = 27019
dbpath = /mongodb/data02
logpath = /mongodb/data02/mongo.log
logappend = true
replSet = rs0

--2.2.3 从节点二配置

 touch /mongodb/data03/mongodb_27020.conf, 写入以下:

fork = true
port = 27020
dbpath = /mongodb/data03
logpath = /mongodb/data03/mongo.log
logappend = true
replSet = rs0

--2.3 启动主从节点

 mongod -f /mongodb/data01/mongodb_27018.conf
mongod -f /mongodb/data02/mongodb_27019.conf
mongod -f /mongodb/data03/mongodb_27020.conf

--2.4 初始化 Replica Sets
--2.4.1 登陆结点一初始化 

 [mongo@redhatB mongodb]$ mongo 127.0.0.1:27018
MongoDB shell version: 2.2.1
connecting to: 127.0.0.1:27018/test

> rs.inirs.initiate();
{
        "info2" : "no configuration explicitly specified -- making one",
        "me" : "redhatB.example.com:27018",
        "info" : "Config now saved locally.  Should come online in about a minute.",
        "ok" : 1
}

备注:使用的是默认配置。

--2.5 查看 Replica Sets 配置

 > rs.conf();
{
        "_id" : "rs0",
        "version" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "redhatB.example.com:27018"
                }
        ]
}

备注:只能看到一个节点信息;

--2.6 增加从节点

 rs0:PRIMARY> rs.add("redhatB.example.com:27019");
{ "ok" : 1 }
rs0:PRIMARY> rs.add("redhatB.example.com:27020");
{ "ok" : 1 }
rs0:PRIMARY>

--2.7 再次查看 Replica Sets 配置

 rs0:PRIMARY> rs.conf();
{
        "_id" : "rs0",
        "version" : 3,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "redhatB.example.com:27018"
                },
                {
                        "_id" : 1,
                        "host" : "redhatB.example.com:27019"
                },
                {
                        "_id" : 2,
                        "host" : "redhatB.example.com:27020"
                }
        ]
}

备注:这时已经能看到三个节点信息。

--2.8 查看 Replica Sets 状态

 rs0:PRIMARY> rs.status();
{
        "set" : "rs0",
        "date" : ISODate("2012-11-22T08:43:21Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "redhatB.example.com:27018",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 2445,
                        "optime" : Timestamp(1353573735000, 1),
                        "optimeDate" : ISODate("2012-11-22T08:42:15Z"),
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "redhatB.example.com:27019",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 74,
                        "optime" : Timestamp(1353573735000, 1),
                        "optimeDate" : ISODate("2012-11-22T08:42:15Z"),
                        "lastHeartbeat" : ISODate("2012-11-22T08:43:21Z"),
                        "pingMs" : 0
                },
                {
                        "_id" : 2,
                        "name" : "redhatB.example.com:27020",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 66,
                        "optime" : Timestamp(1353573735000, 1),
                        "optimeDate" : ISODate("2012-11-22T08:42:15Z"),
                        "lastHeartbeat" : ISODate("2012-11-22T08:43:19Z"),
                        "pingMs" : 0
                }
        ],
        "ok" : 1
}

备注:rs.status() 命令可以显示节点状态具体信息。
  
  
三 测试
--3.1 登陆主节点

 [mongo@redhatB mongodb]$ mongo 127.0.0.1:27018
MongoDB shell version: 2.2.1
connecting to: 127.0.0.1:27018/test

rs0:PRIMARY> db.test_1.save({id:1});

rs0:PRIMARY> db.test_1.find();
{ "_id" : ObjectId("50ade66efabbde9e747577b1"), "id" : 1 }

备注:主节点可以读写;

--3.2 登陆从节点一

 [mongo@redhatB mongodb]$ mongo 127.0.0.1:27019
MongoDB shell version: 2.2.1
connecting to: 127.0.0.1:27019/test
rs0:SECONDARY> show dbs;show dbs;
local   0.125GB
test    0.0625GB
rs0:SECONDARY> show collections;
Thu Nov 22 16:48:15 uncaught exception: error: { "$err" : "not master and slaveOk=false", "code" : 13435 }
rs0:SECONDARY>

备注:从节点不可读写,但能看到主库创建的集合,从库需要使用 rs.slaveOk() 命令开启只读。
 
--3.3 开启从库只读模式

 [mongo@redhatB mongodb]$ mongo 127.0.0.1:27019
MongoDB shell version: 2.2.1
connecting to: 127.0.0.1:27019/test

rs0:SECONDARY> rs.slaveOk();

rs0:SECONDARY> show collections;
system.indexes
test_1

rs0:SECONDARY> db.test_1.find();
{ "_id" : ObjectId("50ade66efabbde9e747577b1"), "id" : 1 }

rs0:SECONDARY> db.test_1.save({id:2});db.
not master

备注:这时从库可读了(不可写),同理设置从库二。 到了这里,三节点 Replica Sets 环境已经全部
           搭建好了,步骤比较简单。

上一篇:OpenStack Mitaka 版本中的 domain 和 admin


下一篇:Inno Setup 安装