Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How mongodb clears connections and logs

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to clear mongodb connections and logs, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Preface

Recently, I have been doing concurrency, and the server is stuck.

It seems that pymongo failed to completely close the tcp connection created by mongodb and made a high-frequency connection before the mongo connection was automatically released, resulting in a full number of connections.

So erase the established connection and record it and share it.

View connection information for MongoDB

Db.serverStatus () connections

Current number of connections to current

Number of connections available for available

The count of all incoming connections created by connection.totalCreated to the server. This number includes connections that have been closed.

Clear all current currentOp:

Var ops = db.currentOp () .inprog;for (I = 0; I

< ops.length; i++){ var opid = ops[i].opid; db.killOp(opid); print("Stopping op ... #"+opid)}; 上面的命令无法关闭current连接 关闭current的方法一: 查看所有current的tcp连接: netstat -nat | grep '27017' 查看指定端口的进程号 netstat -tpna | grep :35522 然后kill掉该进程 kill -9 14335 再到db-shell中查看,已经关闭一个current了。 这种方式关掉的tcp连接状态会变成TIME_WAIT,端口依旧被占用,因为ACK是由我们主动关闭发出的。 不过还好的是一般等一段时间链接自动会被释放,端口也不再被占用了。 可以看到35522和35524都已经被释放。 关闭current的方法二: 如果你知道是由哪个程序连接的mongodb,比如python程序 那么可以使用命令查看程序端口占用情况 ps -aux | grep python 然后kill掉对应的python程序,run.py,也能完成current的清除 批量清除指定程序: ps -efww|grep -w "run.py" |grep -v grep|cut -c 9-15|xargs kill -9日志 查看日志位置 cat /etc/mongod.conf 日志文件过大,不能通过vim查看,用tail查看最后100行 tail -n 100 mongo.log 不需要重启服务,重新开启一个新日志文件的方法 use admindb.runCommand({logRotate:1}) 运行过程中不能删除日志文件,清空日志文件但不删除文件的命令(删除后没有生成日志) : >

Run.log

You can also use rotation log logRotate: to cut the log

Scriptdb.getCollection ('assembly name') .aggregate ([{$group: {_ id: {'deduplication field'}, count: {$sum:1}, dups: {$addToSet:'$_ id'}, {$match: {count: {$gt:1}], {allowDiskUse: true}) .forEach (function (doc) {doc.dups.shift (); db.getCollection ('collection name'). Remove ({_ id: {$in: doc.dups}}) }) Export data

Mongoexport-h localhost:27017-u user name-p password-d library name-c collection name-o export file path

The above is all the contents of the article "how to clear connections and logs in mongodb". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report