環(huán)境說明
創(chuàng)新互聯-專業(yè)網站定制、快速模板網站建設、高性價比習水網站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式習水網站制作公司更省心,省錢,快速模板網站建設找我們,業(yè)務覆蓋習水地區(qū)。費用合理售后完善,10余年實體公司更值得信賴。IP | Role |
10.240.216.151 | master |
10.240.216.152 | slave(standby master) |
10.240.216.153 | slave |
mongodb下載與安裝
在10.240.216.151(主庫)上
# wgethttp://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(備庫)上
# wgethttp://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 } }
在主庫上創(chuàng)建庫和表以及添加一條數據
> 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.如果不能找到研發(fā)修改代碼指向新的主庫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 } }
針對以上操作復雜的過程,我們作如下優(yōu)化
在主庫10.240.216.151上執(zhí)行
# /usr/local/mongodb/bin/mongod--fork --port 4000 --dbpath /data/db/geomaster/ --logpath/data/log/geomaster.log --logappend --master
在從庫(備主)10.240.216.152上執(zhí)行
# /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上執(zhí)行
#/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" }
上面的結果說明,備用從庫是即可生效的,需要保證的是我們的寫是確保從主庫上寫入的。
另外有需要云服務器可以了解下創(chuàng)新互聯cdcxhl.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
名稱欄目:mongodb主從復制安裝文檔-創(chuàng)新互聯
本文地址:http://www.rwnh.cn/article38/pcjpp.html
成都網站建設公司_創(chuàng)新互聯,為您提供外貿建站、App設計、小程序開發(fā)、定制網站、響應式網站、網站維護
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯