MongoDB的sharding功能(转)
时间:2010-10-21 来源:不得闲
[[email protected] ~/mongodata]$ mkdir 27020 27021 27022
[[email protected] ~/mongodata]$ ls
27020 27021 27022
[[email protected] ~/mongodb/bin]$ ./mongod --dbpath /home/falcon/mongodata/27020 --port 27020 > /home/falcon/mongodata/27020.log &
[[email protected] ~/mongodb/bin]$ ./mongod --dbpath /home/falcon/mongodata/27021 --port 27021 > /home/falcon/mongodata/27021.log &
[[email protected] ~/mongodb/bin]$ ./mongod --dbpath /home/falcon/mongodata/27022 --port 27022 > /home/falcon/mongodata/27022.log &
falcon 2612 1 0 20:15 ? 00:00:00 ./mongod --dbpath /home/falcon/mongodata/27020 --port 27020
falcon 2619 1 0 20:15 ? 00:00:00 ./mongod --dbpath /home/falcon/mongodata/27021 --port 27021
falcon 2625 1 0 20:15 ? 00:00:00 ./mongod --dbpath /home/falcon/mongodata/27022 --port 27022
falcon 2658 1 0 20:15 ? 00:00:00 ./mongos --configdb localhost:27022
falcon 2804 2772 0 20:31 pts/0 00:00:00 bin/mongo
falcon 2847 2812 0 20:55 pts/2 00:00:00 grep mongo
[[email protected] ~/mongodb/bin]$
[[email protected] ~/mongodb/bin]$ netstat -an -t
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:10022 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:587 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:27020 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:27021 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:27022 0.0.0.0:* LISTEN
......
tcp 0 0 0.0.0.0:28020 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:28021 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:28022 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
........
MongoDB shell version: 1.2.4-
url: test
connecting to: test
type "help" for help
> show dbs
admin
config
Local
switched to db admin
> db.runCommand( { addshard : "localhost:27020", allowLocal : true } )
{"ok" : 1 , "added" : "localhost:27020"}
> db.runCommand( { addshard : "localhost:27021", allowLocal : true } )
{"ok" : 1 , "added" : "localhost:27021"}
> db.runCommand({listshards:1}); 查看shard节点列表
{
"shards" : [
{
"_id" : ObjectId("4b9cd380c33000afad27718e"),
"host" : "localhost:27020"
},
{
"_id" : ObjectId("4b9cd381c33000afad27718f"),
"host" : "localhost:27021"
}
],
"ok" : 1
}
> config = config.getSisterDB("config")
> user001=db.getSisterDB("user001");
user001
> db.runCommand({enablesharding:"user001"})
{ "ok" : 1 }
> db.printShardingStatus();
--- Sharding Status ---
sharding version: { "_id" : ObjectId("4b9cd354c33000afad27718d"), "version" : 2 }
shards:
{ "_id" : ObjectId("4b9cd380c33000afad27718e"), "host" : "localhost:27020" }
{ "_id" : ObjectId("4b9cd381c33000afad27718f"), "host" : "localhost:27021" }
databases:
{ "name" : "admin", "partitioned" : false, "primary" : "localhost:27022", "_id" : ObjectId("4b9cd3776693dcfa468dec13") }
{ "name" : "user001", "partitioned" : true, "primary" : "localhost:27021", "_id" : ObjectId("4b9cde866693dcfa468dec17") }
my chunks
switched to db user001
> db.createCollection("user_001")
{ "ok" : 1 }
> show collections
system.indexes
user_001
> db.user_001.insert({uid:1,username:"Falcon.C",sex:"男",age:25});
> db.user_001.find();
{ "_id" : ObjectId("4b9ce1a6c84d7f20576c4df1"), "uid" : 1, "username" : "Falcon.C", "sex" : "男", "age" : 25 }
.:
27020 27021 27022 mongos.log
./27020:
27020.log mongod.lock test.0 test.1 test.ns _tmp
./27020/_tmp:
./27021:
27021.log mongod.lock _tmp user.0 user001.0 user001.1 user001.ns user.1 user.ns
./27021/_tmp:
./27022:
27022.log config.0 config.ns mongod.lock mongos.log _tmp
./27022/_tmp:
[[email protected] ~/mongodata]$
{ "isdbgrid" : 1, "hostname" : "www.fwphp.cn", "ok" : 1 }
> db.$cmd.findOne({ismaster:1});
{ "ismaster" : 1, "msg" : "isdbgrid", "ok" : 1 }
> printShardingStatus(db.getSisterDB("config"))
--- Sharding Status ---
sharding version: { "_id" : ObjectId("4b9cd354c33000afad27718d"), "version" : 2 }
shards:
{ "_id" : ObjectId("4b9cd380c33000afad27718e"), "host" : "localhost:27020" }
{ "_id" : ObjectId("4b9cd381c33000afad27718f"), "host" : "localhost:27021" }
databases:
{ "name" : "admin", "partitioned" : false, "primary" : "localhost:27022", "_id" : ObjectId("4b9cd3776693dcfa468dec13") }
my chunks
{ "name" : "user001", "partitioned" : true, "primary" : "localhost:27021", "_id" : ObjectId("4b9cde866693dcfa468dec17") }
my chunks
> use admin
switched to db admin
> db.runCommand({netstat:1})
{ "configserver" : "localhost:27022", "isdbgrid" : 1, "ok" : 1 }
>
[[email protected] ~/mongodata]$ ls
27020 27021 27022
[[email protected] ~/mongodb/bin]$ ./mongod --dbpath /home/falcon/mongodata/27020 --port 27020 > /home/falcon/mongodata/27020.log &
[[email protected] ~/mongodb/bin]$ ./mongod --dbpath /home/falcon/mongodata/27021 --port 27021 > /home/falcon/mongodata/27021.log &
[[email protected] ~/mongodb/bin]$ ./mongod --dbpath /home/falcon/mongodata/27022 --port 27022 > /home/falcon/mongodata/27022.log &
启动mongos时,默认开启了27017端口
[[email protected] ~/mongodb/bin]$ ./mongos --configdb localhost:27022 > /home/falcon/mongodata/config.log &检查是否启动
[[email protected] ~/mongodb/bin]$ ps -ef|grep mongofalcon 2612 1 0 20:15 ? 00:00:00 ./mongod --dbpath /home/falcon/mongodata/27020 --port 27020
falcon 2619 1 0 20:15 ? 00:00:00 ./mongod --dbpath /home/falcon/mongodata/27021 --port 27021
falcon 2625 1 0 20:15 ? 00:00:00 ./mongod --dbpath /home/falcon/mongodata/27022 --port 27022
falcon 2658 1 0 20:15 ? 00:00:00 ./mongos --configdb localhost:27022
falcon 2804 2772 0 20:31 pts/0 00:00:00 bin/mongo
falcon 2847 2812 0 20:55 pts/2 00:00:00 grep mongo
[[email protected] ~/mongodb/bin]$
[[email protected] ~/mongodb/bin]$ netstat -an -t
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:10022 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:587 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:27020 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:27021 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:27022 0.0.0.0:* LISTEN
......
tcp 0 0 0.0.0.0:28020 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:28021 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:28022 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
........
看到以上信息证明mongodb启动完整,对于开启的28020、28021、28022是对于的http接口
[[email protected] ~/mongodb/bin]$ ./mongo 默认连接到mongos上MongoDB shell version: 1.2.4-
url: test
connecting to: test
type "help" for help
> show dbs
admin
config
Local
加入shard节点
> use adminswitched to db admin
> db.runCommand( { addshard : "localhost:27020", allowLocal : true } )
{"ok" : 1 , "added" : "localhost:27020"}
> db.runCommand( { addshard : "localhost:27021", allowLocal : true } )
{"ok" : 1 , "added" : "localhost:27021"}
> db.runCommand({listshards:1}); 查看shard节点列表
{
"shards" : [
{
"_id" : ObjectId("4b9cd380c33000afad27718e"),
"host" : "localhost:27020"
},
{
"_id" : ObjectId("4b9cd381c33000afad27718f"),
"host" : "localhost:27021"
}
],
"ok" : 1
}
新建自动切片的库user001:
> config = connect("localhost:27022")> config = config.getSisterDB("config")
> user001=db.getSisterDB("user001");
user001
> db.runCommand({enablesharding:"user001"})
{ "ok" : 1 }
> db.printShardingStatus();
--- Sharding Status ---
sharding version: { "_id" : ObjectId("4b9cd354c33000afad27718d"), "version" : 2 }
shards:
{ "_id" : ObjectId("4b9cd380c33000afad27718e"), "host" : "localhost:27020" }
{ "_id" : ObjectId("4b9cd381c33000afad27718f"), "host" : "localhost:27021" }
databases:
{ "name" : "admin", "partitioned" : false, "primary" : "localhost:27022", "_id" : ObjectId("4b9cd3776693dcfa468dec13") }
{ "name" : "user001", "partitioned" : true, "primary" : "localhost:27021", "_id" : ObjectId("4b9cde866693dcfa468dec17") }
my chunks
我们来在user001中新建表,插入数据
> use user001switched to db user001
> db.createCollection("user_001")
{ "ok" : 1 }
> show collections
system.indexes
user_001
> db.user_001.insert({uid:1,username:"Falcon.C",sex:"男",age:25});
> db.user_001.find();
{ "_id" : ObjectId("4b9ce1a6c84d7f20576c4df1"), "uid" : 1, "username" : "Falcon.C", "sex" : "男", "age" : 25 }
我们来看看user001库被分配到了哪个shard上
[[email protected] ~/mongodata]$ ls -R.:
27020 27021 27022 mongos.log
./27020:
27020.log mongod.lock test.0 test.1 test.ns _tmp
./27020/_tmp:
./27021:
27021.log mongod.lock _tmp user.0 user001.0 user001.1 user001.ns user.1 user.ns
./27021/_tmp:
./27022:
27022.log config.0 config.ns mongod.lock mongos.log _tmp
./27022/_tmp:
[[email protected] ~/mongodata]$
从以上的文件可以看出,user001被分配到了27021的shard上了,但是通过mongos路由,我们并感觉不到是数据存放在哪个shard的chunk上
Sharding的管理命令
{ "isdbgrid" : 1, "hostname" : "www.fwphp.cn", "ok" : 1 }
> db.$cmd.findOne({ismaster:1});
{ "ismaster" : 1, "msg" : "isdbgrid", "ok" : 1 }
> printShardingStatus(db.getSisterDB("config"))
--- Sharding Status ---
sharding version: { "_id" : ObjectId("4b9cd354c33000afad27718d"), "version" : 2 }
shards:
{ "_id" : ObjectId("4b9cd380c33000afad27718e"), "host" : "localhost:27020" }
{ "_id" : ObjectId("4b9cd381c33000afad27718f"), "host" : "localhost:27021" }
databases:
{ "name" : "admin", "partitioned" : false, "primary" : "localhost:27022", "_id" : ObjectId("4b9cd3776693dcfa468dec13") }
my chunks
{ "name" : "user001", "partitioned" : true, "primary" : "localhost:27021", "_id" : ObjectId("4b9cde866693dcfa468dec17") }
my chunks
> use admin
switched to db admin
> db.runCommand({netstat:1})
{ "configserver" : "localhost:27022", "isdbgrid" : 1, "ok" : 1 }
>
参考信息:http://www.mongodb.org/display/DOCS/Sharding
相关阅读 更多 +