构建一个Mongo分片集群

时间:2022-05-08 01:35:26

构建一个Mongo分片集群

本文中的表述,只代表本人的学习理解,表述可能不准确,不到位,更准确的信息请到官网或其他地方查询。

规划

分片集群需要涉及到这些内容:mongos、configserver、shard、replica set。为了集群的高可用,在本文中每一部分将配置多个节点(副本)。

Mongos:是集群请求的入口,负责把对应的数据请求转发到shard分片中。

ConfigServer: 存储所有数据库元信息(路由、分片)的配置。Mongos中的数据需要从这里加载。

Shard:分片,将1个collection的内容分到不同机器上,这样就解决了硬件性能和容量问题,分而治之的思想。

ReplicaSet:副本集,为了保证每个分片的高可用,每个分片配置了多个副本集。

 

机器节点规划

计划安装3个mongos,3个configServer,3个分片(shard),每个分片上有2个副本集,1个仲裁。所以共需要15个节点。而实际有三台机器:vm603,vm601,vmzsh。所以在每台机器上启多个端口,以不同的端口代表不同的节点。

每个实际机器上有5个节点,这5个节点分别是:

1.      mongos

2.      config Server

3.      分片Shard1的副本集/仲裁

4.      分片Shard2的副本集/仲裁

5.      分片Shard3的副本集/仲裁

端口规划:

mongos为 20000, config server 为 21000, shard1为 22001 , shard2为22002, shard3为22003.

 

具体如表格所示:

 

Vm603 (114.174)

Vm601 (114.175)

Vmzsh (114.173)

 

Mongos:20000

Mongos:20000

Mongos:20000

 

Config Server:21000

Config Server:21000

Config Server:21000

 

Shard1-replica set:22001

仲裁:22001

Shard1-replica set:22001

 

Shard2-replica set:22002

仲裁:22002

Shard2-replica set:22002

 

Shard3-replica set:22003

仲裁:22003

Shard3-replica set:22003

 

 

 

 

注:114.17x,表未IP:192.168.114.17x

 

 

节点配置

需要的目录

以下操作需要每台机器都要操作

 

mkdir -p /usr/local/mongodb/mongos/log

mkdir -p /usr/local/mongodb/config/data

mkdir -p /usr/local/mongodb/config/log

mkdir -p /usr/local/mongodb/shard1/data

mkdir -p /usr/local/mongodb/shard1/log

mkdir -p /usr/local/mongodb/shard2/data

mkdir -p /usr/local/mongodb/shard2/log

mkdir -p /usr/local/mongodb/shard3/data

mkdir -p /usr/local/mongodb/shard3/log

 

启动配置服务器

以下操作需要每台机器都要操作

[root@vm603 bin]# ./mongod--port 21000 --configsvr --dbpath /usr/local/mongodb/config/data --logpath/usr/local/mongodb/config/log/config.log –fork

启动mongos服务器

需要上面3个config server都配置完成。

以下操作需要每台机器都要操作

./mongos--configdb 192.168.114.173:21000,192.168.114.174:21000,192.168.114.175:21000--port 20000 --logpath /usr/local/mongodb/mongos/log/mongos.log  --fork

启动mongo的每个分片的副本集:

以下操作需要每台机器都要操作

 

./mongod--shardsvr --replSet shard1 --port 22001 --dbpath/usr/local/mongodb/shard1/data --logpath/usr/local/mongodb/shard1/log/shard1.log --fork --nojournal --oplogSize 10

 

./mongod--shardsvr --replSet shard2 --port 22002 --dbpath/usr/local/mongodb/shard2/data --logpath /usr/local/mongodb/shard2/log/shard2.log--fork --nojournal --oplogSize 10    

 

./mongod--shardsvr --replSet shard3 --port 22003 --dbpath/usr/local/mongodb/shard3/data --logpath/usr/local/mongodb/shard3/log/shard3.log --fork --nojournal --oplogSize 10

设置分片的副本集配置

 

每个分片均需要连接到任意1个副本集

第1个分片

[root@vmzsh bin]# ./mongo localhost:22001

 

> use admin

switched to db admin

>config={_id:"shard1",members:[

... {_id:0,host:"192.168.114.173:22001"},

...{_id:1,host:"192.168.114.174:22001"},

...{_id:2,host:"192.168.114.175:22001",arbiterOnly:true}

... ]}

 

> rs.initiate(config);

 

 

第2个分片:

 

[root@vmzsh bin]# ./mongo localhost:22002

MongoDB shell version: 2.2.2

connecting to: localhost:22002/test

> use admin

switched to db admin

>config={_id:"shard2",members:[

...{_id:0,host:"192.168.114.173:22002"},

...{_id:1,host:"192.168.114.174:22002"},

...{_id:2,host:"192.168.114.175:22002",arbiterOnly:true}

... ]}

> rs.initiate(config);

 

 

第3个分片:

[root@vmzsh bin]# ./mongo localhost:22003

MongoDB shell version: 2.2.2

connecting to: localhost:22003/test

> use admin

switched to db admin

>config={_id:"shard3",members:[

...{_id:0,host:"192.168.114.173:22003"},

...{_id:1,host:"192.168.114.174:22003"},

...{_id:2,host:"192.168.114.175:22003",arbiterOnly:true}

... ]}

 

> rs.initiate(config);

 

配置分片集群

 

连接到其中任意一个mongos节点,并切换到admin数据库做以下配置

 

[root@vm603 bin]# ./mongolocalhost:20000/admin

MongoDB shell version: 2.2.2

connecting to: localhost:20000/admin

mongos>

db.runCommand({addshard:"shard1/192.168.114.173:22001,192.168.114.174:22001,192.168.114.175:22001",name:"shard1",maxSize:20480});

{ "shardAdded" :"shard1", "ok" : 1 }

mongos> db.runCommand({addshard:"shard2/192.168.114.173:22002,192.168.114.174:22002,192.168.114.175:22002",name:"shard2",maxSize:20480});

{ "shardAdded" :"shard2", "ok" : 1 }

mongos> db.runCommand({addshard:"shard3/192.168.114.173:22003,192.168.114.174:22003,192.168.114.175:22003",name:"shard3",maxSize:20480});

{ "shardAdded" :"shard3", "ok" : 1 }

 

说明:

如里shard是单台服务器,用 db.runCommand( { addshard : “[: ]”} )

如果shard是副本集,

db.runCommand( {addshard : “replicaSetName/[:port][,serverhostname2[:port],…]”}); 

查看分片集群配置信息

mongos> db.runCommand({listshards:1});

{
"shards" : [
{
"_id" :"shard1",
"host" :"shard1/192.168.114.173:22001,192.168.114.174:22001",
"maxSize" :NumberLong(20480)
},
{
"_id" :"shard2",
"host" :"shard2/192.168.114.173:22002,192.168.114.174:22002",
"maxSize" :NumberLong(20480)
},
{
"_id" :"shard3",
"host" :"shard3/192.168.114.173:22003,192.168.114.174:22003",
"maxSize" : NumberLong(20480)
}
],
"ok" : 1
}


 

创建数据库并激活分片:

[root@vm601 bin]# ./mongolocalhost:20000/admin

mongos> db

admin

mongos> use nosql

switched to db nosql

mongos> for (var i=0;i<100;i++)db.tan.insert({uid:i,uname:'nosql'+i});

mongos> use admin

switched to db admin

 

激活数据库分片功能

mongos>db.runCommand({enablesharding:"nosql"});

{ "ok" : 1 }

mongos>

集合分片

mongos> use nosql

switched to db nosql

mongos>db.tan.ensureIndex({"uid":1});

mongos> use admin

switched to db admin

mongos> db.runCommand({shardcollection:"nosql.tan",key:{uid:1}});

{ "collectionsharded" :"nosql.tan", "ok" : 1 }

mongos>

 

mongos> use nosql

switched to db nosql

mongos> db.tan.stats();
{
"sharded" : true,
"ns" : "nosql.tan",
"count" : 100,
"numExtents" : 2,
"size" : 5600,
"storageSize" : 20480,
"totalIndexSize" : 16352,
"indexSizes" : {
"_id_" : 8176,
"uid_1" : 8176
},
"avgObjSize" : 56,
"nindexes" : 2,
"nchunks" : 1,
"shards" : {
"shard1" : {
"ns" :"nosql.tan",
"count" :100,
"size" :5600,
"avgObjSize": 56,
"storageSize": 20480,
"numExtents": 2,
"nindexes" :2,
"lastExtentSize" : 16384,
"paddingFactor" : 1,
"systemFlags": 1,
"userFlags" :0,
"totalIndexSize" : 16352,
"indexSizes": {
"_id_" : 8176,
"uid_1" : 8176
},
"ok" : 1
}
},
"ok" : 1
}


 

该集合只存放在1个分片上。再增加些数据,然后查看:

mongos> for(vari=101;i<1000000;i++)db.tan.insert({uid:i,uname:'nosql'+i});

mongos> db.tan.stats();
{
"sharded" : true,
"ns" : "nosql.tan",
"count" : 999999,
"numExtents" : 24,
"size" : 59960000,
"storageSize" : 104407040,
"totalIndexSize" : 60714976,
"indexSizes" : {
"_id_" : 32638592,
"uid_1" : 28076384
},
"avgObjSize" : 59.96005996005996,
"nindexes" : 2,
"nchunks" : 6,
"shards" : {
"shard1" : {
"ns" :"nosql.tan",
"count" :384748,
"size" :23084912,
"avgObjSize": 60.00008317132253,
"storageSize" :43171840,
"numExtents": 10,
"nindexes" :2,
"lastExtentSize" : 13942784,
"paddingFactor" : 1,
"systemFlags": 1,
"userFlags" :0,
"totalIndexSize" : 23448768,
"indexSizes": {
"_id_" : 12607392,
"uid_1" : 10841376
},
"ok" : 1
},
"shard2" : {
"ns" :"nosql.tan",
"count" :15598,
"size" :895884,
"avgObjSize": 57.43582510578279,
"storageSize": 2793472,
"numExtents": 5,
"nindexes" :2,
"lastExtentSize" : 2097152,
"paddingFactor" : 1,
"systemFlags": 1,
"userFlags" :0,
"totalIndexSize" : 1046528,
"indexSizes": {
"_id_" : 555968,
"uid_1" : 490560
},
"ok" : 1
},
"shard3" : {
"ns" :"nosql.tan",
"count" :599653,
"size" :35979204,
"avgObjSize": 60.00004002314672,
"storageSize": 58441728,
"numExtents": 9,
"nindexes" :2,
"lastExtentSize": 20643840,
"paddingFactor" : 1,
"systemFlags": 1,
"userFlags" :0,
"totalIndexSize" : 36219680,
"indexSizes": {
"_id_" : 19475232,
"uid_1" : 16744448
},
"ok" : 1
}
},
"ok" : 1
}


Mongo的分片集群配置完成。

JAVA*问该集群里,可同时连接到上面3个mongos上.

 

 

本文部分参考:http://www.lanceyan.com/tech/arch/mongodb_shard1.html