mongodb sharding 配置

这篇博客详细介绍了MongoDB的分片配置过程,包括启动各个节点、添加分片、设置分片键以及数据迁移。同时,还提到了如何通过mongostat监控分片状态,并展示了数据导入导出的方法,对于理解MongoDB分片管理和运维具有实践指导意义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

启动
202.173.8.57 (Shard1)
./mongod --port 30001 --dbpath /home/mongodb/workspace/db/shard1 --logpath /home/mongodb/workspace/db/log/shard1.log --shardsvr --logappend --fork

202.173.8.59 (Shard2)
./mongod --port 30002 --dbpath /home/mongodb/workspace/db/shard2 --logpath /home/mongodb/workspace/db/log/shard2.log --shardsvr --logappend --fork

202.173.8.57 (Config)
./mongod --port 30003 --dbpath /home/mongodb/workspace/db/config --logpath /home/mongodb/workspace/db/log/config.log --configsvr --logappend --fork

202.173.8.57 (Mongos)
./mongos --port 30004  --logpath /home/mongodb/workspace/db/log/mongos.log  --logappend --configdb "202.173.8.57:30003"  --chunkSize 800 --fork



202.173.8.59 (Mongos)
./mongos --port 30005  --logpath /home/mongodb/workspace/db/log/mongos.log  --logappend --configdb "202.173.8.57:30003"  --chunkSize 800 --fork

#login set shard
./mongo 202.173.9.57:30004/admin
配置shard
db.runCommand( { addshard : "202.173.8.57:30001" } );
db.runCommand( { addshard : "202.173.8.59:30002" } );

指定用户为分片
db.runCommand( { enablesharding : "mongodb_test" } );
指定Key 1 为order asc -1 为desc
db.runCommand( { shardcollection : "mongodb_test.student_0", key : { stuId: 1,name:1 } } )
db.runCommand( { shardcollection : "mongodb_test.student_1", key : { stuId: 1,name:1 } } )
db.runCommand( { shardcollection : "mongodb_test.student_2", key : { stuId: 1,name:1 } } )
db.runCommand( { shardcollection : "mongodb_test.student_3", key : { stuId: 1,name:1 } } )
db.runCommand( { shardcollection : "mongodb_test.student_4", key : { stuId: 1,name:1 } } )

for  (var  i  =  1;  i  <=  500000;  i++)  db.student_0.insert({stuId:i,  name:"wangwenlong"+i,  addr:"Beijing",
country:"China"})



更新到其它机器,更新配置信息
mongos> use config
switched to db config
mongos> db.shards.find()
{ "_id" : "shard0000", "host" : "202.173.8.57:30001" }
{ "_id" : "shard0001", "host" : "202.173.8.59:30002" }






查看统计数据
./mongostat -host ip -port 20001  连接sharding的端口
连接mongos
./mongo ip:端口/admin                   
./mongo ip:端口/kx_monitor


use admin

 
 


数据已经导入测试环境(17号至19号的快照数据),导入方法如下:
 
1. ISODate("2013-02-17T08:00:00.000Z").valueOf() ------------------------> xxxxxxxxxxxxxxxxxxx(时间戳)
    ISODate("2013-02-19T08:00:00.000Z").valueOf() ------------------------> xxxxxxxxxxxxxxxxxxx(时间戳)
 
2 ./mongoexport -h 202.173.8.37 --port 30000 -d km_mongodb -c snapshot_0 -q '{executeTime:{$gte:new Date(xxxxxxxxxxxxxx),$lt:new Date(xxxxxxxxxxxxxx)}}' -o 20130322_snapshot_0.json
   ./mongoexport -h 202.173.8.37 --port 30000 -d km_mongodb -c snapshotResult_0 -q '{executeTime:{$gte:new Date(xxxxxxxxxxxxxx),$lt:new Date(xxxxxxxxxxxxxx)}}' -o 20130322_snapshotResult_0.json
 
3 ./mongoimport -h 202.173.8.57 --port 30000 -d km_mongodb -c snapshotResult_0 --file 20130322_snapshotResult_0.json
   ./mongoimport -h 202.173.8.57 --port 30000 -d km_mongodb -c snapshot_0 --file 20130322_snapshot_0.json
   
   
   ./mongoimport -h 202.173.8.57 --port 30000 -d km_mongodb -c snapshot_0 --file 201209070202019275_snapshot_5.json


mongos> ISODate("2013-02-05T23:59:00Z").valueOf()
1360108740000
mongos> ISODate("2013-02-19T00:01:00Z").valueOf()
1361232060000



./mongoimport -h 202.173.8.57 --port 30000 -d km_mongodb -c snapshot_5 --file 20130228/snapshot/05_19_snapshot_5.json


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值