MongoDB Sharding实现(在单机上做测试)(Windows版本)

达达伦 2014-05-12 04:56:53
正在学习中~总结一下~也希望能给童鞋们一点帮助。

模拟第一个服务器:
mongod.exe --shardsvr --port 10001 --dbpath D:/nosql/mongodb/db/10001 --logpath D:/nosql/mongodb/logs/10001/log --replSet shard1 --rest
mongod.exe --shardsvr --port 10002 --dbpath D:/nosql/mongodb/db/10002 --logpath D:/nosql/mongodb/logs/10002/log --replSet shard2 --rest

模拟第二个服务器:
mongod.exe --shardsvr --port 20001 --dbpath D:/nosql/mongodb/db/20001 --logpath D:/nosql/mongodb/logs/20001/log --replSet shard1 --rest
mongod.exe --shardsvr --port 20002 --dbpath D:/nosql/mongodb/db/20002 --logpath D:/nosql/mongodb/logs/20002/log --replSet shard2 --rest

获取服务器上所有MongoD配置
mongod.exe --configsvr --port 30001 --dbpath D:/nosql/mongodb/db/30001 --logpath D:/nosql/mongodb/logs/30001/log --rest

启动MongoS
mongos.exe --port 30002 --logpath D:/nosql/mongodb/logs/30002/log --chunkSize 1 --configdb "192.168.91.76:30001"

进入第一个分片上的任一台机:
mongo.exe --port 10001或mongo.exe --port 20001
设置第一个分片:
config = {_id: 'shard1', members: [
{_id: 0, host: '192.168.91.76:10001'},
{_id: 1, host: '192.168.91.76:20001'}
]}
rs.initiate(config)
rs.status()
设置第一个分片主从:
config = {_id: 'shard1', members: [
{_id: 0, host: '192.168.91.76:10001', arbiterOnly: true},
{_id: 1, host: '192.168.91.76:20001'}
]}

进入第二个分片上的任一台机:
mongo.exe --port 10002或mongo.exe --port 20002
设置第二个分片:
config = {_id: 'shard2', members: [
{_id: 0, host: '192.168.91.76:10002'},
{_id: 1, host: '192.168.91.76:20002'}
]}
rs.initiate(config)
rs.status()
设置第二个分片主从:
config = {_id: 'shard2', members: [
{_id: 0, host: '192.168.91.76:10002', arbiterOnly: true},
{_id: 1, host: '192.168.91.76:20002'}
]}

进入mongoS:
mongos.exe --port 30002
添加Shard:
db.runCommand({ addshard:'shard1/192.168.91.76:10001,192.168.91.76:20001' })
db.runCommand({ addshard:'shard2/192.168.91.76:10002,192.168.91.76:10002' })

###Called db.isMaster(), rs.config(), rs.status() on the primary machine.
###Called db.isMaster() on the two secondary machines
###Restarted mongos

启动shard
db.runCommand({ listshards:1 })
db.runCommand({ enablesharding:'test' })
db.runCommand({ shardcollection:'test.data', key:{_id:1} })
db.printShardingStatus()

下一篇写mongodb集群java使用~
...全文
174 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
达达伦 2014-05-12
  • 打赏
  • 举报
回复
引用 2 楼 longtian1213 的回复:
这应该去博客区
原来我发错地方勒。。。怪不得发个帖要积分。。。
达达伦 2014-05-12
  • 打赏
  • 举报
回复
引用 1 楼 defonds 的回复:
转载请注明原文地址
自己写的。谢谢。
S117 2014-05-12
  • 打赏
  • 举报
回复
这应该去博客区
Defonds 2014-05-12
  • 打赏
  • 举报
回复
转载请注明原文地址

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧