您好,登錄后才能下訂單哦!
環境說明
IP | Role |
10.240.216.151 | master |
10.240.216.152 | slave(standby master) |
10.240.216.153 | slave |
mongodb下載與安裝
在10.240.216.151(主庫)上
# wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.0.4.tgz
(如果不能正常下載,可以直接從瀏覽器下載下來,然后傳到服務器上)
# tar -zxf mongodb-linux-x86_64-2.0.4.tgz-C /usr/local/
# mkdir -p /data/{db/geomaster/,log}
# touch /data/log/geomaster.log
# cd /usr/local/
# mv mongodb-linux-x86_64-2.0.4 mongodb
# /usr/local/mongodb/bin/mongod --fork--port 4000 --master --dbpath /data/db/geomaster/ --logpath /data/log/geomaster.log--logappend 啟動數據庫
# kill -2 `ps -ef |grep mongod |grep -vgrep |awk '{print $2}'` 關閉數據庫
在10.240.216.152 和 10.240.216.153(備庫)上
# wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.0.4.tgz
(如果不能正常下載,可以直接從瀏覽器下載下來,然后傳到服務器上)
# tar -zxf mongodb-linux-x86_64-2.0.4.tgz-C /usr/local/
# mkdir -p /data/{db/geoslave/,log}
# touch /data/log/geosalve.log
# cd /usr/local/
# mv mongodb-linux-x86_64-2.0.4 mongodb
# /usr/local/mongodb/bin/mongod --fork--port 4000 --slave --source 10.240.216.151:4000 --dbpath /data/db/geoslave/--logpath /data/log/geosalve.log --logappend --nojournal 啟動數據庫
# kill -2 `ps -ef |grep mongod |grep -vgrep |awk '{print $2}'` 關閉數據庫
驗證
在主庫上
# /usr/local/mongodb/bin/mongo127.0.0.1:4000
> db.runCommand ( {"isMaster": 1 } )
{ "ismaster" : true,"maxBsonObjectSize" : 16777216, "ok" : 1 }
在備庫上
# /usr/local/mongodb/bin/mongo127.0.0.1:4000
> db.runCommand ( {"isMaster": 1 } )
{
"ismaster" : 0,
"info" : "dead: data toostale halted replication",
"maxBsonObjectSize" :16777216,
"ok" : 1
}
> use local
switched to db local
> db.sources.find()
{ "_id" :ObjectId("55ebfa53ecf735439d6bafeb"), "host" :"10.240.216.151:4000", "source" : "main","syncedTo" : { "t" : 1441528400000, "i" : 1 } }
在主庫上創建庫和表以及添加一條數據
> use mydb
switched to db mydb
> db
mydb
>db.mydb.user.insert({"username":"zdh"});
> db.mydb.user.find();
{ "_id" :ObjectId("55ebfc2d92e237c3cb30ef9a"), "username" :"zdh" }
> show dbs;
local 6.2001953125GB
mydb 0.203125GB
在從庫上檢查數據同步情況
> show dbs;
local 0.203125GB
mydb 0.203125GB
> use mydb
switched to db mydb
> db.mydb.user.find()
{"_id" : ObjectId("55ebfc2d92e237c3cb30ef9a"),"username" : "zdh" }
以上輸出結果表示OK
對于上面這種master-slave結構有一個很重要的缺陷就是當主庫掛掉后,從庫要晉升為主庫時,需要手動做大量的工作才行,具體步驟有:
1. 停掉其中一臺slave,然后重新以master的身份啟動(晉升10.240.216.152為主庫)
# kill -2 `ps -ef |grep mongod |grep -vgrep |awk '{print $2}'`
# /usr/local/mongodb/bin/mongod --fork--port 4000 --master --dbpath /data/db/geoslave/ --logpath/data/log/geosalve.log –logappend
2. 如果不能找到研發修改代碼指向新的主庫IP,那么可以將原主庫關機并將其的IP切到新主庫服務器上(切IP場景,這種情況下slave端不需要重啟mongodb進程,此時原主庫將不能啟動,這是一個問題)
# ifconfig bond0:1 10.240.216.151 netmask255.255.255.0 up
# arping -I bond0 -c 3 -s 10.240.216.15110.240.216.254
3. 將從庫10.240.216.153指向新的主庫,需要先停掉mongodb,然后source指向新主庫啟動(在不切IP的情況下操作)
# kill -2 `ps -ef |grep mongod |grep -vgrep |awk '{print $2}'`
# rm -f /data/db/geoslave/mongod.lock /data/db/geoslave/local.*
# /usr/local/mongodb/bin/mongod --fork--port 4000 --slave --source 10.240.216.152:4000 --dbpath /data/db/geoslave/--logpath /data/log/geosalve.log --logappend –nojournal
# /usr/local/mongodb/bin/mongo127.0.0.1:4000
> use local
switched to db local
> db.sources.find()
{ "_id" :ObjectId("55ec04dadfd5d65f2f7fe404"), "host" :"10.240.216.152:4000", "source" : "main","syncedTo" : { "t" : 1441531234000, "i" : 1 } }
針對以上操作復雜的過程,我們作如下優化
在主庫10.240.216.151上執行
# /usr/local/mongodb/bin/mongod--fork --port 4000 --dbpath /data/db/geomaster/ --logpath/data/log/geomaster.log --logappend --master
在從庫(備主)10.240.216.152上執行
# /usr/local/mongodb/bin/mongod--fork --port 4000 --master --slave --dbpath /data/db/geoslave/ --logpath/data/log/geosalve.log –logappend
# /usr/local/mongodb/bin/mongo127.0.0.1:4000
> use local
switched to db local
> db.sources.find()
>db.sources.insert({"host":"10.240.216.151:4000"});
> db.sources.find()
{ "_id" :ObjectId("55ec120751fd14c6b2f23a18"), "host" :"10.240.216.151:4000" }
> db.sources.find() 隔30秒左右
{ "_id" :ObjectId("55ec120751fd14c6b2f23a18"), "host" :"10.240.216.151:4000", "source" : "main", "syncedTo": { "t" : 1441534596000, "i" : 1 } }
在從庫10.240.216.153上執行
#/usr/local/mongodb/bin/mongod --fork --port 4000 --slave --dbpath /data/db/geomaster/ --logpath/data/log/geosalve.log --logappend –nojournal
# /usr/local/mongodb/bin/mongo127.0.0.1:4000
> use local
switched to db local
> db.sources.find()
>db.sources.insert({"host":"10.240.216.151:4000"})
>db.sources.insert({"host":"10.240.216.152:4000"})
> db.sources.find()
{ "_id" :ObjectId("55ec127a1924006f4e8b2e97"), "host" : "10.240.216.151:4000","source" : "main", "syncedTo" : { "t" :1441534586000, "i" : 1 } }
{ "_id" :ObjectId("55ec127e1924006f4e8b2e98"), "host" :"10.240.216.152:4000", "source" : "main","syncedTo" : { "t" : 1441534593000, "i" : 1 } }
以上配置完成。就實現了當主庫10.240.216.151掛掉后,備用主庫10.240.216.152可以直接提供對外的服務,無需重啟mongodb進程,并且從庫無需作任何更改。
驗證
在主庫10.240.216.151上
> show dbs
local 6.2001953125GB
mydb 0.203125GB
> use mydb
switched to db mydb
>db.mydb.user.insert({"username":"zhongguo","age":"26"})
> db.mydb.user.find()
{ "_id" :ObjectId("55ec12e4bab480b5485dd72e"), "username" :"zhongguo", "age" : "26" }
>db.mydb.user.insert({"username":"zhongguo2","age":"30"})
在從庫(備主)10.240.216.152上
> use mydb
switched to db mydb
> db.mydb.user.find()
{ "_id" :ObjectId("55ec12e4bab480b5485dd72e"), "username" :"zhongguo", "age" : "26" }
> db.mydb.user.insert({"username":"finished","time":"2015-09-06"})
> db.mydb.user.find()
{ "_id" : ObjectId("55ec12e4bab480b5485dd72e"),"username" : "zhongguo", "age" : "26" }
{ "_id" : ObjectId("55ec133e51fd14c6b2f23a19"),"username" : "finished", "time" :"2015-09-06" }
> db.mydb.user.find()
{ "_id" :ObjectId("55ec12e4bab480b5485dd72e"), "username" :"zhongguo", "age" : "26" }
{ "_id" :ObjectId("55ec133e51fd14c6b2f23a19"), "username" :"finished", "time" : "2015-09-06" }
{ "_id" :ObjectId("55ec136abab480b5485dd72f"), "username" :"zhongguo2", "age" : "30" }
在從庫10.240.216.153上
> use mydb
switched to db mydb
> db.mydb.user.find()
{ "_id" :ObjectId("55ec12e4bab480b5485dd72e"), "username" :"zhongguo", "age" : "26" }
> db.mydb.user.find()
{ "_id" : ObjectId("55ec12e4bab480b5485dd72e"),"username" : "zhongguo", "age" : "26" }
{ "_id" : ObjectId("55ec133e51fd14c6b2f23a19"),"username" : "finished", "time" :"2015-09-06" }
> db.mydb.user.find()
{ "_id" : ObjectId("55ec12e4bab480b5485dd72e"),"username" : "zhongguo", "age" : "26" }
{ "_id" :ObjectId("55ec133e51fd14c6b2f23a19"), "username" :"finished", "time" : "2015-09-06" }
{ "_id" :ObjectId("55ec136abab480b5485dd72f"), "username" :"zhongguo2", "age" : "30" }
上面的結果說明,備用從庫是即可生效的,需要保證的是我們的寫是確保從主庫上寫入的。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。