絶好のMongoDB学習資料の8.Replication (2)

6600 ワード

2. Master/Slave
Master/Slaveは典型的なバックアップスキームであり、MongoDBは「One Master Multi Salver」や「Multi Master One Slave」など多くの導入方式をサポートしています.
まず、簡単な「ミラー・バックアップ」から始めます.
$ sudo mkdir -p /var/mongodb/0
$ sudo mkdir -p /var/mongodb/1

$ sudo ./mongod --fork --logpath /dev/null --dbpath /var/mongodb/0 --master
forked process: 1388
all output going to: /dev/null

$ sudo ./mongod --fork --logpath /dev/null --dbpath /var/mongodb/1 --port 27018 --slave --source localhost:27017 --autoresync
forked process: 1401
all output going to: /dev/null

Autoresyncパラメータは、システムに予期せぬ事態が発生し、プライマリ・スレーブ・データが同期しない場合に自動的にレプリケーション・オペレーションを開始します(同期レプリケーションは10分以内に1回のみ実行されます).このほか、更新頻度(秒)を--slavedelayで設定することもできます.isMaster、printReplicationInfo、printSlaveReplicationInfoなどのコマンドを使用して、関連するステータス情報を取得します.
$ ./mongo 

> db.isMaster()
{ "ismaster" : true, "ok" : 1 }

> db.printReplicationInfo()
configured oplog size:   990MB
log length start to end: 1164secs (0.32hrs)
oplog first event time:  Mon Aug 23 2010 12:23:54 GMT+0800 (CST)
oplog last event time:   Mon Aug 23 2010 12:43:18 GMT+0800 (CST)
now:                     Mon Aug 23 2010 12:43:24 GMT+0800 (CST)

$ ./mongo localhost:27018

> db.isMaster()
{ "ismaster" : false, "ok" : 1 }

> db.printSlaveReplicationInfo()
source:   localhost:27017
         syncedTo: Mon Aug 23 2010 12:43:58 GMT+0800 (CST)
                 = 10secs ago (0hrs)

> show dbs
admin
local
test

> use local
switched to db local

> show collections
me
pair.sync
sources
system.indexes

> db.sources.find()
{ "_id" : ObjectId("4c71f8178d806ad3f54dd89a"), "host" : "localhost:27017", "source" : "main", "syncedTo" : { "t" : 1282538738000, "i" : 1 }, "localLogTs" : { "t" : 0, "i" : 0 } }

Slaveの構成情報はlocalに保存する.sourcesで.通常、プライマリ・スレーブ・スキーマを使用して読み書き分離を実現しますが、Slave_を設定する必要があります.OK.
$ ipython

IPython 0.10 -- An enhanced Interactive Python.

In [1]: from pymongo import *

In [2]: m_conn = Connection()

In [3]: s_conn = Connection(host="localhost:27018", slave_okay=True)

In [4]: m_db = m_conn.test

In [5]: s_db = s_conn.test

# ----------------------------------------- #

In [6]: m_db.users.insert({"name":"user3"})
Out[6]: ObjectId('4c71feb0499b140632000000')

In [7]: s_db.users.find_one({"name":"user3"}) #   Slave
Out[7]: {u'_id': ObjectId('4c71feb0499b140632000000'), u'name': u'user3'}

In [8]: m_db.users.remove({"name":"user3"}) #   Master  

In [9]: for u in m_db.users.find(): print u
   ....:
{u'_id': ObjectId('4c71fa4d5e01e1ba6d62398f'), u'name': u'user1'}

In [10]: for u in s_db.users.find(): print u # Slave  
   ....:
{u'_id': ObjectId('4c71fa4d5e01e1ba6d62398f'), u'name': u'user1'}

# ----------------------------------------- #

In [11]: s_db.users.insert({"name":"userx"}) #   Slave  
Out[11]: ObjectId('4c71ff2c499b140632000001')

In [12]: for u in m_db.users.find(): print u # Master  
   ....:
{u'_id': ObjectId('4c71fa4d5e01e1ba6d62398f'), u'name': u'user1'}

In [13]: for u in s_db.users.find(): print u # Slave  
   ....:
{u'_id': ObjectId('4c71fa4d5e01e1ba6d62398f'), u'name': u'user1'}

Slaveの読み取りは正常ですが、書き込みは効果がありません.「One Slave Two Master」スキームを導入することもできます.
$ sudo mkdir -p /var/mongodb/m1
$ sudo mkdir -p /var/mongodb/m2
$ sudo mkdir -p /var/mongodb/s

$ sudo ./mongod --fork --dbpath /var/mongodb/m1 --logpath /dev/null --master
forked process: 1616
all output going to: /dev/null

$ sudo ./mongod --fork --dbpath /var/mongodb/m2 --logpath /dev/null --port 27018 --master
forked process: 1627
all output going to: /dev/null

$ sudo ./mongod --fork --dbpath /var/mongodb/s --logpath /dev/null --port 27019 --slave
forked process: 1638
all output going to: /dev/null

Slaveに接続し、構成を追加します.
$ ./mongo localhost:27019

MongoDB shell version: 1.6.1
connecting to: localhost:27019/test

> use local
switched to db local

> db.sources.insert({host:"localhost:27017"})
> db.sources.insert({host:"localhost:27018"})

> db.printSlaveReplicationInfo()
source:   localhost:27017
         syncedTo: Mon Aug 23 2010 13:06:03 GMT+0800 (CST)
                 = 17secs ago (0hrs)
source:   localhost:27018
         doing initial sync

2台のMasterにデータを追加し、コピー効果を確認します.
$ ./mongo localhost:27017

MongoDB shell version: 1.6.1
connecting to: localhost:27017/test

> use db1
switched to db db1

> db.users.insert({name:"user1"})
> db.users.insert({name:"user2"})

> db.users.find()
{ "_id" : ObjectId("4c720211ade34e85c5380eab"), "name" : "user1" }
{ "_id" : ObjectId("4c720214ade34e85c5380eac"), "name" : "user2" }

> exit
bye
$ ./mongo localhost:27018

MongoDB shell version: 1.6.1
connecting to: localhost:27018/test

> use db2
switched to db db2

> db.blogs.insert({title:"aaa"})
> db.blogs.insert({title:"bbb"})
> db.blogs.insert({title:"ccc"})

> db.blogs.find()
{ "_id" : ObjectId("4c720236f24118cb2f59218d"), "title" : "aaa" }
{ "_id" : ObjectId("4c720239f24118cb2f59218e"), "title" : "bbb" }
{ "_id" : ObjectId("4c72023cf24118cb2f59218f"), "title" : "ccc" }

> exit
bye
$ ./mongo localhost:27019

MongoDB shell version: 1.6.1
connecting to: localhost:27019/test

> show dbs
admin
db1
db2
local

> use db1
switched to db db1

> db.users.find()
{ "_id" : ObjectId("4c720211ade34e85c5380eab"), "name" : "user1" }
{ "_id" : ObjectId("4c720214ade34e85c5380eac"), "name" : "user2" }

> use db2
switched to db db2

> db.blogs.find()
{ "_id" : ObjectId("4c720236f24118cb2f59218d"), "title" : "aaa" }
{ "_id" : ObjectId("4c720239f24118cb2f59218e"), "title" : "bbb" }
{ "_id" : ObjectId("4c72023cf24118cb2f59218f"), "title" : "ccc" }
>

すべて正常です.