91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MongoDB之分片

發布時間:2020-08-09 12:56:15 來源:ITPUB博客 閱讀:147 作者:stonebox1122 欄目:關系型數據庫
1、環境
操作系統信息:
IP 操作系統 MongoDB
10.163.91.15 RHLE6.5_x64 mongodb-linux-x86_64-rhel62-3.4.7.tgz
10.163.91.16 RHLE6.5_x64 mongodb-linux-x86_64-rhel62-3.4.7.tgz
10.163.91.17 RHLE6.5_x64 mongodb-linux-x86_64-rhel62-3.4.7.tgz


服務器規劃:
10.163.97.15 10.163.97.16 10.163.97.17 端口
mongos mongos mongos 20000
config server config server config server 21000
shard server1 主節點 shard server1 副節點 shard server1 仲裁 27001
shard server2 仲裁 shard server2 主節點 shard server2 副節點 27002
shard server3 副節點 shard server3 仲裁 shard server3 主節點 27003


從上表可以看到有四個組件:mongos、config server、shard、replica set。
mongos:數據庫集群請求的入口,所有的請求都通過mongos進行協調,不需要在應用程序添加一個路由選擇器,mongos自己就是一個請求分發中心,它負責把對應的數據請求轉發到對應的shard服務器上。在生產環境通常有多mongos作為請求的入口,防止其中一個掛掉所有的mongodb請求都沒有辦法操作。
config server:顧名思義為配置服務器,存儲所有數據庫元信息(路由、分片)的配置。mongos本身沒有物理存儲分片服務器和數據路由信息,只是緩存在內存里,配置服務器則實際存儲這些數據。mongos第一次啟動或者關掉重啟就會從 config server 加載配置信息,以后如果配置服務器信息變化會通知到所有的 mongos 更新自己的狀態,這樣 mongos 就能繼續準確路由。在生產環境通常有多個 config server 配置服務器(必須配置為1個或者3個),因為它存儲了分片路由的元數據,防止數據丟失!
shard:分片(sharding)是指將數據庫拆分,將其分散在不同的機器上的過程。將數據分散到不同的機器上,不需要功能強大的服務器就可以存儲更多的數據和處理更大的負載。基本思想就是將集合切成小塊,這些塊分散到若干片里,每個片只負責總數據的一部分,最后通過一個均衡器來對各個分片進行均衡(數據遷移)。
replica set:中文翻譯副本集,其實就是shard的備份,防止shard掛掉之后數據丟失。復制提供了數據的冗余備份,并在多個服務器上存儲數據副本,提高了數據的可用性, 并可以保證數據的安全性。
仲裁者(Arbiter):是復制集中的一個MongoDB實例,它并不保存數據。仲裁節點使用最小的資源,不能將Arbiter部署在同一個數據集節點中,可以部署在其他應用服務器或者監視服務器中,也可部署在單獨的虛擬機中。為了確保復制集中有奇數的投票成員(包括primary),需要添加仲裁節點做為投票,否則primary不能運行時不會自動切換primary。
簡單了解之后,可以這樣總結一下,應用請求mongos來操作mongodb的增刪改查,配置服務器存儲數據庫元信息,并且和mongos做同步,數據最終存入在shard(分片)上,為了防止數據丟失同步在副本集中存儲了一份,仲裁在數據存儲到分片的時候決定存儲到哪個節點。
總共規劃了mongos 3個, config server 3個,數據分3片 shard server 3個,每個shard 有一個副本一個仲裁也就是 3 * 2 = 6 個,總共需要部署15個實例。這些實例可以部署在獨立機器也可以部署在一臺機器,我們這里測試資源有限,只準備了 3臺機器,在同一臺機器只要端口不同就可以了。


2、安裝mongodb
分別在3臺機器上面安裝mongodb
[root@D2-POMS15 ~]# tar -xvzf mongodb-linux-x86_64-rhel62-3.4.7.tgz -C /usr/local/
[root@D2-POMS15 ~]# mv /usr/local/mongodb-linux-x86_64-rhel62-3.4.7/ /usr/local/mongodb

配置環境變量
[root@D2-POMS15 ~]# vim .bash_profile
export PATH=$PATH:/usr/local/mongodb/bin/
[root@D2-POMS15 ~]# source .bash_profile


分別在每臺機器建立conf、mongos、config、shard1、shard2、shard3六個目錄,因為mongos不存儲數據,只需要建立日志文件目錄即可。
[root@D2-POMS15 ~]# mkdir -p /usr/local/mongodb/conf
[root@D2-POMS15 ~]# mkdir -p /usr/local/mongodb/mongos/log
[root@D2-POMS15 ~]# mkdir -p /usr/local/mongodb/config/data
[root@D2-POMS15 ~]# mkdir -p /usr/local/mongodb/config/log
[root@D2-POMS15 ~]# mkdir -p /usr/local/mongodb/shard1/data
[root@D2-POMS15 ~]# mkdir -p /usr/local/mongodb/shard1/log
[root@D2-POMS15 ~]# mkdir -p /usr/local/mongodb/shard2/data
[root@D2-POMS15 ~]# mkdir -p /usr/local/mongodb/shard2/log
[root@D2-POMS15 ~]# mkdir -p /usr/local/mongodb/shard3/data
[root@D2-POMS15 ~]# mkdir -p /usr/local/mongodb/shard3/log


3、config server配置服務器
mongodb3.4以后要求配置服務器也創建副本集,不然集群搭建不成功。
在三臺服務器上面添加配置文件:
[root@D2-POMS15 ~]# vi /usr/local/mongodb/conf/config.conf
## 配置文件內容
pidfilepath = /usr/local/mongodb/config/log/configsrv.pid
dbpath = /usr/local/mongodb/config/data
logpath = /usr/local/mongodb/config/log/congigsrv.log
logappend = true

bind_ip = 0.0.0.0
port = 21000
fork = true

#declare this is a config db of a cluster;
configsvr = true

#副本集名稱
replSet=configs

#設置最大連接數
maxConns=20000

分別啟動三臺服務器的config server
[root@D2-POMS15 ~]# mongod -f /usr/local/mongodb/conf/config.conf
about to fork child process, waiting until server is ready for connections.
forked process: 15368
child process started successfully, parent exiting

登錄任意一臺配置服務器,初始化配置副本集
[root@D2-POMS15 ~]# mongo --port 21000
> config = {
...    _id : "configs",
...     members : [
...         {_id : 0, host : "10.163.97.15:21000" },
...         {_id : 1, host : "10.163.97.16:21000" },
...         {_id : 2, host : "10.163.97.17:21000" }
...     ]
... }
{
        "_id" : "configs",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "10.163.97.15:21000"
                },
                {
                        "_id" : 1,
                        "host" : "10.163.97.16:21000"
                },
                {
                        "_id" : 2,
                        "host" : "10.163.97.17:21000"
                }
        ]
}
> rs.initiate(config)
{ "ok" : 1 }

其中,"_id" : "configs"應與配置文件中配置的replSet一致,"members" 中的 "host" 為三個節點的 ip 和 port。

4、配置分片副本集(三臺機器)
設置第一個分片副本集
添加配置文件:
[root@D2-POMS15 ~]# vi /usr/local/mongodb/conf/shard1.conf
#配置文件內容
pidfilepath = /usr/local/mongodb/shard1/log/shard1.pid
dbpath = /usr/local/mongodb/shard1/data
logpath = /usr/local/mongodb/shard1/log/shard1.log
logappend = true

bind_ip = 0.0.0.0
port = 27001
fork = true

#打開web監控
httpinterface=true
rest=true

#副本集名稱
replSet=shard1

#declare this is a shard db of a cluster;
shardsvr = true

#設置最大連接數
maxConns=20000

啟動三臺服務器的shard1 server
[root@D2-POMS15 ~]# mongod -f /usr/local/mongodb/conf/shard1.conf
about to fork child process, waiting until server is ready for connections.
forked process: 15497
child process started successfully, parent exiting

登陸一臺服務器(不要在仲裁節點),初始化副本集
[root@D2-POMS15 ~]# mongo --port 27001
#使用admin數據庫
> use admin
switched to db admin
#定義副本集配置,第三個節點的 "arbiterOnly":true 代表其為仲裁節點。
> config = {
...    _id : "shard1",
...     members : [
...         {_id : 0, host : "10.163.97.15:27001" },
...         {_id : 1, host : "10.163.97.16:27001" },
...         {_id : 2, host : "10.163.97.17:27001" , arbiterOnly: true }
...     ]
... }
{
        "_id" : "shard1",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "10.163.97.15:27001"
                },
                {
                        "_id" : 1,
                        "host" : "10.163.97.16:27001"
                },
                {
                        "_id" : 2,
                        "host" : "10.163.97.17:27001",
                        "arbiterOnly" : true
                }
        ]
}
#初始化副本集配置
> rs.initiate(config);
{ "ok" : 1 }


設置第二個分片副本集
添加配置文件:
[root@D2-POMS15 ~]# vi /usr/local/mongodb/conf/shard2.conf
#配置文件內容
pidfilepath = /usr/local/mongodb/shard2/log/shard2.pid
dbpath = /usr/local/mongodb/shard2/data
logpath = /usr/local/mongodb/shard2/log/shard2.log
logappend = true

bind_ip = 0.0.0.0
port = 27002
fork = true

#打開web監控
httpinterface=true
rest=true

#副本集名稱
replSet=shard2

#declare this is a shard db of a cluster;
shardsvr = true

#設置最大連接數
maxConns=20000

啟動三臺服務器的shard2 server:
[root@D2-POMS15 ~]# mongod -f /usr/local/mongodb/conf/shard2.conf
about to fork child process, waiting until server is ready for connections.
forked process: 15622
child process started successfully, parent exiting

登陸一臺服務器(不要在仲裁節點),初始化副本集
[root@D2-POMS15 ~]# mongo --port 27002
> use admin
switched to db admin
> config = {
...    _id : "shard2",
...     members : [
...         {_id : 0, host : "10.163.97.15:27002"  , arbiterOnly: true },
...         {_id : 1, host : "10.163.97.16:27002" },
...         {_id : 2, host : "10.163.97.17:27002" }
...     ]
... }
{
        "_id" : "shard2",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "10.163.97.15:27002",
                        "arbiterOnly" : true
                },
                {
                        "_id" : 1,
                        "host" : "10.163.97.16:27002"
                },
                {
                        "_id" : 2,
                        "host" : "10.163.97.17:27002"
                }
        ]
}
> rs.initiate(config);
{ "ok" : 1 }

設置第三個分片副本集
添加配置文件:
[root@D2-POMS15 ~]# vi /usr/local/mongodb/conf/shard3.conf
#配置文件內容
pidfilepath = /usr/local/mongodb/shard3/log/shard3.pid
dbpath = /usr/local/mongodb/shard3/data
logpath = /usr/local/mongodb/shard3/log/shard3.log
logappend = true

bind_ip = 0.0.0.0
port = 27003
fork = true

#打開web監控
httpinterface=true
rest=true

#副本集名稱
replSet=shard3

#declare this is a shard db of a cluster;
shardsvr = true

#設置最大連接數
maxConns=20000

啟動三臺服務器的shard3 server
[root@D2-POMS15 ~]# mongod -f /usr/local/mongodb/conf/shard3.conf
about to fork child process, waiting until server is ready for connections.
forked process: 15742
child process started successfully, parent exiting

登陸一臺服務器(不要在仲裁節點),初始化副本集
> use admin
switched to db admin
> config = {
...    _id : "shard3",
...     members : [
...         {_id : 0, host : "10.163.97.15:27003" },
...         {_id : 1, host : "10.163.97.16:27003" , arbiterOnly: true},
...         {_id : 2, host : "10.163.97.17:27003" }
...     ]
... }
{
        "_id" : "shard3",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "10.163.97.15:27003"
                },
                {
                        "_id" : 1,
                        "host" : "10.163.97.16:27003",
                        "arbiterOnly" : true
                },
                {
                        "_id" : 2,
                        "host" : "10.163.97.17:27003"
                }
        ]
}
> rs.initiate(config);
{ "ok" : 1 }

可以看到目前已經啟動了配置服務器和分片服務器。
[root@D2-POMS15 ~]# ps -ef | grep mongo | grep -v grep
root     15368     1  0 15:52 ?        00:00:07 mongod -f /usr/local/mongodb/conf/config.conf
root     15497     1  0 16:00 ?        00:00:04 mongod -f /usr/local/mongodb/conf/shard1.conf
root     15622     1  0 16:06 ?        00:00:02 mongod -f /usr/local/mongodb/conf/shard2.conf
root     15742     1  0 16:21 ?        00:00:00 mongod -f /usr/local/mongodb/conf/shard3.conf


5、配置路由服務器 mongos
在三臺服務器上面添加配置文件:
[root@D2-POMS15 ~]# vi /usr/local/mongodb/conf/mongos.conf
#內容
pidfilepath = /usr/local/mongodb/mongos/log/mongos.pid
logpath = /usr/local/mongodb/mongos/log/mongos.log
logappend = true

bind_ip = 0.0.0.0
port = 20000
fork = true

#監聽的配置服務器,只能有1個或者3個 configs為配置服務器的副本集名字
configdb = configs/10.163.97.15:21000,10.163.97.16:21000,10.163.97.17:21000

#設置最大連接數
maxConns=20000

啟動三臺服務器的mongos server
[root@D2-POMS15 ~]# mongos -f /usr/local/mongodb/conf/mongos.conf
about to fork child process, waiting until server is ready for connections.
forked process: 20563
child process started successfully, parent exiting

[root@D2-POMS15 ~]# mongo --port 20000
mongos> db.stats()
{
        "raw" : {
                "shard1/10.163.97.15:27001,10.163.97.16:27001" : {
                        "db" : "admin",
                        "collections" : 1,
                        "views" : 0,
                        "objects" : 3,
                        "avgObjSize" : 146.66666666666666,
                        "dataSize" : 440,
                        "storageSize" : 36864,
                        "numExtents" : 0,
                        "indexes" : 2,
                        "indexSize" : 65536,
                        "ok" : 1,
                        "$gleStats" : {
                                "lastOpTime" : Timestamp(0, 0),
                                "electionId" : ObjectId("7fffffff0000000000000001")
                        }
                },
                "shard2/10.163.97.16:27002,10.163.97.17:27002" : {
                        "db" : "admin",
                        "collections" : 1,
                        "views" : 0,
                        "objects" : 2,
                        "avgObjSize" : 114,
                        "dataSize" : 228,
                        "storageSize" : 16384,
                        "numExtents" : 0,
                        "indexes" : 2,
                        "indexSize" : 32768,
                        "ok" : 1,
                        "$gleStats" : {
                                "lastOpTime" : Timestamp(0, 0),
                                "electionId" : ObjectId("7fffffff0000000000000001")
                        }
                },
                "shard3/10.163.97.15:27003,10.163.97.17:27003" : {
                        "db" : "admin",
                        "collections" : 1,
                        "views" : 0,
                        "objects" : 2,
                        "avgObjSize" : 114,
                        "dataSize" : 228,
                        "storageSize" : 16384,
                        "numExtents" : 0,
                        "indexes" : 2,
                        "indexSize" : 32768,
                        "ok" : 1,
                        "$gleStats" : {
                                "lastOpTime" : Timestamp(0, 0),
                                "electionId" : ObjectId("7fffffff0000000000000002")
                        }
                }
        },
        "objects" : 7,
        "avgObjSize" : 127.71428571428571,
        "dataSize" : 896,
        "storageSize" : 69632,
        "numExtents" : 0,
        "indexes" : 6,
        "indexSize" : 131072,
        "fileSize" : 0,
        "extentFreeList" : {
                "num" : 0,
                "totalSize" : 0
        },
        "ok" : 1
}


6、啟用分片
目前搭建了mongodb配置服務器、路由服務器,各個分片服務器,不過應用程序連接到mongos路由服務器并不能使用分片機制,還需要在路由服務器里設置分片配置,讓分片生效。
登陸任意一臺mongos
[root@D2-POMS15 ~]# mongo --port 20000
#使用admin數據庫
mongos> use admin
switched to db admin
#串聯路由服務器與分配副本集
mongos> sh.addShard("shard1/10.163.97.15:27001,10.163.97.16:27001,10.163.97.17:27001")
{ "shardAdded" : "shard1", "ok" : 1 }
mongos> sh.addShard("shard2/10.163.97.15:27002,10.163.97.16:27002,10.163.97.17:27002")
{ "shardAdded" : "shard2", "ok" : 1 }
mongos> sh.addShard("shard3/10.163.97.15:27003,10.163.97.16:27003,10.163.97.17:27003")
{ "shardAdded" : "shard3", "ok" : 1 }
#查看集群狀態
mongos> sh.status()
--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("599d34bf612249caec3fc9fe")
}
  shards:
        {  "_id" : "shard1",  "host" : "shard1/10.163.97.15:27001,10.163.97.16:27001",  "state" : 1 }
        {  "_id" : "shard2",  "host" : "shard2/10.163.97.16:27002,10.163.97.17:27002",  "state" : 1 }
        {  "_id" : "shard3",  "host" : "shard3/10.163.97.15:27003,10.163.97.17:27003",  "state" : 1 }
  active mongoses:
        "3.4.7" : 1
 autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
                Balancer lock taken at Wed Aug 23 2017 15:54:40 GMT+0800 (CST) by ConfigServer:Balancer
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours:
                No recent migrations
  databases:


7、測試
目前配置服務、路由服務、分片服務、副本集服務都已經串聯起來了,但我們的目的是希望插入數據,數據能夠自動分片。連接在mongos上,準備讓指定的數據庫、指定的集合分片生效。
[root@D2-POMS15 ~]# mongo --port 20000
mongos> use admin
switched to db admin
#指定testdb分片生效
mongos> db.runCommand({enablesharding :"testdb"});
{ "ok" : 1 }
#指定數據庫里需要分片的集合和片鍵
mongos> db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } )
{ "collectionsharded" : "testdb.table1", "ok" : 1 }
mongos> sh.status()
--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("599d34bf612249caec3fc9fe")
}
  shards:
        {  "_id" : "shard1",  "host" : "shard1/10.163.97.15:27001,10.163.97.16:27001",  "state" : 1 }
        {  "_id" : "shard2",  "host" : "shard2/10.163.97.16:27002,10.163.97.17:27002",  "state" : 1 }
        {  "_id" : "shard3",  "host" : "shard3/10.163.97.15:27003,10.163.97.17:27003",  "state" : 1 }
  active mongoses:
        "3.4.7" : 1
 autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
                Balancer lock taken at Wed Aug 23 2017 15:54:40 GMT+0800 (CST) by ConfigServer:Balancer
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours:
                4 : Success
  databases:
        {  "_id" : "testdb",  "primary" : "shard1",  "partitioned" : true }
                testdb.table1
                        shard key: { "id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                shard1  1
                        { "id" : { "$minKey" : 1 } } -->> { "id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0)

以上就是設置testdb的 table1 表需要分片,根據 id 自動分片到 shard1 ,shard2,shard3 上面去。要這樣設置是因為不是所有mongodb 的數據庫和表 都需要分片!

測試分片:
#連接mongos服務器
[root@D2-POMS15 ~]# mongo --port 20000
#使用testdb
mongos> use testdb
switched to db testdb
#插入測試數據
mongos> for(var i=1;i<=100000;i++){db.table1.insert({id:i,"test1":"testval1"})}
WriteResult({ "nInserted" : 1 })
#查看分片情況
mongos> sh.status()
--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("599d34bf612249caec3fc9fe")
}
  shards:
        {  "_id" : "shard1",  "host" : "shard1/10.163.97.15:27001,10.163.97.16:27001",  "state" : 1 }
        {  "_id" : "shard2",  "host" : "shard2/10.163.97.16:27002,10.163.97.17:27002",  "state" : 1 }
        {  "_id" : "shard3",  "host" : "shard3/10.163.97.15:27003,10.163.97.17:27003",  "state" : 1 }
  active mongoses:
        "3.4.7" : 1
 autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
                Balancer lock taken at Wed Aug 23 2017 15:54:40 GMT+0800 (CST) by ConfigServer:Balancer
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours:
                6 : Success
  databases:
        {  "_id" : "testdb",  "primary" : "shard1",  "partitioned" : true }
                testdb.table1
                        shard key: { "id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                shard1  1
                                shard2  1
                                shard3  1
                        { "id" : { "$minKey" : 1 } } -->> { "id" : 2 } on : shard2 Timestamp(2, 0)
                        { "id" : 2 } -->> { "id" : 20 } on : shard3 Timestamp(3, 0)
                        { "id" : 20 } -->> { "id" : { "$maxKey" : 1 } } on : shard1 Timestamp(3, 1)

可以看到這里分片很不均衡,原因是默認的chunkSize為64M,這里的數據量沒有達到64M,可以修改一下chunkSize的大小,方便測試:
[root@D2-POMS15 ~]# mongo --port 20000
mongos> use config
switched to db config
mongos> db.settings.save( { _id:"chunksize", value: 1 } )
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : "chunksize" })
mongos> db.settings.find();
{ "_id" : "balancer", "stopped" : false, "mode" : "full" }
{ "_id" : "chunksize", "value" : 1 }

修改后重新來測試:
mongos> use testdb
switched to db testdb
mongos> db.table1.drop();
true
mongos> use admin
switched to db admin
mongos> db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } )
{ "collectionsharded" : "testdb.table1", "ok" : 1 }
mongos> use testdb
switched to db testdb
mongos> for(var i=1;i<=100000;i++){db.table1.insert({id:i,"test1":"testval1"})}
WriteResult({ "nInserted" : 1 })
mongos> sh.status()
--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("599d34bf612249caec3fc9fe")
}
  shards:
        {  "_id" : "shard1",  "host" : "shard1/10.163.97.15:27001,10.163.97.16:27001",  "state" : 1 }
        {  "_id" : "shard2",  "host" : "shard2/10.163.97.16:27002,10.163.97.17:27002",  "state" : 1 }
        {  "_id" : "shard3",  "host" : "shard3/10.163.97.15:27003,10.163.97.17:27003",  "state" : 1 }
  active mongoses:
        "3.4.7" : 1
 autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
                Balancer lock taken at Wed Aug 23 2017 15:54:40 GMT+0800 (CST) by ConfigServer:Balancer
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours:
                14 : Success
  databases:
        {  "_id" : "testdb",  "primary" : "shard1",  "partitioned" : true }
                testdb.table1
                        shard key: { "id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                shard1  4
                                shard2  4
                                shard3  3
                        { "id" : { "$minKey" : 1 } } -->> { "id" : 2 } on : shard2 Timestamp(5, 1)
                        { "id" : 2 } -->> { "id" : 20 } on : shard3 Timestamp(6, 1)
                        { "id" : 20 } -->> { "id" : 9729 } on : shard1 Timestamp(7, 1)
                        { "id" : 9729 } -->> { "id" : 21643 } on : shard1 Timestamp(3, 3)
                        { "id" : 21643 } -->> { "id" : 31352 } on : shard2 Timestamp(4, 2)
                        { "id" : 31352 } -->> { "id" : 43021 } on : shard2 Timestamp(4, 3)
                        { "id" : 43021 } -->> { "id" : 52730 } on : shard3 Timestamp(5, 2)
                        { "id" : 52730 } -->> { "id" : 64695 } on : shard3 Timestamp(5, 3)
                        { "id" : 64695 } -->> { "id" : 74404 } on : shard1 Timestamp(6, 2)
                        { "id" : 74404 } -->> { "id" : 87088 } on : shard1 Timestamp(6, 3)
                        { "id" : 87088 } -->> { "id" : { "$maxKey" : 1 } } on : shard2 Timestamp(7, 0)

mongos> db.table1.stats()
{
        "sharded" : true,
        "capped" : false,
        "ns" : "testdb.table1",
        "count" : 100000,
        "size" : 5400000,
        "storageSize" : 1736704,
        "totalIndexSize" : 2191360,
        "indexSizes" : {
                "_id_" : 946176,
                "id_1" : 1245184
        },
        "avgObjSize" : 54,
        "nindexes" : 2,
        "nchunks" : 11,
        "shards" : {
                "shard1" : {
                        "ns" : "testdb.table1",
                        "size" : 2376864,
                        "count" : 44016,
                        "avgObjSize" : 54,
                        "storageSize" : 753664,
                        "capped" : false,
                        "nindexes" : 2,
                        "totalIndexSize" : 933888,
                        "indexSizes" : {
                                "_id_" : 405504,
                                "id_1" : 528384
                        },
                        "ok" : 1
                },
                "shard2" : {
                        "ns" : "testdb.table1",
                        "size" : 1851768,
                        "count" : 34292,
                        "avgObjSize" : 54,
                        "storageSize" : 606208,
                        "capped" : false,
                        "nindexes" : 2,
                        "totalIndexSize" : 774144,
                        "indexSizes" : {
                                "_id_" : 335872,
                                "id_1" : 438272
                        },
                        "ok" : 1
                },
                "shard3" : {
                        "ns" : "testdb.table1",
                        "size" : 1171368,
                        "count" : 21692,
                        "avgObjSize" : 54,
                        "storageSize" : 376832,
                        "capped" : false,
                        "nindexes" : 2,
                        "totalIndexSize" : 483328,
                        "indexSizes" : {
                                "_id_" : 204800,
                                "id_1" : 278528
                        },
                        "ok" : 1
                }
        },
        "ok" : 1
}

可以看到現在的數據分布就均勻多了。


8、后期運維
啟動關閉
mongodb的啟動順序是,先啟動配置服務器,在啟動分片,最后啟動mongos.
mongod -f /usr/local/mongodb/conf/config.conf
mongod -f /usr/local/mongodb/conf/shard1.conf
mongod -f /usr/local/mongodb/conf/shard2.conf
mongod -f /usr/local/mongodb/conf/shard3.conf
mongod -f /usr/local/mongodb/conf/mongos.conf
關閉時,直接killall殺掉所有進程
killall mongod
killall mongos

參考:
https://docs.mongodb.com/manual/sharding/
http://www.lanceyan.com/tech/arch/mongodb_shard1.html
http://www.cnblogs.com/ityouknow/p/7344005.html
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亳州市| 广宗县| 黄冈市| 梅河口市| 陈巴尔虎旗| 龙井市| 红原县| 西宁市| 太保市| 宁南县| 启东市| 武强县| 惠水县| 鄢陵县| 晋城| 南宁市| 张家川| 伽师县| 盐边县| 苏州市| 珠海市| 隆尧县| 沂水县| 绿春县| 镇沅| 环江| 黑河市| 梁山县| 绥宁县| 恩平市| 永新县| 惠安县| 河池市| 菏泽市| 和平区| 堆龙德庆县| 工布江达县| 河南省| 体育| 福清市| 白银市|