標籤:table tran 角色 環境 main 參數 大量 kernel var
概述
MongoDB分區是使用多個伺服器儲存資料的方法,以支援巨大的資料存放區和對資料進行操作。分區技術可以滿足MongoDB資料量大量增長的需求,當一台MongoDB伺服器不足以儲存海量資料或不足以提供可接受的讀寫輸送量時,我們就可以在多台伺服器上分割資料,使得資料庫系統能儲存和處理更多的資料。
MongoDB分區群集的組成
- Shard:分區伺服器,用於儲存實際的資料區塊,實際生產環境中一個shard server角色可由幾台伺服器組成一個Replica Set承擔,防止主機單點故障。
- Config Server:設定管理員,儲存了整個分區群集的配置資訊,其中包括chunk資訊。
- Routers:路由器,用戶端由此接入,且讓整個群集看上去像單一資料庫,前端應用可以透明使用。
部署MongoDB分區群集
實驗環境:
- 1台路由執行個體(連接埠27017)
- 1台配置執行個體(連接埠37017)
- 2台shard執行個體(連接埠47017,47018)
1.安裝MongoDB3.2
[[email protected] ~]# yum install openssl-devel -y[[email protected] tomcat]# tar zxvf mongodb-linux-x86_64-3.2.1.tgz -C /opt/
2.建立4個執行個體
[[email protected] opt]# mkdir -p /data/mongodb/mongodb{1,2,3,4}[[email protected] mongodb]# mkdir logs[[email protected] mongodb]# touch logs/mongodb{1,2,3,4}.log[[email protected] logs]# chmod -R 777 *.log[[email protected] logs]# ulimit -n 25000 //最大進程數//[[email protected] logs]# ulimit -u 25000 //最大檔案數//
3.部署設定管理員
[[email protected] bin]# vim mongodb1.confport=37017dbpath=/data/mongodb/mongodb1logpath=/data/mongodb/logs/mongodb1.loglogappend=truefork=truemaxConns=5000storageEngine=mmapv1configsvr=true~
1)某節點記憶體不足時,從其他節點分配記憶體
[[email protected] bin]# sysctl -w vm.zone_reclaim_mode=0 //核心參數為0的話,那麼系統會傾向於從其他節點分配記憶體//[[email protected] bin]# echo never > /sys/kernel/mm/transparent_hugepage/enabled[[email protected] bin]# echo never > /sys/kernel/mm/transparent_hugepage/defrag[[email protected] bin]# ln -s /usr/local/mongodb/bin/mongo /usr/bin/mongo[[email protected] bin]# ln -s /usr/local/mongodb/bin/mongod /usr/bin/mongod
2)啟動設定管理員
[[email protected] bin]# mongod -f mongodb1.conf
4.配置分區伺服器
[[email protected] bin]# vim mongodb2.conf port=47017dbpath=/data/mongodb/mongodb2logpath=/data/mongodb/logs/mongodb2.loglogappend=truefork=truemaxConns=5000storageEngine=mmapv1shardsvr=true[[email protected] bin]# vim mongodb3.conf port=47018dbpath=/data/mongodb/mongodb3logpath=/data/mongodb/logs/mongodb3.loglogappend=truefork=truemaxConns=5000storageEngine=mmapv1shardsvr=true
1)啟動mongodb2、mongodb3
[[email protected] bin]#mongod -f mongodb2.conf[[email protected] bin]#mongod -f mongodb3.conf
5.啟動路由伺服器
[[email protected] bin]# ./mongos --port 27017 --fork --logpath=/usr/local/mongodb/bin/route.log --configdb 192.168.126.141:37017 --chunkSize 1************顯示*******************************************2018-07-18T09:21:53.507+0800 W SHARDING [main] Running a sharded cluster with fewer than 3 config servers should only be done for testing purposes and is not recommended for production.about to fork child process, waiting until server is ready for connections.forked process: 3580child process started successfully, parent exiting
6.啟用分區伺服器
[[email protected] bin]# mongomongos> show dbsconfig 0.031GBmongos> sh.status() //#shards下為空白,沒有分區伺服器//mongos> sh.addShard("192.168.126.204:47017")mongos> sh.addShard("192.168.126.204:47018")mongos> sh.status()shards: { "_id" : "shard0000", "host" : "192.168.126.141:47017" } { "_id" : "shard0001", "host" : "192.168.126.141:47018" }
1)建立Kgc資料庫,建立users集合并且插入100000條資料
mongos> use kgcmongos> db.createCollection(‘users‘)mongos> for(var i=1;i<=100000;i++)db.users.insert({"id":1,"name":"jack"+i})mongos> db.users.find(){ "_id" : ObjectId("5b4e9b580f25d0730817aea1"), "id" : 1, "name" : "jack1" }{ "_id" : ObjectId("5b4e9b580f25d0730817aea2"), "id" : 2, "name" : "jack1" }{ "_id" : ObjectId("5b4e9b580f25d0730817aea3"), "id" : 3, "name" : "jack1" }{ "_id" : ObjectId("5b4e9b580f25d0730817aea4"), "id" : 4, "name" : "jack1" }{ "_id" : ObjectId("5b4e9b590f25d0730817aea5"), "id" : 5, "name" : "jack1" }{ "_id" : ObjectId("5b4e9b590f25d0730817aea6"), "id" : 6, "name" : "jack1" }{ "_id" : ObjectId("5b4e9b590f25d0730817aea7"), "id" : 7, "name" : "jack1" }{ "_id" : ObjectId("5b4e9b590f25d0730817aea8"), "id" : 8, "name" : "jack1" }{ "_id" : ObjectId("5b4e9b590f25d0730817aea9"), "id" : 9, "name" : "jack1" }{ "_id" : ObjectId("5b4e9b590f25d0730817aeaa"), "id" : 10, "name" : "jack1" }..........//省略//
7.實現分區功能
[[email protected] bin]# ./mongoimport -d kgc -c users --file /opt/testdb.txtmongos> show dbsconfig 0.031GBkgc 0.078GBmongos> use kgcswitched to db kgcmongos> show tablessystem.indexesusersmongos> sh.status() //查看資料庫分區資訊// databases: { "_id" : "kgc", "primary" : "shard0000", "partitioned" : false } //資料庫尚未分區//
1)啟用資料庫分區
mongos> sh.enableSharding("kgc") //啟用資料庫分區//mongos> sh.status()databases: { "_id" : "kgc", "primary" : "shard0000", "partitioned" : true }mongos> db.users.createIndex({"id":1}) //對users表建立索引//mongos> sh.shardCollection("kgc.users",{"id":1}) //表分區//mongos> sh.status() shards: { "_id" : "shard0000", "host" : "192.168.126.141:47017" } { "_id" : "shard0001", "host" : "192.168.126.141:47018" }
在CentOS7上部署MongoDB分區群集