MongoDB的分片

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Stubborn_Cow/article/details/52316155

一,分片
1,启动shard、config、mongos节点
(1),启动需要分片的节点
        启动3个mongodb的实例

mongod --shardsvr --dbpath=/usr/local/mongodb/data1 --logpath=/usr/local/mongodb/log/data1.log --port=27081 --fork
mongod --shardsvr --dbpath=/usr/local/mongodb/data2 --logpath=/usr/local/mongodb/log/data2.log --port=27082 --fork
mongod --shardsvr --dbpath=/usr/local/mongodb/data3 --logpath=/usr/local/mongodb/log/data3.log --port=27083 --fork

(2),启动config
        启动1个mongodb的实例,这里其实也是普通的DB,不过是新建了config库,里面保存了路由的信息
mongod --configsvr --dbpath=/usr/local/mongodb/config --logpath=/usr/local/mongodb/log/config.log --port=27001 --fork

(3),启动mongos
         这里启动一个mongos进程,用来做数据的路由,访问的入口,而数据是保存在config里的。
mongos --configdb 10.1.2.197:27001 --logpath=/usr/local/mongodb/log/configdb.log --port=27002 --fork


也可以使用配置文件启动

(1),启动需要分片的节点

(2),启动config

(3),启动mongos

配置文件


2,配置分片
(1),进入mongos

mongo admin --port 27002

(2),用addshard添加到路由器中
db.runCommand({addshard:"10.1.2.197:27081"})
db.runCommand({addshard:"10.1.2.197:27082"})
db.runCommand({addshard:"10.1.2.197:27083"})

(3),指定分片的数据库
db.runCommand({"enablesharding":"userdb"})

(4),指定分片的键
 db.runCommand({shardcollection:"userdb.order",key:{_id:1}})

3,查看状态
db.printShardingStatus()

第一:shards 从图中可以看到:分成三片了,shard0000、shard0001、shard0002。

第二:databases:partitioned表示是否分区,可以看到userdb已分区。

第三:chunks:分成了一段。都在shard0000。


db.order.stats()


4,测试

mongos> for(var i =1;i<10000;i++){ db.order.insert({id:100+i,name:"name"+i,age:i}) }


可以看到,自动分片的结果并不是平均的。

展开阅读全文

mongodb分片奔溃

03-09

因为资源紧张,一开始使用的是两台服务器,最近加了第3台服务器,很多集合的数据已经上亿,遇到的问题:rn1、当数据进行查询的时候,我看了下有42条线程同时在执行查询操作,所有的写入都停止了,有没有办法解决这个问题,在没有查询的情况下,其实每秒的插入量应该到4K+的rninsert query update delete getmore command flushes mapped vsize res faults qr|qw ar|aw netIn netOut conn set repl timern *0 *0 *0 *0 0 6|0 0 2.1G 177.0M 0 0|0 0|0 766b 9k 27 RTR 2017-03-09T14:26:41+08:00rn *0 *0 *0 *0 0 1|0 0 2.1G 177.0M 0 0|0 0|0 143b 8k 27 RTR 2017-03-09T14:26:42+08:00rn *0 *0 *0 *0 0 2|0 0 2.1G 177.0M 0 0|0 0|0 201b 8k 27 RTR 2017-03-09T14:26:43+08:00rn *0 *0 *0 *0 0 1|0 0 2.1G 177.0M 0 0|0 0|0 143b 8k 27 RTR 2017-03-09T14:26:44+08:00rn *0 *0 *0 *0 0 1|0 0 2.1G 177.0M 0 0|0 0|0 143b 8k 27 RTR 2017-03-09T14:26:45+08:00rn *0 *0 *0 *0 0 1|0 0 2.1G 177.0M 0 0|0 0|0 143b 8k 27 RTR 2017-03-09T14:26:46+08:00rn *0 *0 *0 *0 0 1|0 0 2.1G 177.0M 0 0|0 0|0 143b 8k 27 RTR 2017-03-09T14:26:47+08:00rn *0 *0 *0 *0 0 1|0 0 2.1G 177.0M 0 0|0 0|0 143b 8k 27 RTR 2017-03-09T14:26:48+08:00rn *0 *0 *0 *0 0 1|0 0 2.1G 177.0M 0 0|0 0|0 143b 8k 27 RTR 2017-03-09T14:26:49+08:00rn *0 *0 *0 *0 0 1|0 0 2.1G 177.0M 0 0|0 0|0 143b 8k 27 RTR 2017-03-09T14:26:50+08:00rninsert query update delete getmore command flushes mapped vsize res faults qr|qw ar|aw netIn netOut conn set repl timern *0 *0 *0 *0 0 1|0 0 2.1G 177.0M 0 0|0 0|0 143b 8k 27 RTR 2017-03-09T14:26:51+08:00rn *0 *0 *0 *0 0 1|0 0 2.1G 177.0M 0 0|0 0|0 143b 8k 27 RTR 2017-03-09T14:26:52+08:00rn *0 *0 *0 *0 0 2|0 0 2.1G 177.0M 0 0|0 0|0 201b 8k 27 RTR 2017-03-09T14:26:53+08:00rn *0 *0 *0 *0 0 1|0 0 2.1G 177.0M 0 0|0 0|0 143b 8k 27 RTR 2017-03-09T14:26:54+08:00rn *0 *0 *0 *0 0 6|0 0 2.1G 177.0M 0 0|0 0|0 762b 8k 27 RTR 2017-03-09T14:26:55+08:00rn *0 *0 *0 *0 0 1|0 0 2.1G 177.0M 0 0|0 0|0 143b 8k 27 RTR 2017-03-09T14:26:56+08:00rn 1265 *0 *0 *0 0 190|0 0 2.1G 177.0M 0 0|0 0|0 2m 22k 27 RTR 2017-03-09T14:26:57+08:00rnrn2、发现经常出现主分片宕掉,比如昨天:是因为移动分片的时间过长了吗?rnrn2017-03-08T12:23:12.871+0800 I WRITE [conn490] write request to old shard version 1887|1||5896e9b370956d953a47dfe1 waiting for migration commitrn2017-03-08T12:23:12.871+0800 I SHARDING [conn490] Waiting for 10 seconds for the migration critical section to endrn2017-03-08T12:23:12.871+0800 I - [conn76] 论坛

mongodb分片 用户问题

04-15

公司服务器4台rn192.168.2.1 config mongosrn192.168.2.2 shard 分片rn192.168.2.3 shard 分片rn192.168.2.4 shard 分片rnrn192.168.2.1服务器配置文件如下:rn[root@namenode mongodb]# cat conf/mongodb.conf rnport=27018rndbpath=/usr/local/mongodb/data/rnlogpath=/usr/local/mongodb/logs/mongodb.logrnlogappend=truernfork=truern[color=#FF0000]auth=true[/color]rn[root@namenode mongodb]# cat conf/configdb.conf rnport=30000rnconfigdb=192.168.2.1:27018rnlogpath=/usr/local/mongodb/logs/config.logrnlogappend=truernfork=truern[root@namenode mongodb]# rnrn我在没有加入auth=true时rn./bin/mongod -f conf/mongodb.conf rn./bin/mongos -f conf/configdb.conf rnrn可以启动rnrn但是我在192.168.2.1服务器上面的mongodb.conf 上面加入了auth=true后rn./bin/mongos -f conf/configdb.conf 无法启动rnrn提示日志如下:rn***** SERVER RESTARTED *****rnrnrnMon Apr 14 14:50:33.609 [mongosMain] MongoS version 2.4.9 starting: pid=4234 port=30000 64-bit host=namenode.test.com (--help for usrnage)rnMon Apr 14 14:50:33.610 [mongosMain] git version: 52fe0d21959e32a5bdbecdc62057db386e4e029crnMon Apr 14 14:50:33.610 [mongosMain] build info: Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 rnx86_64 BOOST_LIB_VERSION=1_49rnMon Apr 14 14:50:33.610 [mongosMain] options: config: "conf/configdb.conf", configdb: "192.168.2.1:27018", fork: "true", logappenrnd: "true", logpath: "/usr/local/mongodb/logs/config.log", port: 30000 rnMon Apr 14 14:50:33.612 [mongosMain] warning: couldn't check dbhash on config server 192.168.2.1:27018 :: caused by :: ok: 0.0, ernrrmsg: "unauthorized" rnMon Apr 14 14:50:33.613 warning: couldn't check dbhash on config server 192.168.2.1:27018 :: caused by :: ok: 0.0, errmsg: "unautrnhorized" rnMon Apr 14 14:50:33.648 [mongosMain] scoped connection to 192.168.2.1:27018 not being returned to the poolrnMon Apr 14 14:50:33.648 [mongosMain] ERROR: error upgrading config database to v4 :: caused by :: could not load config version for rnupgrade :: caused by :: 11010 count fails: ok: 0.0, errmsg: "unauthorized" rnrnrn***** SERVER RESTARTED *****rnrnrn但是我在mongos的配置文件中加入了auth=true后,提示没有此参数。rnrn项目需要用户及密码访问分片服务器啊。rn这个问题如何解决。请大神给出指点。rn谢谢。rnrn 论坛

没有更多推荐了,返回首页