建立主/從服務器
主服務器:132.129.31.213:10111(A)
從服務器:132.129.31.213:10112(B)
1 啓動Master數據庫服務器:
$./mongod –master -port=10111 -dbpath=/home/tsaip/mongodb/data/10111 -nohttpinterface & 注: 默認端口27017
2 啓動Slave數據庫服務器:5s同步一次
$./mongod –slave –source=132.129.31.213:10111 -port=10112 -dbpath=/home/tsaip/mongodb/data/10112 –slavedelay 5 -nohttpinterface &
3測試同步結果:
//登錄master數據庫服務器
$ ./mongo -host 132.129.31.213 -port 10111
MongoDB shell version: 1.8.2-rc2
connecting to: 132.129.31.213:10111/test
> use test;
switched to db test
> db.user.insert({_id:1,name:'samlee',age:80});
> db.user.find();
{ "_id" : 1, "name" : "samlee", "age" : 80 } >
//登錄slave數據庫服務器
$ ./mongo -host 132.129.31.213 -port 10112
MongoDB shell version: 1.8.2-rc2
connecting to: 132.129.31.213:10112/test
> use test;
switched to db test
> db.user.find(); { "_id" : 1, "name" : "samlee", "age" : 80 }
>
數據同步成功!!
Master
--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數據過時後自動重連
4. 切換Master/Slave角色
a) 假設已經具備主機A和從機B,此時想切換它們的角色,步驟如下:(假設A是健康的)
b) 用fsync命令暫停A上的寫操作,
c) 確定B是從機,關閉B上的服務
d) 清空B上的local.*文件
e) 用-master選項重啓B服務
f) 在B上執行一次寫操作,初始化oplog,獲得一個同步起始點
g) 關閉B服務,此時B已經有了信的local.*文件
h) 關閉A服務,並且用B上新的local.*文件來代替A上的local.*文件(拷貝之前,記得先壓縮,因爲文件可能很大)
i) 用-master選項重啓B服務
j) 在平時的slave選項上加一個-fastsync選項來重啓A服務
如果A不是健康的,但是硬件是健康的,那麼跳過上面的前兩步,並且用B上所有文件去替換A上的文件,重啓服務。