Chinaunix首页 | 论坛 | 博客
  • 博客访问: 354461
  • 博文数量: 235
  • 博客积分: 2611
  • 博客等级: 少校
  • 技术积分: 1187
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-25 11:48
文章分类

全部博文(235)

文章存档

2015年(3)

2014年(2)

2011年(2)

2009年(20)

2008年(208)

分类: NOSQL

2014-05-20 20:26:59


一、环境

MongoDB Sharding Cluster 包含三种角色:

Shard Server: mongod 实例,用于存储实际的数据块,数据层

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

Route Server: mongos 实例,前端路由,客户端由此接入,且让整个集群看上去像单一进程  

 数据库,路由层。

 

 

10.14.2.201

10.14.2.202

10.14.2.203

10.14.2.204

Shard1

Shard2

Shard3

Shard1

Shard2

Shard1

Shard2

Shard3

Shard3

Shard3

Shard1

Shard2

 

1.节点:

S1: 10.14.2.201, 10.14.2.202, 10.14.2.203, 10.14.2.204 (arbiter)
S2: 10.14.2.201, 10.14.2.202, 10.14.2.203 (arbiter), 10.14.2.204
S3: 10.14.2.201, 10.14.2.202(arbiter), 10.14.2.203, 10.14.2.204

 

主机

端口信息

10.14.2.201

mongod shard1:27017

mongod shard2:27018

mongod shard3:27019

mongod config:30000

mongs:40000

10.14.2.202

mongod shard1:27017

mongod shard2:27018

mongod shard3:27019

mongod config:30000

mongs:40000

10.14.2.203

mongod shard1:27017

mongod shard2:27018

mongod shard3:27019

mongod config:30000

mongs:40000

10.14.2.204

mongod shard1:27017

mongod shard2:27018

mongod shard3:27019

mongod config:30000

mongs:40000

 


二、安装部署

在所有机器上运行

1、下载mongodb程序

    mongodb-linux-x86_64-2.4.4.tgz

2、解压mongodb-linux-x86_64-2.4.4.tgz/export/ 目录下

  tar -xvf mongodb-linux-x86_64-2.4.4.tgz  -C  /usr/local/

3、建立软连接

   cd /usr/local/

   ln -s mongodb-linux-x86_64-2.4.4  mongodb

 

4、配置环境变量  vim /etc/profile  添加 

#mongodb

export PATH=/usr/local/mongodb/bin:$PATH

source /etc/profile

 

5、创建目录

mkdir -p /export/mongodb/data/shard1

mkdir -p /export/mongodb/data/shard2

mkdir -p /export/mongodb/data/shard3

mkdir -p /export/mongodb/data/config

mkdir -p /export/mongodb/logs/shard1

mkdir -p /export/mongodb/logs/shard2

mkdir -p /export/mongodb/logs/shard3

mkdir -p /export/mongodb/logs/config

mkdir -p /export/mongodb/logs/mongos

 

6、在10.14.2.201(任意一台都可以) 生成keyfile文件

openssl rand -base64 753 > /export/mongodb/keyfile

 

7、把keyfile文件传输到其他机器上

scp /export/mongodb/keyfile 10.14.2.202:/export/mongodb/

scp /export/mongodb/keyfile 10.14.2.203:/export/mongodb/

scp /export/mongodb/keyfile 10.14.2.204:/export/mongodb/

 

8、在每台机器上执行 修改keyfile权限

chmod 600 /export/mongodb/keyfile

 

配置Replica Sets,Config Server

 

9、在每台机器上执行

/usr/local/mongodb/bin/mongod --fork --shardsvr  --port 27017 --dbpath /export/mongodb/data/shard1/ --logpath /export/mongodb/logs/shard1/log.log --logappend --replSet s1 --auth --rest  --keyFile    /export/mongodb/keyfile

/usr/local/mongodb/bin/mongod --fork --shardsvr  --port 27018 --dbpath /export/mongodb/data/shard2/ --logpath /export/mongodb/logs/shard2/log.log --logappend --replSet s2 --auth  --rest   --keyFile   /export/mongodb/keyfile

/usr/local/mongodb/bin/mongod --fork --shardsvr  --port 27019 --dbpath /export/mongodb/data/shard3/ --logpath /export/mongodb/logs/shard3/log.log --logappend --replSet s3 --auth  --rest   --keyFile    /export/mongodb/keyfile

/usr/local/mongodb/bin/mongod --fork --configsvr --port 30000 --dbpath /export/mongodb/data/config/ --logpath /export/mongodb/logs/config/log.log --logappend --auth --rest   --keyFile    /export/mongodb/keyfile

 

 10、10.14.2.201上执行

 

1)查看mongod的进程是否启动

ps aux | grep mongodb | grep -v grep

 
2)初始化replica sets

/usr/local/mongodb/bin/mongo --port 27017

  config = {_id: 's1', members: [
        {_id: 0, host: '10.14.2.201:27017'},
        {_id: 1, host: '10.14.2.202:27017'},

        {_id: 2, host: '10.14.2.203:27017'},
        {_id: 3, host: '10.14.2.204:27017', arbiterOnly: true}
    ]}

    rs.initiate(config)
    rs.status()

11、10.14.2.202上执行

 

1)查看mongod的进程是否启动

ps aux | grep mongodb | grep -v grep

 

2)初始化replica sets

/usr/local/mongodb/bin/mongo --port  27018

  config = {_id: 's2', members: [
        {_id: 0, host: '10.14.2.201:27018'},
        {_id: 1, host: '10.14.2.202:27018'},

        {_id: 2, host: '10.14.2.203:27018', arbiterOnly: true },
        {_id: 3, host: '10.14.2.204:27018' }
    ]}
   rs.initiate(config)
   rs.status()

 

12、10.14.2.203上执行 

1)查看mongod的进程是否启动

ps aux | grep mongodb | grep -v grep

 

2)初始化replica sets

/usr/local/mongodb/bin/mongo --port 27019

   config = {_id: 's3', members: [
        {_id: 0, host: '10.14.2.201:27019'},
        {_id: 1, host: '10.14.2.202:27019', arbiterOnly: true },

        {_id: 2, host: '10.14.2.203:27019' },
        {_id: 3, host: '10.14.2.204:27019' }
    ]}
   rs.initiate(config)
    rs.status()

 

 

配置Mongos(在每一台机子上建立路由)

13、在每台机器上执行

/usr/local/mongodb/bin/mongos --fork --port 40000 --logpath  /export/mongodb/logs/mongos/log.log  --configdb 10.14.2.201:30000,10.14.2.202:30000,10.14.2.203:30000   --keyFile    /export/mongodb/keyfile

 

添加分片

1连接任意一台,其他无需这样操作:

/usr/local/mongodb/bin/mongo --port 40000

use admin

db.runCommand({addshard:'s1/10.14.2.201:27017,10.14.2.202:27017,10.14.2.203:27017'})

db.runCommand({addshard:'s2/10.14.2.201:27018,10.14.2.202:27018,10.14.2.203:27018' })

db.runCommand({addshard:'s3/10.14.2.201:27019,10.14.2.203:27019,10.14.2.203:27019' })

 

db.runCommand({ listshards:1 })

通过执行以下命令,可以让数据库跨shard,如果不执行这步,数据库只会存放在一个shard,一旦激活数据库分片,数据库中不同的collection将被存放在不同的shard上,但一个collection仍旧存放在同一个shard上,要使单个collection也分片,还需单独对collection作些操作

db.runCommand({enablesharding:"test"});

要使单个collection也分片存储,需要给collection指定一个分片key,通过以下命令操作

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

 

创建测试数据

use test

 for(var i=1;i<=200003;i++)  db.c1.save({id:i,value1:"1234567890",value2:"1234567890",value3:"1234567890",value4:"1234567890"});


> db.c1.stats()  \\查看数据已经分到了s1s2s3

{

      "sharded" : true,

      "ns" : "test.c1",

      "count" : 200003,

      "numExtents" : 14,

      "size" : 25600384,

      "storageSize" : 40599552,

      "totalIndexSize" : 12182240,

      "indexSizes" : {

            "_id_" : 6540800,

            "id_1" : 5641440

      },

      "avgObjSize" : 128,

      "nindexes" : 2,

      "nchunks" : 4,

      "shards" : {

            "s1" : {

                  "ns" : "test.c1",

                  "count" : 194287,

                  "size" : 24868736,

                  "avgObjSize" : 128,

                  "storageSize" : 37797888,

                  "numExtents" : 8,

                  "nindexes" : 2,

                  "lastExtentSize" : 15290368,

                  "paddingFactor" : 1,

                  "systemFlags" : 1,

                  "userFlags" : 0,

                  "totalIndexSize" : 11781616,

                  "indexSizes" : {

                       "_id_" : 6328224,

                       "id_1" : 5453392

                  },

                  "ok" : 1

            },

            "s2" : {

                  "ns" : "test.c1",

                  "count" : 0,

                  "size" : 0,

                  "storageSize" : 8192,

                  "numExtents" : 1,

                  "nindexes" : 2,

                  "lastExtentSize" : 8192,

                  "paddingFactor" : 1,

                  "systemFlags" : 1,

                  "userFlags" : 0,

                  "totalIndexSize" : 16352,

                  "indexSizes" : {

                       "_id_" : 8176,

                       "id_1" : 8176

                  },

                  "ok" : 1

            },

            "s3" : {

                  "ns" : "test.c1",

                  "count" : 5716,

                  "size" : 731648,

                  "avgObjSize" : 128,

                  "storageSize" : 2793472,

                  "numExtents" : 5,

                  "nindexes" : 2,

                  "lastExtentSize" : 2097152,

                  "paddingFactor" : 1,

                  "systemFlags" : 1,

                  "userFlags" : 0,

                  "totalIndexSize" : 384272,

                  "indexSizes" : {

                       "_id_" : 204400,

                       "id_1" : 179872

                  },

                  "ok" : 1

            }

      },

      "ok" : 1

}

 

 

阅读(1760) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~