mongodb单机配置shard分片
#####################################################操作系统:
1
2
3
4
5
# cat /etc/issue
CentOS release 6.4 (Final)
Kernel \r on an \m
# uname -r
2.6.32-358.el6.x86_64
mongodb版本:
1
2
# mongo -version
MongoDB shell version: 2.6.11
1:目录规划
1
2
3
4
5
6
7
8
9
# mkdir /opt/{shard1,shard2,configsvr,logs,mongos}
# tree /opt/
/opt/
├── configsvr
├── logs
├── mongos
├── shard1
└── shard2
5 directories, 0 files
2:shard1-20000配置文件
1
2
3
4
5
6
7
# cat /etc/shard1.conf
logpath=/opt/logs/shard1.log
logappend=true
fork = true
port = 20000
dbpath=/opt/shard1
pidfilepath = /opt/shard1/20000.pid
启动shard1-20000
1
2
3
4
5
6
# mongod -f /etc/shard1.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3341
child process started successfully, parent exiting
# netstat -ntpl|grep 2000
tcp 0 0 0.0.0.0:20000 0.0.0.0:* LISTEN 3341/mongod
3:shard2-20001配置文件
1
2
3
4
5
6
7
# cat /etc/shard2.conf
logpath=/opt/logs/shard2.log
logappend=true
fork = true
port = 20001
dbpath=/opt/shard2
pidfilepath = /opt/shard2/20001.pid
启动shard2-20001
1
2
3
4
5
6
# mongod -f /etc/shard2.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3360
child process started successfully, parent exiting
# netstat -ntpl|grep 20001
tcp 0 0 0.0.0.0:20001 0.0.0.0:* LISTEN 3360/mongod
4:configsvr-20002配置文件
1
2
3
4
5
6
7
8
# cat /etc/configsvr.conf
logpath=/opt/logs/configsvr.log
logappend=true
fork = true
port = 20002
dbpath=/opt/configsvr
pidfilepath = /opt/configsvr/20002.pid
configsvr = true
启动configsvr-20002
1
2
3
4
5
6
# mongod -f /etc/configsvr.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3377
child process started successfully, parent exiting
# netstat -ntpl|grep 20002
tcp 0 0 0.0.0.0:20002 0.0.0.0:* LISTEN 3377/mongod
5:mongos-20003配置文件
1
2
3
4
5
6
7
# cat /etc/mongos.conf
logpath=/opt/logs/mongos.log
logappend=true
fork = true
port = 20003
pidfilepath = /opt/mongos/20003.pid
configdb = 192.168.75.130:20002
mongos-20003启动
1
2
3
4
5
6
7
# mongos -f /etc/mongos.conf
2015-12-15T09:57:42.222+0800 warning: running with 1 config server should be done only for testing purposes and is not recommended for production
about to fork child process, waiting until server is ready for connections.
forked process: 3452
child process started successfully, parent exiting
# netstat -ntpl|grep 20003
tcp 0 0 0.0.0.0:20003 0.0.0.0:* LISTEN 3452/mongos
6:登录mongos
1
2
3
4
5
6
7
8
9
# mongo --port 20003
MongoDB shell version: 2.6.11
connecting to: 127.0.0.1:20003/test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
查看状态
1
2
3
4
5
6
7
8
9
10
11
12
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 4,
"minCompatibleVersion" : 4,
"currentVersion" : 5,
"clusterId" : ObjectId("566f73969b960c2046e49eba")
}
shards:
databases:
{"_id" : "admin","partitioned" : false,"primary" : "config" }
7:添加shard分片机器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
mongos> sh.addShard("192.168.75.130:20000")
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> sh.addShard("192.168.75.130:20001")
{ "shardAdded" : "shard0001", "ok" : 1 }
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 4,
"minCompatibleVersion" : 4,
"currentVersion" : 5,
"clusterId" : ObjectId("566f73969b960c2046e49eba")
}
shards:
{"_id" : "shard0000","host" : "192.168.75.130:20000" }
{"_id" : "shard0001","host" : "192.168.75.130:20001" }
databases:
{"_id" : "admin","partitioned" : false,"primary" : "config" }
启用shard分片的库名字为'zxl',即为库
1
2
mongos> sh.enableSharding("zxl")
{ "ok" : 1 }
设置集合的名字以及字段,默认自动建立索引,zxl库,haha集合
1
2
mongos> sh.shardCollection("zxl.haha",{age: 1, name: 1})
{ "collectionsharded" : "zxl.haha", "ok" : 1 }
查看状态
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 4,
"minCompatibleVersion" : 4,
"currentVersion" : 5,
"clusterId" : ObjectId("566f73969b960c2046e49eba")
}
shards:
{"_id" : "shard0000","host" : "192.168.75.130:20000" }
{"_id" : "shard0001","host" : "192.168.75.130:20001" }
databases:
{"_id" : "admin","partitioned" : false,"primary" : "config" }
{"_id" : "test","partitioned" : false,"primary" : "shard0000" }
{"_id" : "zxl","partitioned" : true,"primary" : "shard0000" }
zxl.haha
shard key: { "age" : 1, "name" : 1 }
chunks:
shard0000 1
{ "age" : { "$minKey" : 1 }, "name" : { "$minKey" : 1 } } -->> { "age" : { "$maxKey" : 1 }, "name" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0)
mongos> db
test
mongos> show dbs
admin (empty)
config0.016GB
zxl 0.078GB
mongos> use zxl
switched to db zxl
mongos> db
zxl
8:模拟在haha集合中插入数据
1
2
mongos> for (i=1;i<=10000;i++) db.haha.insert({name: "user"+i, age: (i%150)})
WriteResult({ "nInserted" : 1 })
插入后的数据查看状态
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 4,
"minCompatibleVersion" : 4,
"currentVersion" : 5,
"clusterId" : ObjectId("566f73969b960c2046e49eba")
}
shards:
{"_id" : "shard0000","host" : "192.168.75.130:20000" }
{"_id" : "shard0001","host" : "192.168.75.130:20001" }
databases:
{"_id" : "admin","partitioned" : false,"primary" : "config" }
{"_id" : "test","partitioned" : false,"primary" : "shard0000" }
{"_id" : "zxl","partitioned" : true,"primary" : "shard0000" }
zxl.haha
shard key: { "age" : 1, "name" : 1 }
chunks:
shard0001 2
shard0000 1
{ "age" : { "$minKey" : 1 }, "name" : { "$minKey" : 1 } } -->> { "age" : 1, "name" : "user1" } on : shard0001 Timestamp(2, 0)
{ "age" : 1, "name" : "user1" } -->> { "age" : 149, "name" : "user899" } on : shard0000 Timestamp(3, 1)
{ "age" : 149, "name" : "user899" } -->> { "age" : { "$maxKey" : 1 }, "name" : { "$maxKey" : 1 } } on : shard0001 Timestamp(3, 0)
9:查看年龄大于88
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
mongos> db.haha.find({age: {$gt: 88}})
{ "_id" : ObjectId("566f7509ebd7512bf6df23f7"), "name" : "user899", "age" : 149 }
{ "_id" : ObjectId("566f750aebd7512bf6df24e7"), "name" : "user1139", "age" : 89 }
{ "_id" : ObjectId("566f7511ebd7512bf6df439b"), "name" : "user8999", "age" : 149 }
{ "_id" : ObjectId("566f750aebd7512bf6df257d"), "name" : "user1289", "age" : 89 }
{ "_id" : ObjectId("566f7511ebd7512bf6df4431"), "name" : "user9149", "age" : 149 }
{ "_id" : ObjectId("566f750aebd7512bf6df2613"), "name" : "user1439", "age" : 89 }
{ "_id" : ObjectId("566f7511ebd7512bf6df44c7"), "name" : "user9299", "age" : 149 }
{ "_id" : ObjectId("566f750aebd7512bf6df26a9"), "name" : "user1589", "age" : 89 }
{ "_id" : ObjectId("566f7511ebd7512bf6df455d"), "name" : "user9449", "age" : 149 }
{ "_id" : ObjectId("566f750aebd7512bf6df273f"), "name" : "user1739", "age" : 89 }
{ "_id" : ObjectId("566f7511ebd7512bf6df45f3"), "name" : "user9599", "age" : 149 }
{ "_id" : ObjectId("566f750aebd7512bf6df27d5"), "name" : "user1889", "age" : 89 }
{ "_id" : ObjectId("566f7511ebd7512bf6df4689"), "name" : "user9749", "age" : 149 }
{ "_id" : ObjectId("566f750aebd7512bf6df286b"), "name" : "user2039", "age" : 89 }
{ "_id" : ObjectId("566f7511ebd7512bf6df471f"), "name" : "user9899", "age" : 149 }
{ "_id" : ObjectId("566f750bebd7512bf6df2901"), "name" : "user2189", "age" : 89 }
{ "_id" : ObjectId("566f750bebd7512bf6df2997"), "name" : "user2339", "age" : 89 }
{ "_id" : ObjectId("566f7509ebd7512bf6df2163"), "name" : "user239", "age" : 89 }
{ "_id" : ObjectId("566f750bebd7512bf6df2a2d"), "name" : "user2489", "age" : 89 }
{ "_id" : ObjectId("566f750bebd7512bf6df2ac3"), "name" : "user2639", "age" : 89 }
Type "it" for more
以上就是mongodb分片操作
不错不错适合小白入门
页:
[1]