Linux下Mongodb的分布式分片群集配置.docx
Sharding cluster 绍这是一种可以水平扩展的模式,在数据量很大时特给力,实际大规模应用一般会 采用这种架构去构建monodb系统。要构建一个MongoDBSharding Cluster,需要三种角色:Shard Server: mongod实例,用于存储实际的数据块,实际生产环境中一个 shard server角色可由几台机器组个一个replica set承担,防止主机单点故 障Config Server: mongod 实例,存储了整个 Cluster Metadata,其中包括 chunk信息。Route Server: mongos实例,前端路由,客户端由此接入,且让整个集群看 上去像单一数据库,前端应用可以透明使用。Sharding架构图*client机器分配和配置本例架构示例图:ServersM时赋d shnrd22rpn F i g 1Srrver IWnnRflri :ihrf! Il I甑日啊?WuiRnd slvirdli!Xet ,Ahard21Mmsd hardZS:匚个】ej;HWan if *d MdiFilManoslUnm 版 NMriorl rarirkI1. 分别在 3 台机器运行一个mongod 实例(称为 mongod shardll, mongod shard12,mongod shard13) 组织 replica setl,作为 cluster 的 shardl2. 分别在 3 台机器运行一个 mongod 实例(称为 mongod shard21,mongod shard22, mongod shard23) 组织 replica set2,作为 cluster 的 shard23. 每台机器运行一个mongod实例,作为3个config server4. 每台机器运行一个mongs进程,用于客户端连接主机IP端口信息Server1172.16.101.244mongod shard11:27017mongod shard21:27018mongod config1:20000mongs1:30000Server2172.16.101.242mongod shard12:27017mongod shard22:27018mongod config2:20000mongs2:30000Server3172.16.101.241mongod shard13:27017mongod shard23:27018mongod config3:20000mongs3:30000软件准备1.创建用户groupaddmongodbuseradd -gmongodbmongodbpasswdmongodb (123)2. 安装monodb软件chown -R mongodb:mongodb /data/ -这里需要把安装文件的权限赋给mongodb su - mongodbtar zxvfmongodb-linux-x86_64-1.8.2.tar3. 创建数据目录根据本例sharding架构图所示,在各台sever上创建shard数据文件目录Server1:su - monodbcd /usr/local/server/monodbmkdir-p data/shard11mkdir-p data/shard21touch data/shard11.logtouch data/shard21.logServer2:su - monodbcd /usr/local/server/monodbmkdir datamkdir -p data/shard12mkdir -p data/shard22touch data/shard12.logtouch data/shard22.logServer3:su - monodbcd /usr/local/server/monodbmkdir -p data/shard13mkdir -p data/shard23touch data/shard13.logtouch data/shard23.log配置 relica sets1. 配置 shard1 所用到的 replica sets:Server1:cd /usr/local/server/mongodb/bin./bin/mongod-shardsvr-replSet shard1 -port 27017 -dbpath/usr/local/server/mongodb/data/shard11 -oplogSize2048-logpath/usr/local/server/mongodb/data/shard11.log -logappend-forkServer2: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-forkServer3: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-forkserver2: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-forkserver3: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配置三台c onfig serverServerl:cd /usr/local/server/mongodb/mkdir -p data/configtouch data/config.log./bin/mongod-configsvr-dbpath /usr/local/server/mongodb/data/config-port 20000 -logpath /usr/local/server/mongodb/data/config.log -logappend-fork#config server 也需要 dbpathServer2:cd /usr/local/server/mongodb/mkdir -p /usr/local/server/mongodb/data/configtouch data/config.log./bin/mongod-configsvr-dbpath /usr/local/server/mongodb/data/config-port 20000 -logpath /usr/local/server/mongodb/data/config.log -logappend-forkServer3:cd /usr/local/server/mongodb/mkdir-p /usr/local/server/mongodb/data/configtouch data/config.log./bin/mongod-configsvr-dbpath /usr/local/server/mongodb/data/config-port 20000 -logpath /usr/local/server/mongodb/data/config.log -logappend-fork配置mongs在 server1,server2,server3 上分别执行:touch data/mongos.log./bin/mongos -configdb 172.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 不需要 dbpathConfiguring the Shard Cluster连接到其中一个mongos进程,并切换到admin数据库做以下配置1.连接到mongs,并切换到admin./bin/mongo 172.16.101.244:30000/admin>dbAdmin2. 加入 shards如里 shard 是单台服务器,用db.runCommand( addshard :、'<serverhostnana>: <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可使用的最大磁盘空间,单位megabytes4. Listing shards>db.runCommand( listshards : 1 )如果列出了以上二个你加的shards,表示shards已经配置成功5. 激活数据库分片命令:>db.runCommand(enablesharding :、'<dbname>");通过执行以上命令,可以让数据库跨shard,如果不执行这步,数据库只会存放在一个shard,一旦激活 数据库分片,数据库中不同的collection将被存放在不同的shard上,但一个collection仍旧存放在同 一个shard上,要使单个collection也分片,还需单独对collection作些操作Collector分片要使单个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: asharded collection canhave only one unique index,which must exist on the shard key. No other unique indexes can exist on the collection.分片collection例子>db.runCommand(shardcollection: "test.c1", key: id: 1););>for (var i = 0; i< = 2000000; i +) db.c1.save(id: i, value1: "1234567890", value2:"1234567890", value3: "1234567890", value4: "1234567890"); ;> db.c1.stats()“sharded" : true,“ns” : “test.cl”, “count" : 200003, “size" : 25600384, “avgObjSize" : 128, “storageSize" : 44509696, “nindexes" : 2, “nchunks": 15, “shards" : “s1 : “ns" : “test.cl”, “count" : 141941, “size" : 18168448, “avgObjSize" : 128, “storageSize" : 33327616, “numExtents" : 8, “nindexes" : 2, “lastExtentSize" : 12079360, “paddingFactor" : 1, “flags" : 1, “totallndexSize" : 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, “totallndexSize" : 4579328, “indexSizes" : “_id_" : 2416640, “id 1” : 2162688,“ok”: 1,“ok”: 1