|
一、环境
mongodb-1 192.168.3.31 mongos
mongodb-2 192.168.3.32 shard1
mongodb-3 192.168.3.33 shard2
mongodb-4 192.168.3.34 config server
二、配置过程清理数据(我这里是之前做过mongodb的其它实验)
1
2
3
| [iyunv@mongodb-2 ~]# /etc/init.d/mongod stop
Stopping mongod: [ OK ]
[iyunv@mongodb-2 ~]# rm -rf /usr/local/mongodb/data/*
|
1.config server配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| #修改配置文件mongodb.conf
[iyunv@mongodb-4 ~]# cat /usr/local/mongodb/conf/mongodb.conf
#bind_ip=0.0.0.0
#port=27017
configsvr=true #开启config server
dbpath=/usr/local/mongodb/data
logpath=/usr/local/mongodb/log/mongodb.log
pidfilepath=/usr/local/mongodb/log/mongodb.pid
directoryperdb=true
logappend=true
oplogSize=1000
fork=true
rest=true
#noprealloc=true
#master=true
|
启动服务
1
2
3
4
5
6
7
| [iyunv@mongodb-4 ~]# /etc/init.d/mongodb start
Starting mongod: [ OK ]
#默认监听在27019,28019上
[iyunv@mongodb-4 ~]# netstat -tunlp |grep mongo
tcp 0 0 0.0.0.0:27019 0.0.0.0:* LISTEN 1493/mongod
tcp 0 0 0.0.0.0:28019 0.0.0.0:* LISTEN 1493/mongod
|
2.mongos配置
1
2
3
4
5
6
7
| [iyunv@mongodb-1 ~]# mongos --configdb=192.168.3.34 --fork --logpath=/tmp/mongos.log
2015-08-07T08:22:56.955+0800 W SHARDING 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: 1644
child process started successfully, parent exiting
[iyunv@mongodb-1 ~]# netstat -tunlp |grep mongo
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 1644/mongos
|
3.shard1配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| #修改配置文件mongodb.conf
[iyunv@mongodb-2 ~]# cat /usr/local/mongodb/conf/mongodb.conf
#bind_ip=0.0.0.0
#port=27017
dbpath=/usr/local/mongodb/data
logpath=/usr/local/mongodb/log/mongodb.log
pidfilepath=/usr/local/mongodb/log/mongodb.pid
directoryperdb=true
logappend=true
oplogSize=1000
fork=true
#noprealloc=true
#master=true
#replSet=testrs0
|
启动服务
1
2
3
4
| [iyunv@mongodb-2 ~]# /etc/init.d/mongod start
Starting mongod: [ OK ]
[iyunv@mongodb-2 ~]# netstat -tunlp |grep mon
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 1729/mongod
|
4.shard2配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| #修改配置文件mongodb.conf
[iyunv@mongodb-3 ~]# cat /usr/local/mongodb/conf/mongodb.conf
#bind_ip=0.0.0.0
#port=27017
dbpath=/usr/local/mongodb/data
logpath=/usr/local/mongodb/log/mongodb.log
pidfilepath=/usr/local/mongodb/log/mongodb.pid
directoryperdb=true
logappend=true
oplogSize=1000
fork=true
#noprealloc=true
#master=true
#replSet=testrs0
|
启动服务
1
2
3
4
| [iyunv@mongodb-3 ~]# /etc/init.d/mongod start
Starting mongod: [ OK ]
[iyunv@mongodb-3 ~]# netstat -tunlp |grep mon
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 2161/mongod
|
5.在mongos上添加shard节点
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
| [iyunv@mongodb-1 ~]# mongo --host 192.168.3.31
MongoDB shell version: 3.0.5
connecting to: 192.168.3.31:27017/test
Server has startup warnings:
mongos> sh.addShard("192.168.3.32") #添加节点1
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> sh.addShard("192.168.3.33") #添加节点2
{ "shardAdded" : "shard0001", "ok" : 1 }
mongos> sh.status() #查看状态
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("55c3fa618addd3d263602205")
}
shards:
{ "_id" : "shard0000", "host" : "192.168.3.32:27017" }
{ "_id" : "shard0001", "host" : "192.168.3.33:27017" }
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
|
对所需对象启用分片功能
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
| #对数据库启用sharding功能
mongos> sh.enableSharding("testdb")
{ "ok" : 1 }
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("55c3fa618addd3d263602205")
}
shards:
{ "_id" : "shard0000", "host" : "192.168.3.32:27017" }
{ "_id" : "shard0001", "host" : "192.168.3.33:27017" }
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : false, "primary" : "shard0000" }
{ "_id" : "testdb", "partitioned" : true, "primary" : "shard0000" } #已生效
#指定需要分片的Collection及索引
mongos> sh.shardCollection("testdb.students",{"age":1})
{ "collectionsharded" : "testdb.students", "ok" : 1 }
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("55c3fa618addd3d263602205")
}
shards:
{ "_id" : "shard0000", "host" : "192.168.3.32:27017" }
{ "_id" : "shard0001", "host" : "192.168.3.33:27017" }
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : false, "primary" : "shard0000" }
{ "_id" : "testdb", "partitioned" : true, "primary" : "shard0000" }
testdb.students
shard key: { "age" : 1 }
chunks:
shard0000 1
{ "age" : { "$minKey" : 1 } } -->> { "age" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0)
|
分片功能已开启,接下来我们手动创建数据来验证是否会分片
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
32
33
| mongos> for (i=1;i<=100000;i++) db.students.insert({name:"student"+i,age:(i%120),address:"China"})
WriteResult({ "nInserted" : 1 })
mongos> db.students.find().count()
100000
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("55c3fa618addd3d263602205")
}
shards:
{ "_id" : "shard0000", "host" : "192.168.3.32:27017" }
{ "_id" : "shard0001", "host" : "192.168.3.33:27017" }
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
1 : Success
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : false, "primary" : "shard0000" }
{ "_id" : "testdb", "partitioned" : true, "primary" : "shard0000" }
testdb.students
shard key: { "age" : 1 }
chunks:
shard0000 2 #2个分片
shard0001 1 #1个分片
{ "age" : { "$minKey" : 1 } } -->> { "age" : 2 } on : shard0000 Timestamp(2, 1)
{ "age" : 2 } -->> { "age" : 10 } on : shard0000 Timestamp(1, 2)
{ "age" : 10 } -->> { "age" : { "$maxKey" : 1 } } on : shard0001 Timestamp(2, 0)
|
|
|