接MongoDB快速入門
本文主要介紹MongoDB Master/Slaver配置
- 首先創建Mongo Master/home/hrj/mongodb-linux-i686-static-1.6.5/bin/mongod --master --dbpath /home/hrj/mongodb_data --auth --maxConns 50 --port 6688
- 其次創建Mongo Slaver/home/hrj/mongodb-linux-i686-static-1.6.5/bin/mongod --slave --source myna5.sds.cnb.yahoo.com:6688 --auth --maxConns 50 --auth --port 6689 --fastsync --autoresync --dbpath /home/hrj/mongodb_slave_data
- 創建Master、Slaver帳號~/mongodb-linux-i686-static-1.6.5/bin/mongo 127.0.0.1:6689 ### 在Slaver上創建帳號
> use local
switched to db local
> db.addUser('hrj','xxx')
{
"_id" : ObjectId("4d6f6527013fcbcc74575c20"),
"user" : "hrj",
"readOnly" : false,
"pwd" : "b27edaa5a8858aa3d46b60698fce1359"
}
~/mongodb-linux-i686-static-1.6.5/bin/mongo 127.0.0.1:6688 ### 在Master上創建帳號
MongoDB shell version: 1.6.5
connecting to: 127.0.0.1:6688/test
>use local
switched to db local
> db.addUser('hrj','xxx')
{
"_id" : ObjectId("4d6f6527013fcbcc74575c20"),
"user" : "hrj",
"readOnly" : false,
"pwd" : "b27edaa5a8858aa3d46b60698fce1359"
}
>use test ###添加帳號認證
switched to db local
> db.auth('hrj','xxx') - 向Master加載數據,測試Slaver是否正常同步###Master
~/mongodb-linux-i686-static-1.6.5/bin/mongo 127.0.0.1:6688
MongoDB shell version: 1.6.5
connecting to: 127.0.0.1:6688/test
> db.foo.save({'mongodb':'hello world'}) ###導入數據
> db.foo.find() ###查詢數據
{ "_id" : ObjectId("4d6f72c6d807e8561b0f3db5"), "mongodb" : "hello world" }
###Slaver
~/mongodb-linux-i686-static-1.6.5/bin/mongo 127.0.0.1:6689
MongoDB shell version: 1.6.5
connecting to: 127.0.0.1:6689/test
> db.foo.find() ###查詢Slaver同步數據
{ "_id" : ObjectId("4d6f72c6d807e8561b0f3db5"), "mongodb" : "hello world" }
> db.foo.save({'mongodb':'hello world test 1'}) ###Slaver導入數據,提示"not master"
not master
至此Master/Slaver大致調試成功。在這裏大致提一下Master/Slaver特別參數:
--master master模式
--oplogSize arg size limit (in MB) for op log
Slave
--slave slave模式
--source arg source指定master位置
--only arg 單獨指定備份某一database
--slavedelay arg 指定與Master延遲時間(秒)
--autoresync 當Slave數據過時後自動重連
特別推薦:
很可能會出現Master服務器Down掉之後,需要用Slave服務器來頂替Master提供服務器的情況,這個時候就需要做如下操作:
- 停止Slave進程(mongod)
- 刪除Slave數據目錄中的local.*
- 以--master模式啓動Slave