设为首页 收藏本站
查看: 764|回复: 0

[经验分享] MongoDB分片集群配置

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-8-10 10:18:45 | 显示全部楼层 |阅读模式
一、环境
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)





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-96867-1-1.html 上篇帖子: mongodb部署一个集群 下篇帖子: 【MongoDB】3.0 配置文件相关介绍
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表