Tag Archives: mongodb

Конспект лекций M102 MongoDB for DBAs (Week 4)


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /var/www/bezhaco/data/www/bezha.od.ua/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Лекция целиком и полностью посвящена Replication

Создадим тестовые папки

# mkdir 1 2 3

Запускаем реплику

# mongod --dbpath 1 --port 27001 --smallfiles --oplogSize 50 --replSet S1
# mongod --dbpath 2 --port 27002 --smallfiles --oplogSize 50 --replSet S1
# mongod --dbpath 3 --port 27003 --smallfiles --oplogSize 50 --replSet S1
[rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
[rsStart] replSet info you may need to run replSetInitiate -- rs.initiate() in the shell

Запускаем в бэкграунде, форкаем

# mongod --dbpath 1 --port 27001 --smallfiles --oplogSize 50 --replSet S1 --fork --logpath log.1
# mongod --dbpath 2 --port 27002 --smallfiles --oplogSize 50 --replSet S1 --fork --logpath log.2
# mongod --dbpath 3 --port 27003 --smallfiles --oplogSize 50 --replSet S1 --fork --logpath log.3
[rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
[rsStart] replSet info you may need to run replSetInitiate -- rs.initiate() in the shell

Необходимо

1. Создать Config
2. Инициализировать данные: replSetInitiate или -- rs.initiate()

Создаем Config

# mongo --port 27001 --shell
> cfg ={ _id: "S1" , 
members : [ { _id:0 , host: "mongod.bezha.od.ua:27001"}, 
            { _id:1 , host: "mongod.bezha.od.ua:27002"}, 
            { _id:2 , host: "mongod.bezha.od.ua:27003"} 
]
}
cfg
{
        "_id" : "S1",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "mongod.bezha.od.ua:27001"
                },
                {
                        "_id" : 1,
                        "host" : "mongod.bezha.od.ua:27002"
                },
                {
                        "_id" : 2,
                        "host" : "mongod.bezha.od.ua:27003"
                }
        ]
}

Инициализируем данные

> rs.initiate(cfg)
{
        "info" : "Config now saved locally.  Should come online in about a minute.",
        "ok" : 1
}
>
S1:STARTUP2>
S1:SECONDARY> db.isMaster()
{
        "setName" : "S1",
        "ismaster" : false,
        "secondary" : true,
        "hosts" : [
                "mongod.bezha.od.ua:27001",
                "mongod.bezha.od.ua:27003",
                "mongod.bezha.od.ua:27002"
        ],
        "me" : "mongod.bezha.od.ua:27001",
        "maxBsonObjectSize" : 16777216,
        "localTime" : ISODate("2012-11-18T01:56:08.073Z"),
        "ok" : 1
}
S1:PRIMARY> rs.status()
{
        "set" : "S1",
        "date" : ISODate("2012-11-18T01:58:19Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "mongod.bezha.od.ua:27001",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 281,
                        "optime" : Timestamp(1353203756000, 1),
                        "optimeDate" : ISODate("2012-11-18T01:55:56Z"),
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "mongod.bezha.od.ua:27002",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 141,
                        "optime" : Timestamp(1353203756000, 1),
                        "optimeDate" : ISODate("2012-11-18T01:55:56Z"),
                        "lastHeartbeat" : ISODate("2012-11-18T01:58:18Z"),
                        "pingMs" : 0
                },
                {
                        "_id" : 2,
                        "name" : "mongod.bezha.od.ua:27003",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 141,
                        "optime" : Timestamp(1353203756000, 1),
                        "optimeDate" : ISODate("2012-11-18T01:55:56Z"),
                        "lastHeartbeat" : ISODate("2012-11-18T01:58:18Z"),
                        "pingMs" : 0
                }
        ],
        "ok" : 1
}
# mongo --port 27002 --shell
S1:SECONDARY> show collections
Sun Nov 25 14:02:47 uncaught exception: error: { "$err" : "not master and slaveOk=false", "code" : 13435 }
S1:SECONDARY> rs.slaveOk()
S1:SECONDARY> show collections
S1:SECONDARY>

Опции которые можно устанавливать при Replica Set Configuration

Синтаксис

cfg ={ _id: "S1" , 
members : [ { _id:0 , host: "mongod.bezha.od.ua:27001", }, 
            { _id:1 , host: "mongod.bezha.od.ua:27002", }, 
            { _id:2 , host: "mongod.bezha.od.ua:27003", } 
]
}

Опции могут быть

<arbiterOnly : <boolean>,>
<priority: <priority>,>
<hidden : <boolean>,>
<slaveDelay : <number>,>
<tags: { <document> },>
<votes : <number>>

Добавим опций в Replica

S1:PRIMARY> cfg = rs.conf()
{
        "_id" : "S1",
        "version" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "mongod.bezha.od.ua:27001"
                },
                {
                        "_id" : 1,
                        "host" : "2mongod.bezha.od.ua:27002"
                },
                {
                        "_id" : 2,
                        "host" : "mongod.bezha.od.ua:27003"
                }
        ]
}
S1:PRIMARY> cfg.members[2].slaveDelay = 8 * 3600
28800
S1:PRIMARY> cfg.members[2].hidden = true
true
S1:PRIMARY> cfg.members[2].priority = 0
0
S1:PRIMARY> rs.reconfig(cfg)
DBClientCursor::init call() failed
query failed : admin.$cmd ...
trying reconnect to 127.0.0.1:27001
reconnect 127.0.0.1:27001 ok
reconnected to server after rs command (which is normal)

Результат

S1:PRIMARY> rs.conf()
{
        "_id" : "S1",
        "version" : 2,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "mongod.bezha.od.ua:27001"
                },
                {
                        "_id" : 1,
                        "host" : "mongod.bezha.od.ua:27002"
                },
                {
                        "_id" : 2,
                        "host" : "mongod.bezha.od.ua:27003",
                        "priority" : 0,
                        "hidden" : true
                }
        ]
}