MongoDB 最近遇到的几个小问题

(1)连接数据库时报错

ERROR Topshelf.Hosts.ConsoleRunHost.Run 1 
An exception occurred
System.TimeoutException: A timeout occured after 30000ms selecting a server using CompositeServerSelector

错误原因是和转义字符有关。连接字符串使用的URL格式,所以其中的密码中的% 需要转义。

知识扩展:

连接mongo使用URI有特殊字符 '@' 或者":"或者‘%’, 连接会报错,需要进行转义。

解决方法:

把 @ 换成 %40 

把 : 换成 %3A

把 % 换成 %25

 (2)时间字段的范围查询,请注意时间字段的类型。

例如,明看到集合中指定时间段内有数据,但是Count结果还是显示为0。

最早的一笔数据是20170816,但是使用以下语句查询2017-08-14 到 2018-08-18时间段内的文档数据为0.

 

问题在哪儿哪? 

查看发现query.time字段类型是 string.

我们将查询语句的条件格式转换为字段的存储格式,就OK了。

 

所以,在设计集合模式时,要特别注意存储日期时间的字段的类型,建议为Date。另外,查询时也要小心,防止数据异常。

(3)副本集添加节点时报错

错误信息:

{
    "ok" : 0,
    "errmsg" : "Quorum check failed because not enough voting nodes responded; required 2 but only the following 1 voting nodes responded: 172.XXX.XXX.XXX:27017; the following nodes did not respond affirmatively: 172.XXX.XXX.XX:27017 failed with No route to host",
    "code" : 74,
    "codeName" : "NodeNotFound"
}

错误原因是:防火墙没关闭 导致

(4)将常规集合设置为固定集合,既有的索引丢失,需要重建。

db.runCommand({"convertToCapped": "集合名字", size: XXXXXX,"max":XXXXX});

设置为固定集合后,原来的索引都丢失了,需要谨记 。

(曾经的一个转换性能测试,及转为固定集合的耗时:1001 W数据,16.8 G 约耗时 6分钟)

(5)mongodb 对字段中有超过 1024 字节的不会建立索引

MongoDB will not create an index on a collection if the index entry for an existing document exceeds the index key limit (1024 bytes). You can however create a hashed index or text index instead:

除了上面的介绍外,还可以修改启动配置参数 ailIndexKeyTooLong。

(6)配置分片的复制集需,在启动的配置文件中需 指定 shardsvr参数。否则,在启动数据库分片时报错。

错误信息如下:

在config文件中,添加 shardsvr=true 即可。

重启服务,再次启动启动分片,执行OK.

 

(7)MongoDB 服务异常关闭, the process must exit and restart

查看实例的运行log,报错信息如下:

2019-06-16T04:54:54.830+0800 E STORAGE  [thread2] WiredTiger error (28) [1560632094:830160][160893:0x7f2b30e80700], file:WiredTiger.wt, WT_SESSION.checkpoint: /data/mongodb/XXXXXXXXXXX/data/db/WiredTiger.turtle.set: handle-write: pwrite: failed to write 1015 bytes at offset 0: No space left on device
2019-06-16T04:54:54.830+0800 E STORAGE  [thread2] WiredTiger error (28) [1560632094:830727][160893:0x7f2b30e80700], file:WiredTiger.wt, WT_SESSION.checkpoint: /data/mongodb/XXXXXXXXXXX/data/db/WiredTiger.turtle.set: handle-write: pwrite: failed to write 1015 bytes at offset 0: No space left on device
2019-06-16T04:54:54.830+0800 E STORAGE  [thread2] WiredTiger error (0) [1560632094:830818][160893:0x7f2b30e80700], file:WiredTiger.wt, WT_SESSION.checkpoint: WiredTiger.turtle: encountered an illegal file format or internal value
2019-06-16T04:54:54.830+0800 E STORAGE  [thread2] WiredTiger error (-31804) [1560632094:830848][160893:0x7f2b30e80700], file:WiredTiger.wt, WT_SESSION.checkpoint: the process must exit and restart: WT_PANIC: WiredTiger library panic
2019-06-16T04:54:54.830+0800 I -        [thread2] Fatal Assertion 28558 at src/mongo/db/storage/wiredtiger/wiredtiger_util.cpp 361
2019-06-16T04:54:54.830+0800 I -        [thread2] 

***aborting after fassert() failure


2019-06-16T04:54:54.875+0800 F -        [thread2] Got signal: 6 (Aborted).

 0x56500f890ee1 0x56500f88ffd9 0x56500f8904bd 0x7f2b378945e0 0x7f2b374f71f7 0x7f2b374f88e8 0x56500eb24d79 0x56500f59e366 0x56500eb2f329 0x56500eb2f545 0x56500eb2f79d 0x565010234400 0x565010232516 0x565010230e9f 0x5650102315df 0x56501027ee54 0x56501027fbd6 0x565010280f5b 0x565010281219 0x56501026e661 0x5650101e646d 0x7f2b3788ce25 0x7f2b375ba34d
----- BEGIN BACKTRACE -----
{"backtrace":[{"b":"56500E313000","o":"157DEE1","s":"_ZN5mongo15printStackTraceERSo"},{"b":"56500E313000","o":"157CFD9"},{"b":"56500E313000","o":"157D4BD"},{"b":"7F2B37885000","o":"F5E0"},{"b":"7F2B374C2000","o":"351F7","s":"gsignal"},{"b":"7F2B374C2000","o":"368E8","s":"abort"},{"b":"56500E313000","o":"811D79","s":"_ZN5mongo32fassertFailedNoTraceWithLocationEiPKcj"},{"b":"56500E313000","o":"128B366"},{"b":"56500E313000","o":"81C329"},{"b":"56500E313000","o":"81C545","s":"__wt_err"},{"b":"56500E313000","o":"81C79D","s":"__wt_panic"},{"b":"56500E313000"

 

看到这个错,有些发怵;不过有些信息还是很有用的,No space left on device。提示我们 是空间不足。

通过Zabbix 查看相应时间段内的可用空间(%)

确实在  04:54 左右可用空间耗尽。至此,问题已定位。

进一步考虑,为什么空间会短时间耗尽呢? 分析发现,这个时间段我们正在做完整备份,并且是先备份后压缩的方式,占用空间比较大。

优化方案调整为:MongoDB 实现备份压缩(https://www.cnblogs.com/xuliuzai/p/9594138.html)。

 

附注:

MongoDB是一种非关系型数据库(NoSql),很好的实现了面向对象的思想(OO思想),在Mongo DB中 每一条记录都是一个Document对象。Mongo DB最大的优势在于所有的数据持久操作都无需开发人员手动编写SQL语句,直接调用方法就可以轻松的实现CRUD操作。

posted @ 2019-05-16 14:50  东山絮柳仔  阅读(12031)  评论(0编辑  收藏  举报