设为首页 收藏本站
查看: 1293|回复: 1

[经验分享] mongodb单机配置shard分片

[复制链接]

尚未签到

发表于 2016-3-7 08:38:21 | 显示全部楼层 |阅读模式
#####################################################
操作系统:
1
2
3
4
5
[iyunv@zxl-node3 logs]# cat /etc/issue
CentOS release 6.4 (Final)
Kernel \r on an \m
[iyunv@zxl-node3 logs]# uname -r
2.6.32-358.el6.x86_64



mongodb版本:
1
2
[iyunv@zxl-node3 logs]# mongo -version
MongoDB shell version: 2.6.11



1:目录规划
1
2
3
4
5
6
7
8
9
[iyunv@zxl-node3 opt]# mkdir /opt/{shard1,shard2,configsvr,logs,mongos}
[iyunv@zxl-node3 opt]# tree /opt/
/opt/
├── configsvr
├── logs
├── mongos
├── shard1
└── shard2
5 directories, 0 files



2:shard1-20000配置文件
1
2
3
4
5
6
7
[iyunv@zxl-node3 opt]# 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
[iyunv@zxl-node3 opt]# 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
[iyunv@zxl-node3 opt]# 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
[iyunv@zxl-node3 opt]# 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
[iyunv@zxl-node3 opt]# 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
[iyunv@zxl-node3 opt]# 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
[iyunv@zxl-node3 opt]# 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
[iyunv@zxl-node3 opt]# 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
[iyunv@zxl-node3 opt]# 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
[iyunv@zxl-node3 logs]# 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
[iyunv@zxl-node3 logs]# 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
[iyunv@zxl-node3 logs]# 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
[iyunv@zxl-node3 logs]# 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)
config  0.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、欢迎大家加入本站运维交流群:群②: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-187304-1-1.html 上篇帖子: MongoDB 表结构分析工具介绍 -- Variety 下篇帖子: mongodb3.2配置文件yaml格式 单机
累计签到:14 天
连续签到:1 天
发表于 2017-8-23 09:53:45 | 显示全部楼层
不错不错适合小白入门

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

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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