Replica Set使用的是n个mongod节点,构建具备自动的容错功能(auto-failover),自动恢复的(auto-recovery)的高可用方案。
使用Replica Set来实现读写分离。通过在连接时指定或者在主库指定slaveOk,由Secondary来分担读的压力,Primary只承担写操作。
对于Replica Set中的secondary 节点默认是不可读的。
架构图:
分别在各服务器上运行两个mongod实例:
shard11 + shard12 + shard13 ----> 组成一个replica set --|
|-----> sharding_cluster
shard21 + shard22 + shard23 ----> 组成一个replica set --|
Shard Server: 用于存储实际的数据块,实际生产环境中一个shard server角色可由几台机器组个一个relica set承担,防止主机单点故障!
Config Server: 存储了整个 Cluster Metadata,其中包括 chunk 信息!
Route Server: 前端路由,客户端由此接入,且让整个集群看上去像单一数据库,前端应用可以透明使用。
一、安装配置mongodb环境
1.安装
wget http:
tar zxf mongodb-linux-x86_64-2.0.4.tgz
mv mongodb-linux-x86_64-2.0.4 /opt/mongodb
echo "export PATH=$PATH:/opt/mongodb/bin" >> /etc/profile
source /etc/profile
2.建立用户和组
useradd -u 600 -s /bin/false mongodb
3.创建数据目录
在各服务器上建立如下目录:
30服务器:
mkdir -p /data0/mongodb/{db,logs}
mkdir -p /data0/mongodb/db/{shard11,shard21,config}
31服务器:
mkdir -p /data0/mongodb/{db,logs}
mkdir -p /data0/mongodb/db/{shard12,shard22,config}
32服务器:
mkdir -p /data0/mongodb/{db,logs}
mkdir -p /data0/mongodb/db/{shard13,shard23,config}
4.设置各节点服务器hosts解析
true > /etc/hosts
echo -ne "
192.168.8.30 mong01
192.168.8.31 mong02
192.168.8.32 mong03
" >>/etc/hosts
或
cat >> /etc/hosts << EOF
192.168.8.30 mong01
192.168.8.31 mong02
192.168.8.32 mong03
EOF
5.同步时钟
ntpdate ntp.api.bz
写到crontab任务计划中!
这里务必要同步时钟,不然shrad不能同步!
以上配置各节点都进行操作!!
二、配置relica sets
1.配置两个shard
30 server:
/opt/mongodb/bin/mongod -shardsvr -replSet shard1 -port 27021 -dbpath /data0/mongodb/db/shard11 -oplogSize 1000 -logpath /data0/mongodb/logs/shard11.log -logappend --maxConns 10000 --quiet -fork --directoryperdb
sleep 2
/opt/mongodb/bin/mongod -shardsvr -replSet shard2 -port 27022 -dbpath /data0/mongodb/db/shard21 -oplogSize 1000 -logpath /data0/mongodb/logs/shard21.log -logappend --maxConns 10000 --quiet -fork --directoryperdb
sleep 2
echo "all mongo started."
31 server:
/opt/mongodb/bin/mongod -shardsvr -replSet shard1 -port 27021 -dbpath /data0/mongodb/db/shard12 -oplogSize 1000 -logpath /data0/mongodb/logs/shard12.log -logappend --maxConns 10000 --quiet -fork --directoryperdb
sleep 2
numactl --interleave=all /opt/mongodb/bin/mongod -shardsvr -replSet shard2 -port 27022 -dbpath /data0/mongodb/db/shard22 -oplogSize 1000 -logpath /data0/mongodb/logs/shard22.log -logappend --maxConns 10000 --quiet -fork --directoryperdb
sleep 2
echo "all mongo started."
32 server:
numactl --interleave=all /opt/mongodb/bin/mongod -shardsvr -replSet shard1 -port 27021 -dbpath /data0/mongodb/db/shard13 -oplogSize 1000 -logpath /data0/mongodb/logs/shard13.log -logappend --maxConns 10000 --quiet -fork --directoryperdb
sleep 2
numactl --interleave=all /opt/mongodb/bin/mongod -shardsvr -replSet shard2 -port 27022 -dbpath /data0/mongodb/db/shard23 -oplogSize 1000 -logpath /data0/mongodb/logs/shard23.log -logappend --maxConns 10000 --quiet -fork --directoryperdb
sleep 2
echo "all mongo started."
可以对应的把上面的命令放在一个脚本内,方便启动!
也可以写到配置文件中,通过-f参数来启动!
改成配置文件方式:
shardsvr = true
replSet = shard1
port = 27021
dbpath = /data0/mongodb/db/shard11
oplogSize = 1000
logpath = /data0/mongodb/logs/shard11.log
logappend = true
maxConns = 10000
quit=true
profile = 1
slowms = 5
rest = true
fork = true
directoryperdb = true
这样可以通过 mognod -f mongodb.conf来启动了!
我这里把这些命令都放入脚本中:
启动脚本(这里只启动mongod,后面有专门启动config和mongos脚本):
[root@mon1 sh]# cat start.sh
/opt/mongodb/bin/mongod -shardsvr -replSet shard1 -port 27021 -dbpath /data0/mongodb/db/shard11 -oplogSize 1000 -logpath /data0/mongodb/logs/shard11.log -logappend --maxConns 10000 --quiet -fork --directoryperdb
sleep 2
/opt/mongodb/bin/mongod -shardsvr -replSet shard2 -port 27022 -dbpath /data0/mongodb/db/shard21 -oplogSize 1000 -logpath /data0/mongodb/logs/shard21.log -logappend --maxConns 10000 --quiet -fork --directoryperdb
sleep 2
ps aux |grep mongo
echo "all mongo started."
[root@mon2 sh]# cat start.sh
/opt/mongodb/bin/mongod -shardsvr -replSet shard1 -port 27021 -dbpath /data0/mongodb/db/shard12 -oplogSize 1000 -logpath /data0/mongodb/logs/shard12.log -logappend --maxConns 10000 --quiet -fork --directoryperdb
sleep 2
/opt/mongodb/bin/mongod -shardsvr -replSet shard2 -port 27022 -dbpath /data0/mongodb/db/shard22 -oplogSize 1000 -logpath /data0/mongodb/logs/shard22.log -logappend --maxConns 10000 --quiet -fork --directoryperdb
sleep 2
ps aux |grep mongo
echo "all mongo started."
[root@mongo03 sh]# cat start.sh
/opt/mongodb/bin/mongod -shardsvr -replSet shard1 -port 27021 -dbpath /data0/mongodb/db/shard11 -oplogSize 1000 -logpath /data0/mongodb/logs/shard11.log -logappend --maxConns 10000 --quiet -fork --directoryperdb --keyFile=/opt/mongodb/sh/keyFile
sleep 2
/opt/mongodb/bin/mongod -shardsvr -replSet shard2 -port 27022 -dbpath /data0/mongodb/db/shard21 -oplogSize 1000 -logpath /data0/mongodb/logs/shard21.log -logappend --maxConns 10000 --quiet -fork --directoryperdb --keyFile=/opt/mongodb/sh/keyFile
sleep 2
echo "all mongo started."
PS:要是想开启一个HTTP协议的端口提供rest服务,可以在mongod启动参数中加上 --rest 选项!
这样我们可以通过 http://IP:28021/_replSet 查看状态!
生产环境推荐用配置文件和脚本文件方式启动。
三、初始化replica set
1.配置shard1用到的replica sets
[root@mongo01 ~]# mongo 192.168.8.30:27021
> config = {_id: 'shard1', members: [
{_id: 0, host: '192.168.8.30:27021'},
{_id: 1, host: '192.168.8.31:27021'},
{_id: 2, host: '192.168.8.32:27021'}]
}
> config = {_id: 'shard1', members: [
... {_id: 0, host: '192.168.8.30:27021'},
... {_id: 1, host: '192.168.8.31:27021'},
... {_id: 2, host: '192.168.8.32:27021'}]
... }
{
"_id" : "shard1",
"members" : [
{
"_id" : 0,
"host" : "192.168.8.30:27021"
},
{
"_id" : 1,
"host" : "192.168.8.31:27021"
},
{
"_id" : 2,
"host" : "192.168.8.32:27021"
}
]
}
出现如下信息表示成功:
> rs.initiate(config)
{
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
> rs.status()
{
"set" : "shard1",
"date" : ISODate("2012-06-07T11:35:22Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "192.168.8.30:27021",
"health" : 1, #1 表示正常
"state" : 1, #1 表示是primary
"stateStr" : "PRIMARY", #表示此服务器是主库
"optime" : {
"t" : 1339068873000,
"i" : 1
},
"optimeDate" : ISODate("2012-06-07T11:34:33Z"),
"self" : true
},
{
"_id" : 1,
"name" : "192.168.8.31:27021",
"health" : 1, #1 表示正常
"state" : 2, #2 表示是secondary
"stateStr" : "SECONDARY", #表示此服务器是从库
"uptime" : 41,
"optime" : {
"t" : 1339068873000,
"i" : 1
},
"optimeDate" : ISODate("2012-06-07T11:34:33Z"),
"lastHeartbeat" : ISODate("2012-06-07T11:35:21Z"),
"pingMs" : 7
},
{
"_id" : 2,
"name" : "192.168.8.32:27021",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 36,
"optime" : {
"t" : 1341373105000,
"i" : 1
},
"optimeDate" : ISODate("2012-06-07T11:34:00Z"),
"lastHeartbeat" : ISODate("2012-06-07T11:35:21Z"),
"pingMs" : 3
}
],
"ok" : 1
}
PRIMARY>
可以看马上变成 PRIMARY 即主节点!
再到其它节点看下:
[root@mongo02 sh]# mongo 192.168.8.31:27021
MongoDB shell version: 2.0.5
connecting to: 192.168.8.31:27021/test
SECONDARY>
[root@mongo03 sh]# mongo 192.168.8.32:27021
MongoDB shell version: 2.0.5
connecting to: 192.168.8.32:27021/test
SECONDARY>
在所有节点上可以查看Replica Sets 的配置信息:
PRIMARY> rs.conf()
{
"_id" : "shard1",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "192.168.8.30:27021"
},
{
"_id" : 1,
"host" : "192.168.8.31:27021"
},
{
"_id" : 2,
"host" : "192.168.8.32:27021",
"shardOnly" : true
}
]
}
2.配置shard2用到的replica sets
[root@mongo02 sh]# mongo 192.168.8.30:27022
MongoDB shell version: 2.0.5
connecting to: 192.168.8.30:27022/test
> config = {_id: 'shard2', members: [
{_id: 0, host: '192.168.8.30:27022'},
{_id: 1, host: '192.168.8.31:27022'},
{_id: 2, host: '192.168.8.32:27022'}]
}
> config = {_id: 'shard2', members: [
... {_id: 0, host: '192.168.8.30:27022'},
... {_id: 1, host: '192.168.8.31:27022'},
... {_id: 2, host: '192.168.8.32:27022'}]
... }
{
"_id" : "shard2",
"members" : [
{
"_id" : 0,
"host" : "192.168.8.30:27022"
},
{
"_id" : 1,
"host" : "192.168.8.31:27022"
},
{
"_id" : 2,
"host" : "192.168.8.32:27022"
}
]
}
> rs.initiate(config)
{
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
验证节点:
> rs.status()
{
"set" : "shard2",
"date" : ISODate("2012-06-07T11:43:47Z"),
"myState" : 2,
"members" : [
{
"_id" : 0,
"name" : "192.168.8.30:27022",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"optime" : {
"t" : 1341367921000,
"i" : 1
},
"optimeDate" : ISODate("2012-06-07T11:43:40Z"),
"self" : true
},
{
"_id" : 1,
"name" : "192.168.8.31:27022",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 50,
"optime" : {
"t" : 1341367921000,
"i" : 1
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2012-06-07T11:43:46Z"),
"pingMs" : 0,
},
{
"_id" : 2,
"name" : "192.168.8.32:27022",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 81,
"optime" : {
"t" : 1341373254000,
"i" : 1
},
"optimeDate" : ISODate("2012-06-07T11:41:00Z"),
"lastHeartbeat" : ISODate("2012-06-07T11:43:46Z"),
"pingMs" : 0,
}
],
"ok" : 1
}
PRIMARY>
到此就配置好了二个replica sets!
PS: 初始化时,不指定priority默认id 0 为primary
状态中关键数据位:
在用 rs.status()查看replica sets状态时,
state:1表示该host是当前可以进行读写,2:不能读写
health:1表示该host目前是正常的,0:异常
注意:初使化replica sets时也可以用这种方法:
db.runCommand({"replSetInitiate":{"_id":"shard1","members":[{"_id":0,"host":"192.168.8.30:27021"},{"_id":1,"host":"192.168.8.31:27021"},{"_id":2,"host":"192.168.8.32:27021","shardOnly":true}]}})
可以省略用rs.initiate(config)。
四、配置三台config server
分别在各服务器上运行(启动都一样):
/opt/mongodb/bin/mongod --configsvr --dbpath /data0/mongodb/db/config --port 20000 --logpath /data0/mongodb/logs/config.log --logappend --fork --directoryperdb
用脚本形式:
[root@mongo01 sh]# cat config.sh
/opt/mongodb/bin/mongod --configsvr --dbpath /data0/mongodb/db/config --port 20000 --logpath /data0/mongodb/logs/config.log --logappend --fork --directoryperdb
[root@mongo01 sh]# pwd
/opt/mongodb/sh
[root@mongo01 sh]# ./config.sh
然后在各节点查看有没有启动起来:
[root@mongo01 sh]# ps aux |grep mong
root 25343 0.9 6.8 737596 20036 ? Sl 19:32 0:12 /opt/mongodb/bin/mongod -shardsvr -replSet shard1 -port 27021 -dbpath /data0/mongodb/db/shard1 -oplogSize 100 -logpath /data0/mongodb/logs/shard1.log -logappend --maxConns 10000 --quiet -fork --directoryperdb
root 25351 0.9 7.0 737624 20760 ? Sl 19:32 0:11 /opt/mongodb/bin/mongod -shardsvr -replSet shard2 -port 27022 -dbpath /data0/mongodb/db/shard2 -oplogSize 100 -logpath /data0/mongodb/logs/shard2.log -logappend --maxConns 10000 --quiet -fork --directoryperdb
root 25669 13.0 4.7 118768 13852 ? Sl 19:52 0:07 /opt/mongodb/bin/mongod --configsvr --dbpath /data0/mongodb/db/config --port 20000 --logpath /data0/mongodb/logs/config.log --logappend --fork --directoryperdb
root 25695 0.0 0.2 61220 744 pts/3 R+ 19:53 0:00 grep mong
五、配置mongs(启动路由)
分别在206、207服务器上运行(也可以在所有节点上启动):
/opt/mongodb/bin/mongos -configdb 192.168.8.30:20000,192.168.8.31:20000,192.168.8.32:20000 -port 30000 -chunkSize 50 -logpath /data0/mongodb/logs/mongos.log -logappend -fork
用脚本形式:
[root@mongo01 sh]# cat mongos.sh
/opt/mongodb/bin/mongos -configdb 192.168.8.30:20000,192.168.8.31:20000,192.168.8.32:20000 -port 30000 -chunkSize 50 -logpath /data0/mongodb/logs/mongos.log -logappend -fork
[root@mongo01 sh]# pwd
/opt/mongodb/sh
[root@mongo01 sh]# ./mongos.sh
[root@mongo01 sh]# ps aux |grep mong
root 25343 0.8 6.8 737596 20040 ? Sl 19:32 0:13 /opt/mongodb/bin/mongod -shardsvr -replSet shard1 -port 27021 -dbpath /data0/mongodb/db/shard1 -oplogSize 100 -logpath /data0/mongodb/logs/shard1.log -logappend --maxConns 10000 --quiet -fork --directoryperdb
root 25351 0.9 7.0 737624 20768 ? Sl 19:32 0:16 /opt/mongodb/bin/mongod -shardsvr -replSet shard2 -port 27022 -dbpath /data0/mongodb/db/shard2 -oplogSize 100 -logpath /data0/mongodb/logs/shard2.log -logappend --maxConns 10000 --quiet -fork --directoryperdb
root 25669 2.0 8.0 321852 23744 ? Sl 19:52 0:09 /opt/mongodb/bin/mongod --configsvr --dbpath /data0/mongodb/db/config --port 20000 --logpath /data0/mongodb/logs/config.log --logappend --fork --directoryperdb
root 25863 0.5 0.8 90760 2388 ? Sl 20:00 0:00 /opt/mongodb/bin/mongos -configdb 192.168.8.30:20000,192.168.8.31:20000,192.168.8.32:20000 -port 30000 -chunkSize 50 -logpath /data0/mongodb/logs/mongos.log -logappend -fork
root 25896 0.0 0.2 61220 732 pts/3 D+ 20:00 0:00 grep mong
注意:
1). mongos里面的ip和端口是config服务的ip和端口:192.168.8.30:20000,192.168.8.31:20000,192.168.8.32:20000
2). 必须先启动config后(并且config启动正常后,有config的进程存在)再启动mongos
六、配置shard集群
1.连接一台路由
[root@mongo01 sh]# mongo 192.168.8.30:30000/admin
MongoDB shell version: 2.0.5
connecting to: 192.168.8.30:30000/admin
mongos>
2.加入shards
mongos> db.runCommand({ addshard : "shard1/192.168.8.30:27021,192.168.8.31:27021,192.168.8.32:27021",name:"shard1",maxSize:20480})
{ "shardAdded" : "shard1", "ok" : 1 }
mongos> db.runCommand({ addshard : "shard2/192.168.8.30:27022,192.168.8.31:27022,192.168.8.32:27022",name:"shard2",maxSize:20480})
{ "shardAdded" : "shard2", "ok" : 1 }
PS:
分片操作必须在 admin 库下操作
如果只启动206和207服务器的路由!因此可不用把208服务器加进来!
可选参数说明:
Name:用于指定每个shard的名字,不指定的话系统将自动分配
maxSize:指定各个shard可使用的最大磁盘空间,单位 MegaBytes
3.列出加入的shards
mongos> db.runCommand( { listshards : 1 } );
{
"shards" : [
{
"_id" : "shard1",
"host" : "shard1/192.168.8.30:27021,192.168.8.31:27021,192.168.8.32:27021",
"maxSize" : NumberLong(20480)
},
{
"_id" : "shard2",
"host" : "shard2/192.168.8.30:27022,192.168.8.31:27022,192.168.8.32:27022",
"maxSize" : NumberLong(20480)
}
],
"ok" : 1
}
PS: 列出了以上二个我加的shards(shard1和shard2),表示shards已经配置成功!!
如果206那台机器挂了,其它两个节点中某个会成为主节点,mongos会自动连接到主节点!
mongos> db.runCommand({ismaster:1});
{
"ismaster" : true,
"msg" : "isdbgrid",
"maxBsonObjectSize" : 16777216,
"ok" : 1
}
mongos> db.runCommand( { listshards : 1 } );
{ "ok" : 0, "errmsg" : "access denied - use admin db" }
mongos> use admin
switched to db admin
mongos> db.runCommand( { listshards : 1 } );
{
"shards" : [
{
"_id" : "s1",
"host" : "shard1/192.168.8.30:27021,192.168.8.31:27021"
},
{
"_id" : "s2",
"host" : "shard2/192.168.8.30:27022,192.168.8.31:27022"
}
],
"ok" : 1
}
mongos>
七.添加分片
1.激活数据库分片
db.runCommand( { enablesharding : "
如:db.runCommand( { enablesharding : "" } );
插入测试数据:
mongos> use nosql
switched to db nosql
mongos> for(var i=0;i<100;i++)db.fans.insert({uid:i,uname:'nosqlfans'+i});
激活数据库:
mongos> use admin
switched to db admin
mongos> db.runCommand( { enablesharding : "nosql" } );
{ "ok" : 1 }
通过执行以上命令,可以让数据库跨shard,如果不执行这步,数据库只会存放在一个shard,一旦激活数据库分片,数据库中不同的collection 将被存放在不同的shard上,但一个collection仍旧存放在同一个shard上,要使单个collection也分片,还需单独对 collection作些操作!
2.添加索引
必须加索引,不然不能对collections分片!
mongos> use nosql
switched to db nosql
mongos> db.fans.find()
{ "_id" : ObjectId("4ff2ae6816df1d1b33bad081"), "uid" : 0, "uname" : "nosqlfans0" }
{ "_id" : ObjectId("4ff2ae6816df1d1b33bad082"), "uid" : 1, "uname" : "nosqlfans1" }
{ "_id" : ObjectId("4ff2ae6816df1d1b33bad083"), "uid" : 2, "uname" : "nosqlfans2" }
{ "_id" : ObjectId("4ff2ae6816df1d1b33bad084"), "uid" : 3, "uname" : "nosqlfans3" }
{ "_id" : ObjectId("4ff2ae6816df1d1b33bad085"), "uid" : 4, "uname" : "nosqlfans4" }
{ "_id" : ObjectId("4ff2ae6816df1d1b33bad086"), "uid" : 5, "uname" : "nosqlfans5" }
{ "_id" : ObjectId("4ff2ae6816df1d1b33bad087"), "uid" : 6, "uname" : "nosqlfans6" }
{ "_id" : ObjectId("4ff2ae6816df1d1b33bad088"), "uid" : 7, "uname" : "nosqlfans7" }
{ "_id" : ObjectId("4ff2ae6816df1d1b33bad089"), "uid" : 8, "uname" : "nosqlfans8" }
{ "_id" : ObjectId("4ff2ae6816df1d1b33bad08a"), "uid" : 9, "uname" : "nosqlfans9" }
{ "_id" : ObjectId("4ff2ae6816df1d1b33bad08b"), "uid" : 10, "uname" : "nosqlfans10" }
{ "_id" : ObjectId("4ff2ae6816df1d1b33bad08c"), "uid" : 11, "uname" : "nosqlfans11" }
{ "_id" : ObjectId("4ff2ae6816df1d1b33bad08d"), "uid" : 12, "uname" : "nosqlfans12" }
{ "_id" : ObjectId("4ff2ae6816df1d1b33bad08e"), "uid" : 13, "uname" : "nosqlfans13" }
{ "_id" : ObjectId("4ff2ae6816df1d1b33bad08f"), "uid" : 14, "uname" : "nosqlfans14" }
{ "_id" : ObjectId("4ff2ae6816df1d1b33bad090"), "uid" : 15, "uname" : "nosqlfans15" }
{ "_id" : ObjectId("4ff2ae6816df1d1b33bad091"), "uid" : 16, "uname" : "nosqlfans16" }
{ "_id" : ObjectId("4ff2ae6816df1d1b33bad092"), "uid" : 17, "uname" : "nosqlfans17" }
{ "_id" : ObjectId("4ff2ae6816df1d1b33bad093"), "uid" : 18, "uname" : "nosqlfans18" }
{ "_id" : ObjectId("4ff2ae6816df1d1b33bad094"), "uid" : 19, "uname" : "nosqlfans19" }
has more
mongos> db.fans.ensureIndex({"uid":1})
mongos> db.fans.find({uid:10}).explain()
{
"cursor" : "BtreeCursor uid_1",
"nscanned" : 1,
"nscannedObjects" : 1,
"n" : 1,
"millis" : 25,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {
"uid" : [
[
10,
10
]
]
}
}
3.Collecton分片
要使单个collection也分片存储,需要给collection指定一个分片key,通过以下命令操作:
db.runCommand( { shardcollection : "",key : });
mongos> use admin
switched to db admin
mongos> db.runCommand({shardcollection : "nosql.fans",key : {uid:1}})
{ "collectionsharded" : "nosql.fans", "ok" : 1 }
PS:
1). 操作必须切换到admin数据库下
2). 分片的collection系统要创建好索引
3). 分片的collection只能有一个在分片key上的唯一索引,其它唯一索引不被允许
4.查看分片状态
mongos> use nosql
mongos> db.fans.stats()
{
"sharded" : true,
"flags" : 1,
"ns" : "nosql.fans",
"count" : 100,
"numExtents" : 2,
"size" : 5968,
"storageSize" : 20480,
"totalIndexSize" : 24528,
"indexSizes" : {
"_id_" : 8176,
"uid0_1" : 8176,
"uid_1" : 8176
},
"avgObjSize" : 59.68,
"nindexes" : 3,
"nchunks" : 1,
"shards" : {
"shard1" : {
"ns" : "nosql.test",
"count" : 100,
"size" : 5968,
"avgObjSize" : 59.68,
"storageSize" : 20480,
"numExtents" : 2,
"nindexes" : 3,
"lastExtentSize" : 16384,
"paddingFactor" : 1,
"flags" : 1,
"totalIndexSize" : 24528,
"indexSizes" : {
"_id_" : 8176,
"uid0_1" : 8176,
"uid_1" : 8176
},
"ok" : 1
}
},
"ok" : 1
}
mongos>
些时分片没有发生变化!再插入比较多的数据:
mongos> use nosql
switched to db nosql
mongos> for(var i=200;i<200003;i++)db.fans.save({uid:i,uname:'nosqlfans'+i});
mongos> db.fans.stats()
{
"sharded" : true,
"flags" : 1,
"ns" : "nosql.fans",
"count" : 200002,
"numExtents" : 12,
"size" : 12760184,
"storageSize" : 22646784,
"totalIndexSize" : 12116832,
"indexSizes" : {
"_id_" : 6508096,
"uid_1" : 5608736
},
"avgObjSize" : 63.80028199718003,
"nindexes" : 2,
"nchunks" : 10,
"shards" : {
"shard1" : {
"ns" : "nosql.fans",
"count" : 9554,
"size" : 573260,
"avgObjSize" : 60.00209336403601,
"storageSize" : 1396736,
"numExtents" : 5,
"nindexes" : 2,
"lastExtentSize" : 1048576,
"paddingFactor" : 1,
"flags" : 1,
"totalIndexSize" : 596848,
"indexSizes" : {
"_id_" : 318864,
"uid_1" : 277984
},
"ok" : 1
},
"shard2" : {
"ns" : "nosql.fans",
"count" : 190448,
"size" : 12186924,
"avgObjSize" : 63.990821641602956,
"storageSize" : 21250048,
"numExtents" : 7,
"nindexes" : 2,
"lastExtentSize" : 10067968,
"paddingFactor" : 1,
"flags" : 1,
"totalIndexSize" : 11519984,
"indexSizes" : {
"_id_" : 6189232,
"uid_1" : 5330752
},
"ok" : 1
}
},
"ok" : 1
}
mongos>
当再次插入大量数据时。。自动分片处理了!!所以OK!!!
八.停止所有服务脚本
[root@mon1 sh]# cat /opt/mongodb/sh/stop.sh
#!/bin/sh
check=`ps aux|grep mongo|grep configdb|awk '{print $2;}'|wc -l`
echo $check
while [ $check -gt 0 ]
do
# echo $check
no=`ps aux|grep mongo|grep configdb|awk '{print $2;}'|sed -n '1p'`
kill -3 $no
echo "kill $no mongo daemon is ok."
sleep 2
check=`ps aux|grep mongo|grep configdb|awk '{print $2;}'|wc -l`
echo "stopping mongo,pls waiting..."
done
check=`ps aux|grep mongo|grep configsvr|awk '{print $2;}'|wc -l`
echo $check
while [ $check -gt 0 ]
do
# echo $check
no=`ps aux|grep mongo|grep configsvr|awk '{print $2;}'|sed -n '1p'`
kill -3 $no
echo "kill $no mongo daemon is ok."
sleep 2
check=`ps aux|grep mongo|grep configsvr|awk '{print $2;}'|wc -l`
echo "stopping mongo,pls waiting..."
done
check=`ps aux|grep mongo|grep shardsvr|awk '{print $2;}'|wc -l`
echo $check
while [ $check -gt 0 ]
do
# echo $check
no=`ps aux|grep mongo|grep shardsvr|awk '{print $2;}'|sed -n '1p'`
kill -3 $no
echo "kill $no mongo daemon is ok."
sleep 2
check=`ps aux|grep mongo|grep shardsvr|awk '{print $2;}'|wc -l`
echo "stopping mongo,pls waiting..."
done
echo "all mongodb stopped!"
九.分片管理
1.listshards:列出所有的Shard
>use admin
>db.runCommand({listshards:1})
2.移除shard
>use admin
>db.runCommand( { removeshard : "shard1/192.168.8.30:27021,192.168.8.31:27021" } )
>db.runCommand( { removeshard : "shard2/192.168.8.30:27022,192.168.8.31:27022" } )
对于移除的分片后,我们再加入相同分片时,会加不进去,可以按如下方法进行:
mongos> use config
switched to db config
mongos> show collections
changelog
chunks
collections
databases
lockpings
locks
mongos
settings
shards
system.indexes
version
mongos> db.shards.find()
{ "_id" : "shard1", "host" : "shard1/192.168.8.30:27021,192.168.8.31:27021,192.168.8.32:27021", "maxSize" : NumberLong(20480) }
{ "_id" : "shard2", "host" : "shard2/192.168.8.30:27022,192.168.8.31:27022,192.168.8.32:27022", "maxSize" : NumberLong(20480) }
要做的就是删除shards表中的信息,把移除的shard键值删除掉!再重新加入shard
如:db.shards.remove({"_id":"shard2"})
3.查看Sharding信息
> printShardingStatus()
PRIMARY> db.system.replset.find()
PRIMARY> rs.isMaster()