Excellent MongoDB learning materials 8. Replication (2)

Source: Internet
Author: User
2. MasterSlaveMasterSlave is a typical backup solution. MongoDB supports multiple deployment methods, such as OneMasterMultiSalver and MultiMasterOneSlave. Start with a simple backup of the image. $ Sudomkdir-pvarmongodb0 $ sudomkdir-pvarmongodb1 $ sudo. mongod --

2. Master/Slave is a typical backup solution. MongoDB supports One Master Multi Salver and Multi Master One Slave among other deployment modes. Start with a simple backup of the image. $ Sudo mkdir-p/var/mongodb/0 $ sudo mkdir-p/var/mongodb/1 $ sudo./mongod --

2. Master/Slave

Master/Slave is a typical backup solution. MongoDB supports multiple deployment methods, such as "One Master Multi Salver" and "Multi Master One Slave.

Start with a simple "backup image.

$ sudo mkdir -p /var/mongodb/0$ sudo mkdir -p /var/mongodb/1$ sudo ./mongod --fork --logpath /dev/null --dbpath /var/mongodb/0 --masterforked process: 1388all output going to: /dev/null$ sudo ./mongod --fork --logpath /dev/null --dbpath /var/mongodb/1 --port 27018 --slave --source localhost:27017 --autoresyncforked process: 1401all output going to: /dev/null

The autoresync parameter automatically starts the replication operation when the Master/Slave Data is in different steps due to system exceptions (synchronous replication is performed only once within 10 minutes ). In addition, you can use -- slavedelay to set the update frequency (in seconds ).

Use commands such as isMaster, printReplicationInfo, and printSlaveReplicationInfo to obtain relevant status information.

$ ./mongo > db.isMaster(){ "ismaster" : true, "ok" : 1 }> db.printReplicationInfo()configured oplog size:   990MBlog 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 dbsadminlocaltest> use localswitched to db local> show collectionsmepair.syncsourcessystem.indexes> db.sources.find(){ "_id" : ObjectId("4c71f8178d806ad3f54dd89a"), "host" : "localhost:27017", "source" : "main", "syncedTo" : { "t" : 1282538738000, "i" : 1 }, "localLogTs" : { "t" : 0, "i" : 0 } }

The Slave configuration information is saved in local. sources.

We usually use the Master/Slave scheme for read/write splitting, but we need to set Slave_ OK.

$ IpythonIPython 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.testIn [5]: s_db = s_conn.test # metadata # In [6]: m_db.users.insert ({"name": "user3"}) Out [6]: objectId ('4c71feb0499b140632000000 ') In [7]: s_db.users.find_one ({"name": "user3"}) # data is copied to SlaveOut [7]: {u' _ id ': objectId ('4c71feb0499b140632000000 '), u 'name': u 'user3'} In [8]: m_db.users.remove ({"name": "user3 "}) # Delete Master data 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 # The Slave record is deleted synchronously ....: {U' _ id': ObjectId ('4c71fa4d5e01e1ba6d62398f'), u 'name': u'user1'} # region # In [11]: s_db.users.insert ({"name ": "userx"}) # insert data Out of Slave [11]: ObjectId ('4c71ff2c499b140632000001 ') In [12]: for u in m_db.users.find (): print u # This data does not exist on the Master ....: {U' _ id': ObjectId ('4c71fa4d5e01e1ba6d62398f'), u 'name': u'user1'} In [13]: for u in s_db.users.find (): print u # Slave also does not have ....: {U' _ id': ObjectId ('4c71fa4d5e01e1ba6d62398f'), u 'name': u'user1 '}

It can be seen that the Slave read operation is normal, but the write operation is ineffective.

We can also deploy the "One Slave Two Master" solution.

$ 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 --masterforked process: 1616all output going to: /dev/null$ sudo ./mongod --fork --dbpath /var/mongodb/m2 --logpath /dev/null --port 27018 --masterforked process: 1627all output going to: /dev/null$ sudo ./mongod --fork --dbpath /var/mongodb/s --logpath /dev/null --port 27019 --slaveforked process: 1638all output going to: /dev/null

Connect to Slave and add configurations.

$ ./mongo localhost:27019MongoDB shell version: 1.6.1connecting to: localhost:27019/test> use localswitched 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

Add data to the two masters to view the replication effect.

$ ./mongo localhost:27017MongoDB shell version: 1.6.1connecting to: localhost:27017/test> use db1switched 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" }> exitbye

$ ./mongo localhost:27018MongoDB shell version: 1.6.1connecting to: localhost:27018/test> use db2switched 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" }> exitbye

$ ./mongo localhost:27019MongoDB shell version: 1.6.1connecting to: localhost:27019/test> show dbsadmindb1db2local> use db1switched to db db1> db.users.find(){ "_id" : ObjectId("4c720211ade34e85c5380eab"), "name" : "user1" }{ "_id" : ObjectId("4c720214ade34e85c5380eac"), "name" : "user2" }> use db2switched to db db2> db.blogs.find(){ "_id" : ObjectId("4c720236f24118cb2f59218d"), "title" : "aaa" }{ "_id" : ObjectId("4c720239f24118cb2f59218e"), "title" : "bbb" }{ "_id" : ObjectId("4c72023cf24118cb2f59218f"), "title" : "ccc" }>

Everything works.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.