服务器环境:
192.168.236.62
192.168.236.63
192.168.236.66

1 各个服务器间时间同步
/usr/sbin/ntpdate asia.pool.ntp.org && /sbin/hwclock --systohc

2 关闭selinux
vi /etc/sysconfig/selinux

3 关闭防火墙
chkconfig --list|grep -i iptables
service iptable stop
********************************************************************************
1 创建用户组、用户、数据目录及日志目录
# groupadd mongodb
# useradd -g mongodb -d /home/mongodb -s /bin/bash mongodb
# passwd mongodb
# su - mongodb
$ mkdir mongodb_data
$ mkdir log
$ cd mongodb_data/
$ pwd
/home/mongodb/mongodb_data


2 将安装包放到mongodb目录下,并将属组权限给用户mongodb
# chown -R mongodb:mongodb mongodb/

3 解压安装包到当前目录下
$ ls
mongodb-linux-x86_64-2.6.5.tgz

$ tar -xvf mongodb-linux-x86_64-2.6.5.tgz

#########################################################################
构建一个 MongoDB Sharding Cluster,需要三种角色:
1 配置shard server
2 配置config server
3 配置mongs(路由进程)


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进程,用于客户端连接

配置replica sets
****************************
服务器192.168.236.62:
[mongodb@MongoDB-M mongodb_data]$ mkdir shard11
[mongodb@MongoDB-M mongodb_data]$ mkdir shard21
[mongodb@MongoDB-M ~]$ cd /mongodb/mongodb-linux-x86_64-2.6.5/bin/
创建shard11
[mongodb@MongoDB-M bin]$ ./mongod --shardsvr --replSet shard1 --port 27001 --dbpath=/home/mongodb/mongodb_data/shard11 --logpath=/home/mongodb/log/shard11_log.log --logappend --fork
创建shard21
[mongodb@MongoDB-M bin]$ ./mongod --shardsvr --replSet shard2 --port 27002 --dbpath=/home/mongodb/mongodb_data/shard21 --logpath=/home/mongodb/log/shard21_log.log --logappend --fork


服务器192.168.236.63
[mongodb@MongoDB-S mongodb_data]$ mkdir shard12
[mongodb@MongoDB-S mongodb_data]$ mkdir shard22
[mongodb@MongoDB-S mongodb_data]$ cd /mongodb/mongodb-linux-x86_64-2.6.5/bin/
创建shard12
[mongodb@MongoDB-S bin]$ ./mongod --shardsvr --replSet shard1 --port 27001 --dbpath=/home/mongodb/mongodb_data/shard12 --logpath=/home/mongodb/log/shard12_log.log --logappend --fork
创建shard22
[mongodb@MongoDB-S bin]$ ./mongod --shardsvr --replSet shard2 --port 27002 --dbpath=/home/mongodb/mongodb_data/shard22 --logpath=/home/mongodb/log/shard22_log.log --logappend --fork


服务器192.168.236.66
[mongodb@MongoDB-H mongodb_data]$ mkdir shard13
[mongodb@MongoDB-H mongodb_data]$ mkdir shard23
[mongodb@MongoDB-H mongodb_data]$ cd /mongodb/mongodb-linux-x86_64-2.6.5/bin/
创建shard13
[mongodb@MongoDB-H bin]$  ./mongod --shardsvr --replSet shard1 --port 27001 --dbpath=/home/mongodb/mongodb_data/shard13 --logpath=/home/mongodb/log/shard13_log.log --logappend --fork
创建shard23
[mongodb@MongoDB-H bin]$  ./mongod --shardsvr --replSet shard2 --port 27002 --dbpath=/home/mongodb/mongodb_data/shard23 --logpath=/home/mongodb/log/shard23_log.log --logappend --fork

初始化replica sets
******************************************************************
用mongo连接到任何一个mongod,执行

初始化shard1的replica sets
[mongodb@MongoDB-M bin]$ ./mongo --port 27001
> config = {_id: 'shard1', members: [
...                {_id: 0, host: '192.168.236.62:27001'},
...                {_id: 1, host: '192.168.236.63:27001'},
...                {_id: 2, host: '192.168.236.66:27001'}]
...           };

> rs.initiate(config);
{
     "info" : "Config now saved locally.  Should come online in about a minute.",
     "ok" : 1
}
>

初始化shard2的replica sets
[mongodb@MongoDB-M bin]$ ./mongo --port 27002
MongoDB shell version: 2.6.5
connecting to: 127.0.0.1:27002/test
> config = {_id: 'shard2', members: [
...                {_id:0, host: '192.168.236.62:27002'},
...                {_id:1, host: '192.168.236.63:27002'},
...                {_id:2, host: '192.168.236.66:27002'}]
...           };

> rs.initiate(config);
{
     "info" : "Config now saved locally.  Should come online in about a minute.",
     "ok" : 1
}
>

#####################################################################
仲裁分片
>use admin
>config = {_id:"s1", members: [
           {"_id":0,"host":"192.168.236.01:27017","priority":2},
           {"_id":1,"host":"192.168.236.02:27018","priority":1},
           {"_id":2,"host":"192.168.236.03:27019","arbiterOnly":true}
           ]
           }
> rs.initiate(config);
> exit;
#####################################################################

配置三台config serve
***************************************************************
服务器192.168.236.62
[mongodb@MongoDB-M ~]$ mkdir -p mongodb_data/config/
[mongodb@MongoDB-M ~]$ cd /mongodb/mongodb-linux-x86_64-2.6.5/bin/
[mongodb@MongoDB-M bin]$ ./mongod --configsvr --dbpath=/home/mongodb/mongodb_data/config/ --port=60000 --logpath=/home/mongodb/log/config.log --logappend --fork

服务器192.168.236.63
[mongodb@MongoDB-S ~]$ mkdir -p mongodb_data/config
[mongodb@MongoDB-S ~]$ cd /mongodb/mongodb-linux-x86_64-2.6.5/bin/
[mongodb@MongoDB-S bin]$ ./mongod --configsvr --dbpath=/home/mongodb/mongodb_data/config/ --port=60000 --logpath=/home/mongodb/log/config.log --logappend --fork

服务器192.168.236.66
[mongodb@MongoDB-H ~]$ mkdir -p mongodb_data/config
[mongodb@MongoDB-H ~]$ cd /mongodb/mongodb-linux-x86_64-2.6.5/bin/
[mongodb@MongoDB-H bin]$ ./mongod --configsvr --dbpath=/home/mongodb/mongodb_data/config/ --port=60000 --logpath=/home/mongodb/log/config.log --logappend --fork

配置mongos
***************************************************************
mongos不需要--dbpath

服务器192.168.236.62
[mongodb@MongoDB-M bin]$ ./mongos --configdb 192.168.236.62:60000,192.168.236.63:60000,192.168.236.66:60000 --port 50000 --chunkSize 64 --logpath=/home/mongodb/log/mongos.log --logappend --fork

服务器192.168.236.63
[mongodb@MongoDB-S bin]$  ./mongos --configdb 192.168.236.62:60000,192.168.236.63:60000,192.168.236.66:60000 --port 50000 --chunkSize 64 --logpath=/home/mongodb/log/mongos.log --logappend --fork

服务器192.168.236.66
[mongodb@MongoDB-H bin]$  ./mongos --configdb 192.168.236.62:60000,192.168.236.63:60000,192.168.236.66:60000 --port 50000 --chunkSize 64 --logpath=/home/mongodb/log/mongos.log --logappend --fork

配置shard server
****************************************************************
1 连接到其中一个mongos进程,并切换到admin数据库
[mongodb@MongoDB-M bin]$ ./mongo 192.168.236.62:50000/admin
MongoDB shell version: 2.6.5
connecting to: 192.168.236.62:50000/admin
mongos>

2 加入shards
mongos> db.runCommand({addshard:"shard1/192.168.236.62:27001,192.168.236.63:27001,192.168.236.66:27001",name:"s1"});
{ "shardAdded" : "s1", "ok" : 1 }
mongos> db.runCommand({addshard:"shard2/192.168.236.62:27002,192.168.236.63:27002,192.168.236.66:27002",name:"s2"});
{ "shardAdded" : "s2", "ok" : 1 }
mongos>

3 查看分片列表
mongos> db.runCommand({listshards:1});
{
     "shards" : [
          {
               "_id" : "s1",
               "host" : "shard1/192.168.236.62:27001,192.168.236.63:27001,192.168.236.66:27001"
          },
          {
               "_id" : "s2",
               "host" : "shard2/192.168.236.62:27002,192.168.236.63:27002,192.168.236.66:27002"
          }
     ],
     "ok" : 1
}
mongos>

4 启动分片
将数据库superwifi分片
mongos> db.runCommand({enablesharding:"superwifi"});
{ "ok" : 1 }

5 将collections分片
mongos> db.runCommand({shardcollection:"superwifi.test",key:{"_id":"hashed"}});
{ "collectionsharded" : "superwifi.test", "ok" : 1 }

10-10 02:29