MongoDB 實現主從複製

來源:互聯網
上載者:User

MongoDB文檔資料庫提供了主從複製模式,其實MongoDB的主從複製配置很簡單,就是啟動MongoDB服務進程的時候 分別指定 --master ,--slave,一個是以主模式啟動,另一個屬於從模式啟動,當主庫更新時,資料就會被被複製到從資料庫中。
此次測試僅在單台伺服器上開啟2deamon來類比2台伺服器進行主從複製:
主庫:./mongod --master --dbpath=/opt/monogdata/data --port=60000
從庫:./mongod --slave --dbpath=/opt/monogdata/slavedata/ --port=60010 --source=127.0.0.1:60000

--主庫:
[monogdb@yangDB bin]$ ./mongo --port 60000
MongoDB shell version: 1.8.3-rc0
connecting to: 127.0.0.1:60000/test
> show dbs
admin   (empty)
local   1.203125GB
test    0.203125GB
> use test
switched to db test
> show collections --查看主庫中的對象。
system.indexes  --system.indexes用來存放索引的表
test--一個測試表
--查看test表中的內容
>db.test.find(); --此操作=select * from test;
{ "_id" : ObjectId("4e3fe5d8e138232e61000000"), "id" : 1, "val" : "hello monogdb" }
--向test表中插入資料
> db.test.insert({id:2,val:"yangql is learing monogdb master slave!"});
> db.test.find();                                                     
{ "_id" : ObjectId("4e3fe5d8e138232e61000000"), "id" : 1, "val" : "hello monogdb" }
{ "_id" : ObjectId("4e45291c018d1a0d765a9788"), "id" : 2, "val" : "yangql is learing monogdb master slave!" }
                 
--備庫
[monogdb@yangDB bin]$ ./mongo --port=60010
MongoDB shell version: 1.8.3-rc0
connecting to: 127.0.0.1:60010/test
> db
test
> db.printSlaveReplicationInfo(); ---顯示主庫的資訊。
source:   127.0.0.1:60000
         syncedTo: Fri Aug 12 2011 21:19:42 GMT+0800 (CST)
                 = 7secs ago (0hrs)
> show collections
system.indexes
test  --主庫的資料已經被複製到從庫了
> db.test.find();
{ "_id" : ObjectId("4e3fe5d8e138232e61000000"), "id" : 1, "val" : "hello monogdb" }
--主庫更新資料後查看從庫。
> db.test.find();
{ "_id" : ObjectId("4e3fe5d8e138232e61000000"), "id" : 1, "val" : "hello monogdb" }
{ "_id" : ObjectId("4e45291c018d1a0d765a9788"), "id" : 2, "val" : "yangql is learing monogdb master slave!" }
--測試在從庫更新資料,但是不成功!
> db.test.insert({id:2,val:"yangql is writing things to  slave,but it can`t do this!"});                                   
not master
> db.test.find(); 
{ "_id" : ObjectId("4e3fe5d8e138232e61000000"), "id" : 1, "val" : "hello monogdb" }
{ "_id" : ObjectId("4e45291c018d1a0d765a9788"), "id" : 2, "val" : "yangql is learing monogdb master slave!" }
monogdb的主從複製是不能達到主庫當機以後,從庫自動升級到主庫的,而Replica Pairs借用arbiter可以完成主從故障遷移 。

  • 1
  • 2
  • 下一頁

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.