码迷,mamicode.com
首页 > 数据库 > 详细

mongodb单机配置shard分片

时间:2016-03-06 23:53:27      阅读:823      评论:0      收藏:0      [点我收藏+]

标签:mongodb 分片


#####################################################

操作系统:

[root@zxl-node3 logs]# cat /etc/issue
CentOS release 6.4 (Final)
Kernel \r on an \m
[root@zxl-node3 logs]# uname -r
2.6.32-358.el6.x86_64

mongodb版本:

[root@zxl-node3 logs]# mongo -version
MongoDB shell version: 2.6.11

1:目录规划

[root@zxl-node3 opt]# mkdir /opt/{shard1,shard2,configsvr,logs,mongos}
[root@zxl-node3 opt]# tree /opt/
/opt/
├── configsvr
├── logs
├── mongos
├── shard1
└── shard2
5 directories, 0 files

2:shard1-20000配置文件

[root@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

[root@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
[root@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配置文件

[root@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

[root@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
[root@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配置文件

[root@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

[root@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
[root@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配置文件

[root@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启动

[root@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
[root@zxl-node3 logs]# netstat -ntpl|grep 20003
tcp        0      0 0.0.0.0:20003               0.0.0.0:*                   LISTEN      3452/mongos

6:登录mongos

[root@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

查看状态

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分片机器

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‘,即为库

mongos> sh.enableSharding("zxl")
{ "ok" : 1 }

设置集合的名字以及字段,默认自动建立索引,zxl库,haha集合

mongos> sh.shardCollection("zxl.haha",{age: 1, name: 1})
{ "collectionsharded" : "zxl.haha", "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" }
        {  "_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集合中插入数据

mongos> for (i=1;i<=10000;i++) db.haha.insert({name: "user"+i, age: (i%150)})
WriteResult({ "nInserted" : 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" }
        {  "_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

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分片操

    


本文出自 “村里的男孩” 博客,请务必保留此出处http://noodle.blog.51cto.com/2925423/1748149

mongodb单机配置shard分片

标签:mongodb 分片

原文地址:http://noodle.blog.51cto.com/2925423/1748149

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!