標籤:作業系統 linux mongodb centos 6.4
一、環境
作業系統:
# uname -r2.6.32-358.el6.x86_64# cat /etc/issueCentOS release 6.4 (Final)Kernel \r on an \m
主:192.168.137.148
從:192.168.137.154
二、主從同步系統時間
#ntpdate ntp.api.bz && hwclock -w --systohc
三、安裝
#wget #tar xf mongodb-linux-x86_64-enterprise-rhel62-2.6.4.tgz#mv mongodb-linux-x86_64-enterprise-rhel62-2.6.4 /usr/local/mongodb#[ ! -e /data/mongodb/data ] && mkdir -p /data/mongodb/data || echo ‘/data/mongodb/data is exist!‘#[ ! -e /data/mongodb/mongodb.log ] && touch /data/mongodb/mongodb.log || echo ‘/data/mongodb/mongodb.log is exist!‘
四、啟動服務
mongodb master:
#/usr/local/mongodb/bin/mongod --master --bind_ip=192.168.137.148 --dbpath=/data/mongodb/data --logpath=/data/mongodb/mongodb.log --logappend --port=27017 --fork
mongodb slave:
#/usr/local/mongodb/bin/mongod --slave --bind_ip=192.168.137.154 --source 192.168.137.148:27017 --dbpath=/data/mongodb/data --logpath=/data/mongodb/mongodb.log --logappend --port=27017 --fork
五、測試
master:
# /usr/local/mongodb/bin/mongo --host 192.168.137.148MongoDB shell version: 2.6.4connecting to: 192.168.0.23:27017/testWelcome to the MongoDB shell.For interactive help, type "help".For more comprehensive documentation, seehttp://docs.mongodb.org/Questions? Try the support grouphttp://groups.google.com/group/mongodb-userServer has startup warnings: 2014-09-24T14:16:58.169+0800 [initandlisten] 2014-09-24T14:16:58.169+0800 [initandlisten] ** WARNING: You are running on a NUMA machine.2014-09-24T14:16:58.169+0800 [initandlisten] ** We suggest launching mongod like this to avoid performance problems:2014-09-24T14:16:58.169+0800 [initandlisten] ** numactl --interleave=all mongod [other options]2014-09-24T14:16:58.169+0800 [initandlisten] > show dbs; #查看資料庫local1.203125GBtest0.203125GB> use test1; #建立庫switched to db test1> db.foo.save({"id":123456,"name":‘zhangsan‘}) #插入資料> db.foo.find({"id":123456}) #查詢資料{ "_id" : ObjectId("542277078091089e1cbfecbc"), "id" : 123456, "name" : "zhangsan" }> show dbs;local1.203125GBtest0.203125GBtest10.203125GB
slave:
# /usr/local/mongodb/bin/mongo --host 192.168.137.148MongoDB shell version: 2.6.4connecting to: 192.168.0.23:27017/testWelcome to the MongoDB shell.For interactive help, type "help".For more comprehensive documentation, seehttp://docs.mongodb.org/Questions? Try the support grouphttp://groups.google.com/group/mongodb-userServer has startup warnings: 2014-09-24T14:16:58.169+0800 [initandlisten] 2014-09-24T14:16:58.169+0800 [initandlisten] ** WARNING: You are running on a NUMA machine.2014-09-24T14:16:58.169+0800 [initandlisten] ** We suggest launching mongod like this to avoid performance problems:2014-09-24T14:16:58.169+0800 [initandlisten] ** numactl --interleave=all mongod [other options]2014-09-24T14:16:58.169+0800 [initandlisten] > show dbs;local0.203125GBtest0.203125GBtest10.203125GB> use test1;switched to db test1> db.foo.find({"id":123456}){ "_id" : ObjectId("542277078091089e1cbfecbc"), "id" : 123456, "name" : "zhangsan" }
資料同步成功
六、常見操作
1)在從伺服器上查詢主伺服器的資訊
> use local;switched to db local> show collections;mesourcessystem.indexes> db.sources.find(){ "_id" : ObjectId("541d8f661037aa7d865374d0"), "host" : "192.168.137.148:27017", "source" : "main", "syncedTo" : { "t" : 1411545377000, "i" : 1 } }
2)在主伺服器上查詢同步狀態
> db.printReplicationInfo();configured oplog size: 944.1375732421875MBlog length start to end: 420339secs (116.76hrs)oplog first event time: Fri Sep 19 2014 19:15:28 GMT+0800 (CST)oplog last event time: Wed Sep 24 2014 16:01:07 GMT+0800 (CST)now: Wed Sep 24 2014 16:01:11 GMT+0800 (CST)
3)在master伺服器上查詢集合狀態
> db.printCollectionStats();foo{"ns" : "test1.foo","count" : 1,"size" : 56,"avgObjSize" : 56,"storageSize" : 4096,"numExtents" : 1,"nindexes" : 1,"lastExtentSize" : 4096,"paddingFactor" : 1,"flags" : 1,"totalIndexSize" : 8176,"indexSizes" : {"_id_" : 8176},"ok" : 1}---system.indexes{"ns" : "test1.system.indexes","count" : 1,"size" : 64,"avgObjSize" : 64,"storageSize" : 4096,"numExtents" : 1,"nindexes" : 0,"lastExtentSize" : 4096,"paddingFactor" : 1,"flags" : 0,"totalIndexSize" : 0,"indexSizes" : {},"ok" : 1}---
七、主從切換
1)停掉slave服務
#killall mongod
2)刪除local資料
#rm -rf /data/mongodb/data/local.*
3)把從伺服器以master身份啟動
#/usr/local/mongodb/bin/mongod --master --bind_ip=192.168.137.154 --dbpath=/data/mongodb/data --logpath=/data/mongodb/mongodb.log --logappend --port=27017 --fork
4)以web方式查看狀態650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/4A/1A/wKioL1QifmrQbd4rAAQNq6bo4Ag351.jpg" title="11.jpg" alt="wKioL1QifmrQbd4rAAQNq6bo4Ag351.jpg" />
本文出自 “技術小菜” 部落格,請務必保留此出處http://390892467.blog.51cto.com/2006821/1557761
Linux系統mongodb主從模式配置