Mongodb

MongoDB:幾分鐘後伺服器套接字關閉

  • January 6, 2022

我正在使用多個AWS intances連接到同一個MongoDB數據庫(在Compose.io彈性部署中)。server <url>:<port> sockets closed但幾分鐘後我得到了錯誤。誰能給我任何關於連接程式碼可能有問題的提示?

連接程式碼

var url = "mongodb://<user>:<password>@<url1>:<port1>,<url2>:<port2>/<dbName>?replicaSet=<replicaSetName>"; 
var options = { 
   server : {"socketOptions.keepAlive": 1}, 
   replSet : { "replicaSet": <replicaSetName>, "socketOptions.keepAlive": 1 } 
}; 
MongoClient.connect(url, options, function(err, db) { ... });

錯誤資訊

Potentially unhandled rejection [2] MongoError: server <url>:<port> sockets closed 
at null. (/var/app/current/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:328:47) 
at g (events.js:199:16) 
at emit (events.js:110:17) 
at null. (/var/app/current/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:101:12) 
at g (events.js:199:16) 
at emit (events.js:110:17) 
at Socket. (/var/app/current/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:142:12) 
at Socket.g (events.js:199:16) 
at Socket.emit (events.js:107:17) 
at TCP.close (net.js:485:12)

首先是一個可能很愚蠢的問題,您是否將所有 , 等佔位符替換為compose.io數據庫中的實際值?

如果您已經這樣做了,也許您可以使用 調試連接mongo shell以連接到您的數據庫實例。

如果您有本地安裝,mongodb則可以使用:

mongo 主機:埠/數據庫名稱 -u 使用者 -ppassword

這也是compose.io文件推薦的連接調試https://docs.compose.io/common-questions/how-to-connect-to-mongodb.html

也可能達到最大連接數(如果您配置了)

2015-12-08T08:49:10.964+0000 I NETWORK [initandlisten] connection accepted from 192.168.34.21:5345 #184 (26 connections now open)
2015-12-08T08:49:10.964+0000 I NETWORK [initandlisten] connection refused because too many open connections: 25

引用自:https://dba.stackexchange.com/questions/108965