MongoDB分片羣集的部署(用心描述,詳細易懂)!!

概念:

MongoDB分片是使用多個服務器存儲數據的方法,以支持巨大的數據存儲和對數據進行存儲前端

優點:

一、減小了每一個分片需啊喲處理的請求數,羣集能夠提升本身的存儲容量和吞吐量linux

二、減小了每一個分片存儲的數據mongodb

三個主要組件:

shard:分片服務器,用於存儲實際的數據塊,由多臺服務器組成一個複製集承擔,防止主機單點故障

config server:配置服務器,存儲整個分片羣集的配置信息,包括塊信息

routers:前端路由,客戶端由此進入,讓整個羣集看上去像單一數據庫

如何部署MongoDB分片羣集!!!數據庫

一、安裝MongoDB3.2版本包和openssl-devel包
[root@localhost ~]# yum install openssl-devel -y [root@localhost ~]# mkdir /abc [root@localhost ~]# mount.cifs //192.168.200.1/orc /abc Password for root@//192.168.200.1/orc: [root@localhost ~]# cd /abc [root@localhost abc]# ls mongodb-linux-x86_64-3.2.1.tgz //MongoDB 3.2版本 [root@localhost abc]# tar zxvf mongodb-linux-x86_64-3.2.1.tgz -C /opt [root@localhost abc]# cd /opt [root@localhost opt]# ls mongodb-linux-x86_64-3.2.1 rh [root@localhost opt]# mv mongodb-linux-x86_64-3.2.1/ /usr/local/mongodb [root@localhost opt]# cd /usr/local/bin/ [root@localhost bin]# ln -s /usr/local/mongodb/bin/mongo /usr/bin/mongo //創建軟鏈接 [root@localhost bin]# ln -s /usr/local/mongodb/bin/mongod /usr/bin/mongod [root@localhost bin]# mkdir -p /data/mongodb/mongodb{1,2,3,4} [root@localhost bin]# mkdir /data/mongodb/logs [root@localhost bin]# touch /data/mongodb/logs/mongodb{1,2,3,4}.log [root@localhost bin]# chmod -R 777 /data/mongodb/logs/*.log [root@localhost bin]# ulimit -n 25000 //打開文件數量 [root@localhost bin]# ulimit -u 25000 //進程併發數
二、部署配置服務器

編輯mongodb1.conf配置文件,端口爲37017,設置configsvr=true,啓動配置服務器vim

[root@localhost bin]# vim mongodb1.con port=37017 dbpath=/data/mongodb/mongodb1 logpath=/data/mongodb/logs/mongodb1.log logappend=true fork=true maxConns=5000 storageEngine=mmapv1 configsvr=true //開啓配置服務器
三、某節點內存不足,從其它節點分配內存
[root@localhost bin]# sysctl -w vm.zone_reclaim_mode=0 vm.zone_reclaim_mode = 0 [root@localhost bin]# echo never > /sys/kernel/mm/transparent_hugepage/enabled [root@localhost bin]# echo never > /sys/kernel/mm/transparent_hugepage/defrag [root@localhost bin]# mongod -f mongodb1.conf about to fork child process, waiting until server is ready for connections. forked process: 6895 child process started successfully, parent exiting
四、部署分片服務器

編輯mongodb2.conf配置文件,端口爲47017,設置shardsvr=true;編輯mongodb3.conf配置文件,端口爲47018,啓動兩個分片服務器服務器

[root@localhost bin]# cp -p mongodb1.conf mongodb2.conf [root@localhost bin]# vim mongodb2.conf port=47017 //修改端口 dbpath=/data/mongodb/mongodb2 logpath=/data/mongodb/logs/mongodb2.log logappend=true fork=true maxConns=5000 storageEngine=mmapv1 shardsvr=true //開啓分片服務器 [root@localhost bin]# cp -p mongodb2.conf mongodb3.conf [root@localhost bin]# vim mongodb3.conf port=47018 dbpath=/data/mongodb/mongodb3 logpath=/data/mongodb/logs/mongodb3.log logappend=true fork=true maxConns=5000 storageEngine=mmapv1 shardsvr=true [root@localhost bin]# mongod -f mongodb2.conf about to fork child process, waiting until server is ready for connections. forked process: 7080 child process started successfully, parent exiting [root@localhost bin]# mongod -f mongodb3.conf about to fork child process, waiting until server is ready for connections. forked process: 7107 child process started successfully, parent exiting [root@localhost bin]# netstat -antp | grep mongod tcp 0 0 0.0.0.0:37017 0.0.0.0:* LISTEN god tcp 0 0 0.0.0.0:47017 0.0.0.0:* LISTEN god tcp 0 0 0.0.0.0:47018 0.0.0.0:* LISTEN god 
五、啓動路由服務器

./mongos --help命令能夠查看路由相關參數信息。chunkSize爲數據塊大小,默認爲200M,這裏將值設爲1併發

[root@localhost bin]# ./mongos --port 27017 --fork --logpath=/usr/local/mongodb/bin/route.log --configdb 192.168.200.142:37017 --chunkSize 1
六、啓用分片服務器
mongos> sh.addShard("192.168.200.142:47017") { "shardAdded" : "shard0000", "ok" : 1 } mongos> sh.addShard("192.168.200.142:47018") { "shardAdded" : "shard0001", "ok" : 1 } mongos> sh.status() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("5b4f68599dcf397cc4e7c598") } shards: //分片服務器開啓 { "_id" : "shard0000", "host" : "192.168.200.142:47017" } { "_id" : "shard0001", "host" : "192.168.200.142:47018" } active mongoses: "3.2.1" : 1 balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: No recent migratio
七、分片功能
[root@localhost bin]# mongo mongos> use school switched to db school mongos> for(var i=1;i<=10000;i++)db.users.insert({"id":i,"name":"jack"+i}) WriteResult({ "nInserted" : 1 }) mongos> show dbs config 0.031GB school 0.078GB mongos> use school switched to db school mongos> show collections system.indexes users mongos> db.users.find().limit(10) //查看頭十行內容 { "_id" : ObjectId("5b4f6a24ef33d588c0dbc4ae"), "id" : 1, "name" : "jack1" } { "_id" : ObjectId("5b4f6a24ef33d588c0dbc4af"), "id" : 2, "name" : "jack2" } { "_id" : ObjectId("5b4f6a24ef33d588c0dbc4b0"), "id" : 3, "name" : "jack3" } { "_id" : ObjectId("5b4f6a24ef33d588c0dbc4b1"), "id" : 4, "name" : "jack4" } { "_id" : ObjectId("5b4f6a24ef33d588c0dbc4b2"), "id" : 5, "name" : "jack5" } { "_id" : ObjectId("5b4f6a24ef33d588c0dbc4b3"), "id" : 6, "name" : "jack6" } { "_id" : ObjectId("5b4f6a24ef33d588c0dbc4b4"), "id" : 7, "name" : "jack7" } { "_id" : ObjectId("5b4f6a24ef33d588c0dbc4b5"), "id" : 8, "name" : "jack8" } { "_id" : ObjectId("5b4f6a24ef33d588c0dbc4b6"), "id" : 9, "name" : "jack9" } { "_id" : ObjectId("5b4f6a24ef33d588c0dbc4b7"), "id" : 10, "name" : "jack10" } mongos> sh.status() //查看數據庫分片信息 --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("5b4f68599dcf397cc4e7c598") } shards: { "_id" : "shard0000", "host" : "192.168.200.142:47017" } { "_id" : "shard0001", "host" : "192.168.200.142:47018" } active mongoses: "3.2.1" : 1 balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: No recent migrations databases: { "_id" : "school", "primary" : "shard0000", "partitioned" : false } mongos> sh.enableSharding("school") //啓用數據庫分片 { "ok" : 1 } mongos> sh.status() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("5b4f68599dcf397cc4e7c598") } shards: { "_id" : "shard0000", "host" : "192.168.200.142:47017" } { "_id" : "shard0001", "host" : "192.168.200.142:47018" } active mongoses: "3.2.1" : 1 balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: No recent migrations databases: { "_id" : "school", "primary" : "shard0000", "partitioned" : true } mongos> db.users.createIndex({"id":1}) //對users表建立索引 { "raw" : { "192.168.200.142:47017" : { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } }, "ok" : 1 } mongos> sh.shardCollection("school.users",{"id":1})//表分片 { "collectionsharded" : "school.users", "ok" : 1 } mongos> sh.status() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("5b4f68599dcf397cc4e7c598") } shards: { "_id" : "shard0000", "host" : "192.168.200.142:47017" } { "_id" : "shard0001", "host" : "192.168.200.142:47018" } active mongoses: "3.2.1" : 1 balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: No recent migrations databases: { "_id" : "school", "primary" : "shard0000", "partitioned" : true } school.users shard key: { "id" : 1 } unique: false balancing: true chunks: shard0000 3 { "id" : { "$minKey" : 1 } } -->> { "id" : 4682 } on : shard0000 Timestamp(1, 0) { "id" : 4682 } -->> { "id" : 9364 } on : shard0000 Timestamp(1, 1) { "id" : 9364 } -->> { "id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 2) //分片成功
相關文章
相關標籤/搜索