高數據量和吞吐量的數據庫應用會對單機的性能形成較大壓力,大的查詢量會將單機的CPU耗盡,大的數據量對單機的存儲壓力較大,最終會耗盡系統的內存而將壓力轉移到磁盤IO上。
MongoDB分片是使用多個服務器存儲數據的方法,以支持巨大的數據存儲和對數據進行操做。分片技術能夠知足MongoDB數據量大量增加的需求,當一臺MongoDB服務器不足以存儲海量數據或者不足以提供可接受的讀寫吞吐量時,咱們就能夠經過在多臺服務器上分割數據,使得數據庫系統能存儲和處理更多的數據。前端
分片爲應對高吞吐量與大數據量提夠了方法python
systemctl stop firewalld.service setenforce 0
IP:172.16.10.26 | IP:172.16.10.27 | IP:172.16.10.29 |
---|---|---|
mongos(27017) | mongos(27017) | mongos(27017) |
config(30000) | config(30000) | config(30000) |
shard1主節點(40001) | shard1副節點(40001) | shard1仲裁節點(40001) |
shard2仲裁節點(40002) | shard2主節點(40002) | shard2副節點(40002) |
shard1副節點(40003) | shard1仲裁節點(40003) | shard1主節點(40003) |
羣集部署的搭建思路,利用三臺服務器,分別安裝mongodb數據庫,每臺服務器建立五個實例(mongos、configs、shard一、shard二、shard3)。三臺不一樣的服務器上的相同名稱的實例,建立爲一個複製集,分別包括主節點,副節點,仲裁節點。mongos不需建立複製集,config不需指定主副節點及仲裁節點,可是要建立複製集。三臺服務器的操做步驟略有差異,可是大可能是都是重複操做,步驟徹底一致。linux
yum install openssl-devel -y tar zxf mongodb-linux-x86_64-rhel70-4.0.0.tgz -C /usr/local mv /usr/local/mongodb-linux-x86_64-rhel70-4.0.0 /usr/local/mongodb //解壓即完成安裝
路由服務器不存儲數據,所以就不須要建立數據存儲目錄,只需建立config、shard一、shaed二、shard3便可,日誌文件建立完成以後還須要給予權限。mongodb
mkdir -p /data/mongodb/logs/ mkdir /etc/mongodb/ mkdir /data/mongodb/config/ mkdir /data/mongodb/shard{1,2,3} touch /data/mongodb/logs/shard{1,2,3}.log touch /data/mongodb/logs/mongos.log touch /data/mongodb/logs/config.log chmod 777 /data/mongodb/logs/*.log
useradd -M -u 8000 -s /sbin/nologin mongo chown -R mongo.mongo /usr/local/mongodb chown -R mongo.mongo /data/mongodb
echo "PATH=/usr/local/mongodb/bin:$PATH" >> /etc/profile source /etc/profile
ulimit -n 25000 ulimit -u 25000 sysctl -w vm.zone_reclaim_mode=0 echo never > /sys/kernel/mm/transparent_hugepage/enabled echo never > /sys/kernel/mm/transparent_hugepage/defrag //*注意*這些優化都是臨時的,重啓失效
#vim /etc/mongodb/config.conf pidfilepath = /data/mongodb//logs/config.pid //pid文件位置 dbpath = /data/mongodb/config/ //數據文件存放位置 logpath = /data/mongodb//logs/config.log //日誌文件位置 logappend = true bind_ip = 0.0.0.0 //監聽地址 port = 30000 //端口號 fork = true replSet=configs //複製集名稱 configsvr = true maxConns=20000 //最大鏈接數
scp /etc/mongodb/config.conf root@172.16.10.27:/etc/mongodb/ scp /etc/mongodb/config.conf root@172.16.10.29:/etc/mongodb/
mongod -f /etc/mongodb/config.conf //三臺服務器操做一致
mongo --port 30000 //建議三臺服務器都進入數據庫,方便查看角色變動 config={_id:"configs",members:[{_id:0,host:"172.16.10.26:30000"},{_id:1,host:"172.16.10.27:30000"},{_id:2,host:"172.16.10.29:30000"}]} //建立複製集 rs.initiate(config) //初始化複製集
#vim /etc/mongodb/shard1.conf pidfilepath = /data/mongodb//logs/shard1.pid dbpath = /data/mongodb/shard1/ logpath = /data/mongodb//logs/shard1.log logappend = true journal = true quiet = true bind_ip = 0.0.0.0 port = 40001 fork = true replSet=shard1 shardsvr = true maxConns=20000
scp /etc/mongodb/shard1.conf root@172.16.10.27:/etc/mongodb/ scp /etc/mongodb/shard1.conf root@172.16.10.29:/etc/mongodb/
mongod -f /etc/mongodb/shard1.conf //三臺服務器操做一致
在shard分片服務器的建立中,須要注意的點是,不是在任一臺服務器上建立都能成功的,若是選擇在預先設置爲仲裁節點的服務器上建立複製集會報錯。以shard1分片服務器爲例,能夠在172.16.10.26和172.16.10.27服務器上建立複製集,在172.16.10.29上建立則會失敗,由於在複製集建立以前,172.16.10.29已經被設置爲仲裁節點。
數據庫
mongo --port 40001 //建議三臺服務器都進入數據庫,方便查看角色變動 use admin config={_id:"shard1",members:[{_id:0,host:"172.16.10.26:40001",priority:2},{_id:1,host:"172.16.10.27:40001",priority:1},{_id:2,host:"172.16.10.29:40001",arbiterOnly:true}]} rs.initiate(config)
#vim /etc/mongodb/shard2.conf pidfilepath = /data/mongodb//logs/shard2.pid dbpath = /data/mongodb/shard2/ logpath = /data/mongodb//logs/shard2.log logappend = true journal = true quiet = true bind_ip = 0.0.0.0 port = 40002 fork = true replSet=shard2 shardsvr = true maxConns=20000
scp /etc/mongodb/shard2.conf root@172.16.10.27:/etc/mongodb/ scp /etc/mongodb/shard2.conf root@172.16.10.29:/etc/mongodb/
mongod -f /etc/mongodb/shard2.conf //三臺服務器操做一致
mongo --port 40002 //建議三臺服務器都進入數據庫,方便查看角色變動 use admin config={_id:"shard2",members:[{_id:0,host:"172.16.10.26:40002",arbiterOnly:true},{_id:1,host:"172.16.10.27:40002",priority:2},{_id:2,host:"172.16.10.29:40002",priority:1}]} rs.initiate(config)
#vim /etc/mongodb/shard3.conf pidfilepath = /data/mongodb//logs/shard3.pid dbpath = /data/mongodb/shard3/ logpath = /data/mongodb//logs/shard3.log logappend = true journal = true quiet = true bind_ip = 0.0.0.0 port = 40003 fork = true replSet=shard3 shardsvr = true maxConns=20000
scp /etc/mongodb/shard3.conf root@172.16.10.27:/etc/mongodb/ scp /etc/mongodb/shard3.conf root@172.16.10.29:/etc/mongodb/
mongod -f /etc/mongodb/shard3.conf //三臺服務器操做一致
mongo --port 40003 //建議三臺服務器都進入數據庫,方便查看角色變動 use admin config={_id:"shard3",members:[{_id:0,host:"172.16.10.26:40003",priority:1},{_id:1,host:"172.16.10.27:40003",arbiterOnly:true},{_id:2,host:"172.16.10.29:40003",priority:2}]} rs.initiate(config);
pidfilepath = /data/mongodb/logs/mongos.pid logpath=/data/mongodb/logs/mongos.log logappend = true bind_ip = 0.0.0.0 port = 27017 fork = true configdb = configs/172.16.10.26:30000,172.16.10.27:30000,172.16.10.29:30000 maxConns=20000
scp /etc/mongodb/mongos.conf root@172.16.10.27:/etc/mongodb/ scp /etc/mongodb/mongos.conf root@172.16.10.29:/etc/mongodb/
mongos -f /etc/mongodb/mongos.conf //三臺服務器操做一致*注意*這裏是「mongos」而非「mongod」
mongo //由於默認端口便是27017,因此此處不接端口號 mongos> use admin mongos> sh.addShard("shard1/172.16.10.26:40001,172.16.10.27:40001,172.16.10.29:40001") mongos> sh.addShard("shard2/172.16.10.26:40002,172.16.10.27:40002,172.16.10.29:40002") mongos> sh.status() //查看羣集狀態 //此處先添加兩各分片服務器,還有一個,待會添加
mongos> use config switched to db config mongos> db.settings.save({"_id":"chunksize","value":1}) //設置塊大小爲1M是方便實驗,否則就須要插入海量數據 WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : "chunksize" })
mongos> use python switched to db python mongos> show collections mongos> for(i=1;i<=50000;i++){db.user.insert({"id":i,"name":"jack"+i})} //在python庫的user表中循環寫入五萬條數據 WriteResult({ "nInserted" : 1 })
mongos>sh.enableSharding("python") //數據庫分片就有針對性,能夠自定義須要分片的庫或者表,畢竟也不是全部數據都是須要分片操做的
建立索引的規則是不能一致性過高,要具備惟一性,例如序號,好比性別這一類重複性過高的就不適合作索引vim
mongos> db.user.createIndex({"id":1}) //以」id「爲索引
mongos> sh.shardCollection("python.user",{"id":1})
mongos> sh.status() --- Sharding Status --- ···省略內容 shards: { "_id" : "shard1", "host" : "shard1/172.16.10.26:40001,172.16.10.27:40001", "state" : 1 } { "_id" : "shard2", "host" : "shard2/172.16.10.27:40002,172.16.10.29:40002", "state" : 1 } ···省略內容 chunks: shard1 3 shard2 3 { "id" : { "$minKey" : 1 } } -->> { "id" : 9893 } on : shard1 Timestamp(2, 0) { "id" : 9893 } -->> { "id" : 19786 } on : shard1 Timestamp(3, 0) { "id" : 19786 } -->> { "id" : 29679 } on : shard1 Timestamp(4, 0) { "id" : 29679 } -->> { "id" : 39572 } on : shard2 Timestamp(4, 1) { "id" : 39572 } -->> { "id" : 49465 } on : shard2 Timestamp(1, 4) { "id" : 49465 } -->> { "id" : { "$maxKey" : 1 } } on : shard2 Timestamp(1, 5)
mongos> use admin switched to db admin mongos> sh.addShard("172.16.10.26:40003,172.16.10.27:40003,172.16.10.29:40003") mongos> sh.status() --- Sharding Status --- ···省略內容 shards: { "_id" : "shard1", "host" : "shard1/172.16.10.26:40001,172.16.10.27:40001", "state" : 1 } { "_id" : "shard2", "host" : "shard2/172.16.10.27:40002,172.16.10.29:40002", "state" : 1 } { "_id" : "shard3", "host" : "shard3/172.16.10.26:40003,172.16.10.29:40003", "state" : 1 } ···省略內容 chunks: shard1 2 shard2 2 shard3 2 { "id" : { "$minKey" : 1 } } -->> { "id" : 9893 } on : shard3 Timestamp(6, 0) { "id" : 9893 } -->> { "id" : 19786 } on : shard1 Timestamp(6, 1) { "id" : 19786 } -->> { "id" : 29679 } on : shard1 Timestamp(4, 0) { "id" : 29679 } -->> { "id" : 39572 } on : shard3 Timestamp(5, 0) { "id" : 39572 } -->> { "id" : 49465 } on : shard2 Timestamp(5, 1) { "id" : 49465 } -->> { "id" : { "$maxKey" : 1 } } on : shard2 Timestamp(1, 5)
服務器又對數據進行從新分片,當你再次移除一個分片服務器,此時又會對數據再次進行分片處理,MongoDB對數據的處理很是靈活服務器