Linux下Mongodb的分布式分片群集(sharding cluster)配置

这篇文章我是从淘宝上转载过来的, 但是经过了一些的修改: 基本上相同

http://www.taobaodba.com/html/525_525.html

Shardingcluster介绍

这是一种可以水平扩展的模式,在数据量很大时特给力,实际大规模应用一般会采用这种架构去构建monodb系统。

要构建一个 MongoDB Sharding Cluster,需要三种角色:

Shard Server: mongod 实例,用于存储实际的数据块,实际生产环境中一个shard server角色可由几台机器组个一个relica set承担,防止主机单点故障

Config Server: mongod 实例,存储了整个 Cluster Metadata,其中包括 chunk 信息。

Route Server: mongos 实例,前端路由,客户端由此接入,且让整个集群看上去像单一数据库,前端应用可以透明使用。

 

机器分配和配置

本例架构示例图:

1.   分别在3台机器运行一个mongod实例(称为mongod shard11,mongod shard12,mongod shard13)组织replica set1,作为cluster的shard1

2.   分别在3台机器运行一个mongod实例(称为mongod shard21,mongod shard22,mongod shard23)组织replica set2,作为cluster的shard2

3.   每台机器运行一个mongod实例,作为3个config server

4.   每台机器运行一个mongs进程,用于客户端连接

 

主机 

IP

端口信息

Server1

172.16.101.244

mongod shard11:27017
mongod shard21:27018
mongod config1:20000
mongs1:30000

Server2

172.16.101.242

mongod shard12:27017
mongod shard22:27018
mongod config2:20000
mongs2:30000

Server3

172.16.101.241

mongod shard13:27017
mongod shard23:27018
mongod config3:20000
mongs3:30000

软件准备

1. 创建用户
groupadd  mongodb
useradd  -g mongodb mongodb
passwd mongodb ([email protected])

2. 安装monodb软件

chown -R mongodb:mongodb /data/ -- 这里需要把安装文件的权限赋给mongodb
su – mongodb
tar zxvf mongodb-linux-x86_64-1.8.2.tar

3. 创建数据目录
根据本例sharding架构图所示,在各台sever上创建shard数据文件目录
Server1:
su – monodb
cd /usr/local/server/monodb
mkdir –p data/shard11
mkdir –p data/shard21

touch data/shard11.log

touch data/shard21.log
Server2:
su – monodb
cd /usr/local/server/monodb
mkdir data

mkdir -p data/shard12
mkdir -p data/shard22

touch data/shard12.log

touch data/shard22.log
Server3:
su – monodb
cd /usr/local/server/monodb
mkdir -p data/shard13
mkdir -p data/shard23

touch data/shard13.log

touch data/shard23.log

配置relica sets

1. 配置shard1所用到的replica sets:
Server1:
cd /usr/local/server/mongodb/bin
./bin/mongod --shardsvr --replSet shard1 --port 27017 --dbpath /usr/local/server/mongodb/data/shard11--oplogSize 2048 --logpath /usr/local/server/mongodb/data/shard11.log --logappend--fork

Server2:
cd /usr/local/server/mongodb/bin
./bin/mongod --shardsvr --replSet shard1 --port 27017 --dbpath /usr/local/server/mongodb/data/shard12--oplogSize 2048 --logpath /usr/local/server/mongodb/data/shard12.log --logappend--fork

Server3:
cd /usr/local/server/mongodb/bin
./bin/mongod --shardsvr --replSet shard1 --port 27017 --dbpath /usr/local/server/mongodb/data/shard13--oplogSize 2048 --logpath /usr/local/server/mongodb/data/shard13.log --logappend--fork

初始化replica set
用mongo连接其中一个mongod,执行:

./bin/mongo --port 27017
> config = {_id: 'shard1', members: [
                         {_id: 0, host: '172.16.101.244:27017'},
                         {_id: 1, host: '172.16.101.242:27017'},
                         {_id: 2, host: '172.16.101.241:27017'}]
           }

> rs.initiate(config);

同样方法,配置shard2用到的replica sets:
server1:
cd /usr/local/server/mongodb/bin
./bin/mongod --shardsvr --replSet shard2 --port 27018 --dbpath /usr/local/server/mongodb/data/shard21--oplogSize 2048 --logpath /usr/local/server/mongodb/data/shard21.log --logappend--fork

server2:
cd /usr/local/server/mongodb/bin
./bin/mongod --shardsvr --replSet shard2 --port 27018 --dbpath /usr/local/server/mongodb/data/shard22--oplogSize 2048 --logpath /usr/local/server/mongodb/data/shard22.log --logappend--fork

server3:
cd /usr/local/server/mongodb/bin
./bin/mongod --shardsvr --replSet shard2 --port 27018 --dbpath /usr/local/server/mongodb/data/shard23--oplogSize 2048 --logpath /usr/local/server/mongodb/data/shard23.log --logappend--fork

初始化replica set
用mongo连接其中一个mongod,执行:

./bin/mongo --port 27018
> config = {_id: 'shard2', members: [
                         {_id: 0, host: '172.16.101.244:27018'},
                         {_id: 1, host: '172.16.101.242:27018'},
                         {_id: 2, host: '172.16.101.241:27018'}]
           }

> rs.initiate(config);

到此就配置好了二个replica sets,也就是准备好了二个shards

配置三台config server

Server1:

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

touch data/config.log
./bin/mongod --configsvr --dbpath /usr/local/server/mongodb/data/config --port20000 --logpath /usr/local/server/mongodb/data/config.log --logappend --fork

#config server也需要dbpath
 
Server2:

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

touch data/config.log
./bin/mongod --configsvr --dbpath /usr/local/server/mongodb/data/config --port20000 --logpath /usr/local/server/mongodb/data/config.log --logappend --fork

Server3:

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

touch data/config.log
./bin/mongod --configsvr --dbpath /usr/local/server/mongodb/data/config --port20000 --logpath /usr/local/server/mongodb/data/config.log --logappend --fork

配置mongs

在server1,server2,server3上分别执行:

touch data/ mongos.log
./bin/mongos --configdb172.16.101.244:20000,172.16.101.242:20000,172.16.101.241:20000 --port 30000 --chunkSize64 --logpath /usr/local/server/mongodb/data/mongos.log --logappend --fork 
#mongs不需要dbpath

Configuring the Shard Cluster

连接到其中一个mongos进程,并切换到admin数据库做以下配置
1. 连接到mongs,并切换到admin
./bin/mongo 172.16.101.244:30000/admin
>db
Admin
2. 加入shards
如里shard是单台服务器,用>db.runCommand( { addshard :“<serverhostname>[:<port>]” } )这样的命令加入,如果shard是replica sets,用replicaSetName/<serverhostname>[:port][,serverhostname2[:port],…]这样的格式表示,例如本例执行:
>db.runCommand({addshard:"shard1/172.16.101.244:27017,172.16.101.242:27017,172.16.101.241:27017",name:"s1", maxsize:20480});
>db.runCommand({addshard:"shard2/172.16.101.244:27018,172.16.101.242:27018,172.16.101.241:27018",name:"s2", maxsize:20480});
注意:在添加第二个shard时,出现error:test database 已经存在的错误,这里用mongo命令连接到第二个replica set,用db.dropDatabase()命令把test数据库给删除然后就可加入

3. 可选参数
Name:用于指定每个shard的名字,不指定的话系统将自动分配
maxSize:指定各个shard可使用的最大磁盘空间,单位megabytes

4. Listing shards
>db.runCommand( { listshards : 1 } )
如果列出了以上二个你加的shards,表示shards已经配置成功

5. 激活数据库分片
命令:
> db.runCommand({enablesharding : “<dbname>”});
通过执行以上命令,可以让数据库跨shard,如果不执行这步,数据库只会存放在一个shard,一旦激活数据库分片,数据库中不同的collection将被存放在不同的shard上,但一个collection仍旧存放在同一个shard上,要使单个collection也分片,还需单独对collection作些操作

Collecton分片

要使单个collection也分片存储,需要给collection指定一个分片key,通过以下命令操作:
> db.runCommand( { shardcollection : “<namespace>”,key :<shardkeypatternobject> });

db.runCommand({shardcollection:"test.c1", key: {id: 1}}););

{ "collectionsharded" :"test.c1", "ok" : 1 }
注:
 a. 分片的collection系统会自动创建一个索引(也可用户提前创建好)
 b. 分片的collection只能有一个在分片key上的唯一索引,其它唯一索引不被允许
One note: a sharded collection can have only one unique index, which must existon the shard key. No other unique indexes can exist on the collection.

分片collection例子

> db.runCommand({shardcollection:"test.c1", key: {id: 1}}););
> for (vari=0; i<=2000000; i++) {db.c1.save({id: i, value1: "1234567890",value2: "1234567890", value3: "1234567890", value4:"1234567890"}); };
> db.c1.stats()
{
        “sharded” : true,
        “ns” : “test.c1″,
        “count” : 200003,
        “size” : 25600384,
        “avgObjSize” : 128,
        “storageSize” : 44509696,
        “nindexes” : 2,
        “nchunks” : 15,
        “shards” : {
               “s1″ : {
                       “ns” : “test.c1″,
                       “count” : 141941,
                       “size” : 18168448,
                       “avgObjSize” : 128,
                       “storageSize” : 33327616,
                       “numExtents” : 8,
                       “nindexes” : 2,
                       “lastExtentSize” : 12079360,
                       “paddingFactor” : 1,
                       “flags” : 1,
                       “totalIndexSize” : 11157504,
                       “indexSizes” : {
                               “_id_” : 5898240,
                               “id_1″ : 5259264
                       },
                       “ok” : 1
               },
               “s2″ : {
                       “ns” : “test.c1″,
                       “count” : 58062,
                       “size” : 7431936,
                       “avgObjSize” : 128,
                       “storageSize” : 11182080,
                       “numExtents” : 6,
                       “nindexes” : 2,
                       “lastExtentSize” : 8388608,
                       “paddingFactor” : 1,
                       “flags” : 1,
                       “totalIndexSize” : 4579328,
                       “indexSizes” : {
                               “_id_” : 2416640,
                               “id_1″ : 2162688
                       },
                       “ok” : 1
               }
        },
        “ok” : 1
}


你可能感兴趣的:(mongodb,linux,数据库,server,存储,sharding)