MongoDB 3.4 分片与副本集实践笔记

第一步: 准备数据节点

必须添加–shardsvr参数。

mongod--port32770--shardsvr--dbpathD:\db\mongodata\shard\mongodb1--logpathD:\db\mongodata\shard\mongodb1.log

mongod--port32771--shardsvr--dbpathD:\db\mongodata\shard\mongodb2--logpathD:\db\mongodata\shard\mongodb2.log

第二步:config server

3.4要求配置服务器必须也是副本集,好吧,我们需要这样弄一个,我暂时只用一个节点做rs。

启2个配置服务器

mongod--port 26010--configsvr --replSet cs--dbpath D:\db\mongodata\shard\mongodbConfig--logpathD:\db\mongodata\shard\mongodbConfig.log
mongod--port 26011--configsvr --replSet cs--dbpath D:\db\mongodata\shard\mongodbConfig1--logpath D:\db\mongodata\shard\mongodbConfig1.log
用mongo连上 26010 ,初始化一下副本集cs:

mongo 127.0.0.1:26010

use admin
cfg = {
    _id:'cs',
    configsvr:true,
    members:[
        {_id:0,host:'127.0.0.1:26010'}
     ]
};

rs.initiate(cfg);
你应该有多个configserver,所以需要添加第二个
rs.add({_id:1,host:'127.0.0.1:26011'})

第三步:启mongos
mongos--port8001--configdbcs/127.0.0.1:26010,127.0.0.1:26011--logpathD:\db\mongodata\shard\mongos.log
W SHARDING [main] Running a sharded cluster with fewer than 3 config servers should only be 
done for testing purposes and is not recommended for production.

第四步:配置分片节点

同样使用mongo 连上 8001 ,添加片节点,并启用aedata库的分片,然后设置片键

mongo 127.0.0.1:8001
sh.addShard('127.0.0.1:32770')
sh.addShard('127.0.0.1:32771')
sh.status()
sh.enableSharding('aedata')
sh.shardCollection('aedata.ac01_test',{'id':1,'idcard':1})

至此,应该已经可以使用了。

第五步:创建用户

如果要加上鉴权,官网说只要在mongos上操作即可,所以连上 8001,简单地添加一个用户:

use aedata
db.createUser({user:'aedata', pwd:'111111',roles:[{role:'readWrite',db:'aedata'}]})
这样就可以了。

最8001端口测试分片

use aedata
for(var i=10000; i<=20000; i++){ db.ac01_test.save({id:i,idcard:"card"+i}); }
db.ac01_test.count()
db.ac01_test.stats()

如果单独连接32770或者32771,那么数据会直接存储在连接的那个分片上面



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值