Skip to content

MongoDB 分片(sharding)

🏷️ MongoDB

按值的范围进行横向分片,简称分片(sharding)

创建两个 mongo 服务器并分别启动;

bash
cd /mongodb/
mkdir ./mongo4 ./mongo5

cd /mongodb/
./bin/mongod --shardsvr --dbpath ./mongo4 --port 27014

cd /mongodb/
./bin/mongod --shardsvr --dbpath ./mongo5 --port 27015

创建一个 config(配置)服务器;

bash
cd /mongodb/
mkdir ./mongoconfig
./bin/mongod --configsvr --dbpath ./mongoconfig --port 27016

创建一个 mongos 服务器,它是对客户的一个单点入口;

bash
cd /mongodb/
./bin/mongos --configdb localhost:27016 --chunkSize 1 --port 27020

连接 mongos 服务器,进入 admin 数据库并配置分片;

bash
[liujj@localhost ~]$ cd /mongodb/
[liujj@localhost mongodb]$ ./bin/mongo localhost:27020/admin
MongoDB shell version: 3.2.4
connecting to: localhost:27020/admin
mongos> db.runCommand( { addshard : "localhost:27014" } )
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> db.runCommand( { addshard : "localhost:27015" } )
{ "shardAdded" : "shard0001", "ok" : 1 }
mongos> db.runCommand( { enablesharding : "test" } )
{ "ok" : 1 }
mongos> db.runCommand( { shardcollection : "test.cities", key : {name : 1} } )
{ "collectionsharded" : "test.cities", "ok" : 1 }
mongos>

从 json 文件导入 cities 数据;

bash
[liujj@localhost ~]$ cd /mongodb/
[liujj@localhost mongodb]$ ./bin/mongoimport -h localhost:27020 --db test --collection cities --type json /mongodb/scripts/mongo_cities1000.json
2016-04-07T07:07:29.561-0700    connected to: localhost:27020
2016-04-07T07:07:32.420-0700    [#######.................] test.cities  3.7 MB/12.3 MB (30.1%)
2016-04-07T07:07:35.419-0700    [############............] test.cities  6.3 MB/12.3 MB (50.7%)
2016-04-07T07:07:38.423-0700    [################........] test.cities  8.6 MB/12.3 MB (69.7%)
2016-04-07T07:07:41.434-0700    [####################....] test.cities  10.7 MB/12.3 MB (86.4%)
2016-04-07T07:07:44.105-0700    [########################] test.cities  12.3 MB/12.3 MB (100.0%)
2016-04-07T07:07:44.105-0700    imported 99838 documents
[liujj@localhost mongodb]$

分别连接两个分片服务器并统计 name 的第一个字符,看看分别存储了哪些数据及数据件数;

点击查看统计结果
bash
localhost:27014
[liujj@localhost mongodb]$ ./bin/mongo localhost:27014
MongoDB shell version: 3.2.4
connecting to: localhost:27014/test
> db.cities.group({
...         initial: { names: {} },
...         reduce: function(city, output) { 
...                 output.names[city.name.substr(0,1)] = 1;
...         },
...         finalize: function (out) {
...                 var ary = ;
...                 for(var p in out.names) { ary.push(p); }
...                 out.names = ary;
...         }
... })
[
        {
                "names" : [
                        "S",
                        "P",
                        "O",
                        "l",
                        "U",
                        "R",
                        "M",
                        "`",
                        "T",
                        "Z",
                        "Y",
                        "Q",
                        "N",
                        "L",
                        "Ḩ",
                        "Ā",
                        "Ç",
                        "V",
                        "W",
                        "İ",
                        "X",
                        "Ş",
                        "Ə",
                        "Ž",
                        "Š",
                        "Č",
                        "Ć",
                        "É",
                        "Á",
                        "v",
                        "О",
                        "Ü",
                        "Í",
                        "Ú",
                        "Ř",
                        "Ö",
                        "‘",
                        "Ø",
                        "Å",
                        "e",
                        "Ţ",
                        "Ṭ",
                        "Ó",
                        "s",
                        "Ò",
                        "Ä",
                        "Œ",
                        "È",
                        "h",
                        "Ý",
                        "Μ",
                        "Ő",
                        "d",
                        "Ẕ",
                        "m",
                        "Ḥ",
                        "Ū",
                        "k",
                        "p",
                        "Ī",
                        "Þ",
                        "Ō",
                        "Ŭ",
                        "Ķ",
                        "Ж",
                        "З",
                        "В",
                        "Т",
                        "С",
                        "Ш",
                        "Р",
                        "П",
                        "Н",
                        "М",
                        "Л",
                        "К",
                        "Ј",
                        "И",
                        "Г",
                        "Д",
                        "Ч",
                        "Ц",
                        "Б",
                        "А",
                        "Ż",
                        "Ħ",
                        "f",
                        "Ñ",
                        "Ś",
                        "Ł",
                        "Î",
                        "b",
                        "Ð",
                        "Ḑ",
                        "Ľ",
                        "ك",
                        "g"
                ]
        }
]
> db.cities.count()
54150
localhost:27015
[liujj@localhost mongodb]$ ./bin/mongo localhost:27015
MongoDB shell version: 3.2.4
connecting to: localhost:27015/test
> db.cities.group({
...         initial: { names: {} },
...         reduce: function(city, output) { 
...                 output.names[city.name.substr(0,1)] = 1;
...         },
...         finalize: function (out) {
...                 var ary = ;
...                 for(var p in out.names) { ary.push(p); }
...                 out.names = ary;
...         }
... })
[
        {
                "names" : [
                        "‘",
                        "城",
                        "东",
                        "’",
                        "A",
                        "B",
                        "E",
                        "C",
                        "D",
                        "연",
                        "옥",
                        "진",
                        "'",
                        "F",
                        "H",
                        "G",
                        "K",
                        "J",
                        "I",
                        "L"
                ]
        }
]
> db.cities.count()
45688

可以看出数据差不多平均分布在两个服务器上,而且开头的字母都不相同。