mongodb單機配置shard分區

來源:互聯網
上載者:User

標籤:mongodb 分區


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

作業系統:

[[email protected] logs]# cat /etc/issueCentOS release 6.4 (Final)Kernel \r on an \m[[email protected] logs]# uname -r2.6.32-358.el6.x86_64

mongodb版本:

[[email protected] logs]# mongo -versionMongoDB shell version: 2.6.11

1:目錄規劃

[[email protected] opt]# mkdir /opt/{shard1,shard2,configsvr,logs,mongos}[[email protected] opt]# tree /opt//opt/├── configsvr├── logs├── mongos├── shard1└── shard25 directories, 0 files

2:shard1-20000設定檔

[[email protected] opt]# cat /etc/shard1.conf logpath=/opt/logs/shard1.loglogappend=truefork = trueport = 20000dbpath=/opt/shard1pidfilepath = /opt/shard1/20000.pid

啟動shard1-20000

[[email protected] opt]# mongod -f /etc/shard1.conf about to fork child process, waiting until server is ready for connections.forked process: 3341child process started successfully, parent exiting[[email protected] opt]# netstat -ntpl|grep 2000tcp        0      0 0.0.0.0:20000               0.0.0.0:*                   LISTEN      3341/mongod


3:shard2-20001設定檔

[[email protected] opt]# cat /etc/shard2.conf logpath=/opt/logs/shard2.loglogappend=truefork = trueport = 20001dbpath=/opt/shard2pidfilepath = /opt/shard2/20001.pid

啟動shard2-20001

[[email protected] opt]# mongod -f /etc/shard2.conf            about to fork child process, waiting until server is ready for connections.forked process: 3360child process started successfully, parent exiting[[email protected] opt]# netstat -ntpl|grep 20001             tcp        0      0 0.0.0.0:20001               0.0.0.0:*                   LISTEN      3360/mongod

4:configsvr-20002設定檔

[[email protected] opt]# cat /etc/configsvr.conf logpath=/opt/logs/configsvr.loglogappend=truefork = trueport = 20002dbpath=/opt/configsvrpidfilepath = /opt/configsvr/20002.pidconfigsvr = true

啟動configsvr-20002

[[email protected] opt]# mongod -f /etc/configsvr.conf about to fork child process, waiting until server is ready for connections.forked process: 3377child process started successfully, parent exiting[[email protected] opt]# netstat -ntpl|grep 20002tcp        0      0 0.0.0.0:20002               0.0.0.0:*                   LISTEN      3377/mongod

5:mongos-20003設定檔

[[email protected] logs]# cat /etc/mongos.conf logpath=/opt/logs/mongos.loglogappend=truefork = trueport = 20003pidfilepath = /opt/mongos/20003.pidconfigdb = 192.168.75.130:20002

mongos-20003啟動

[[email protected] 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 productionabout to fork child process, waiting until server is ready for connections.forked process: 3452child process started successfully, parent exiting[[email protected] logs]# netstat -ntpl|grep 20003tcp        0      0 0.0.0.0:20003               0.0.0.0:*                   LISTEN      3452/mongos

6:登入mongos

[[email protected] logs]# mongo --port 20003MongoDB shell version: 2.6.11connecting to: 127.0.0.1:20003/testWelcome 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> dbtestmongos> show dbsadmin   (empty)config  0.016GBzxl     0.078GBmongos> use zxlswitched to db zxlmongos> dbzxl

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分區

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.