Mongo运行错误:Failed to connect 127.0.0.1:27017,reason:errno:10061由于目标计算机积极拒绝,无法连接

本文介绍了MongoDB服务未启动导致的运行错误,并提供了解决方案,即通过命令mongod--dbpath d:mongodbdata来开启服务。

Mongo运行错误:如下

原因是mongodb的服务没有开启,开启服务后问题就能解决了,开启服务命令:mongod --dbpath "d:\mongodb\data

只要操作mongodb数据库,服务就要一直开着


出处 http://www.cnblogs.com/greenteaone/ 

MongoDB 连接错误: MongoServerSelectionError: connect ECONNREFUSED 180.184.39.232:48354 at Topology.selectServer (D:\ExpressLearning\express-locallibrary-tutorial\node_modules\mongodb\lib\sdam\topology.js:321:38) at runNextTicks (node:internal/process/task_queues:65:5) at listOnTimeout (node:internal/timers:555:9) at process.processTimers (node:internal/timers:529:7) at async Topology._connect (D:\ExpressLearning\express-locallibrary-tutorial\node_modules\mongodb\lib\sdam\topology.js:200:28) at async Topology.connect (D:\ExpressLearning\express-locallibrary-tutorial\node_modules\mongodb\lib\sdam\topology.js:152:13) at async topologyConnect (D:\ExpressLearning\express-locallibrary-tutorial\node_modules\mongodb\lib\mongo_client.js:233:17) at async MongoClient._connect (D:\ExpressLearning\express-locallibrary-tutorial\node_modules\mongodb\lib\mongo_client.js:246:13) at async MongoClient.connect (D:\ExpressLearning\express-locallibrary-tutorial\node_modules\mongodb\lib\mongo_client.js:171:13) at async NativeConnection.createClient (D:\ExpressLearning\express-locallibrary-tutorial\node_modules\mongoose\lib\drivers\node-mongodb-native\connection.js:351:3) { errorLabelSet: Set(0) {}, reason: TopologyDescription { type: 'Single', servers: Map(1) { 'dbconn.sealosbja.site:48354' => [ServerDescription] }, stale: false, compatible: true, heartbeatFrequencyMS: 10000, localThresholdMS: 15, setName: null, maxElectionId: null, maxSetVersion: null, commonWireVersion: 0, logicalSessionTimeoutMinutes: null }, code: undefined, [cause]: MongoNetworkError: connect ECONNREFUSED 180.184.39.232:48354 at Socket.<anonymous> (D:\ExpressLearning\express-locallibrary-tutorial\node_modules\mongodb\lib\cmap\connect.js:285:44) at Object.onceWrapper (node:events:633:26) at Socket.emit (node:events:518:28) at emitErrorNT (node:internal/streams/destroy:170:8) at emitErrorCloseNT (node:internal/streams/destroy:129:3) at process.processTicksAndRejections (node:internal/process/task_queues:90:21) { errorLabelSet: Set(1) { 'ResetPool' }, beforeHandshake: false, [cause]: Error: connect ECONNREFUSED 180.184.39.232:48354 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1634:16) { errno: -4078, code: 'ECONNREFUSED', syscall: 'connect', address: '180.184.39.232', port: 48354 } } } D:\ExpressLearning\express-locallibrary-tutorial\node_modules\mongoose\lib\connection.js:1165 err = new ServerSelectionError(); ^ MongooseServerSelectionError: connect ECONNREFUSED 180.184.39.232:48354 at _handleConnectionErrors (D:\ExpressLearning\express-locallibrary-tutorial\node_modules\mongoose\lib\connection.js:1165:11) at NativeConnection.openUri (D:\ExpressLearning\express-locallibrary-tutorial\node_modules\mongoose\lib\connection.js:1096:11) at runNextTicks (node:internal/process/task_queues:65:5) at listOnTimeout (node:internal/timers:555:9) at process.processTimers (node:internal/timers:529:7) { errorLabelSet: Set(0) {}, reason: TopologyDescription { type: 'Single', servers: Map(1) { 'dbconn.sealosbja.site:48354' => ServerDescription { address: 'dbconn.sealosbja.site:48354', type: 'Unknown', hosts: [], passives: [], arbiters: [], tags: {}, minWireVersion: 0, maxWireVersion: 0, roundTripTime: -1, minRoundTripTime: 0, lastUpdateTime: 144818976, lastWriteDate: 0, error: MongoNetworkError: connect ECONNREFUSED 180.184.39.232:48354 at Socket.<anonymous> (D:\ExpressLearning\express-locallibrary-tutorial\node_modules\mongodb\lib\cmap\connect.js:285:44) at Object.onceWrapper (node:events:633:26) at Socket.emit (node:events:518:28) at emitErrorNT (node:internal/streams/destroy:170:8) at emitErrorCloseNT (node:internal/streams/destroy:129:3) at process.processTicksAndRejections (node:internal/process/task_queues:90:21) { errorLabelSet: Set(1) { 'ResetPool' }, beforeHandshake: false, [cause]: Error: connect ECONNREFUSED 180.184.39.232:48354 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1634:16) { errno: -4078, code: 'ECONNREFUSED', syscall: 'connect', address: '180.184.39.232', port: 48354 } }, topologyVersion: null, setName: null, setVersion: null, electionId: null, logicalSessionTimeoutMinutes: null, maxMessageSizeBytes: null, maxWriteBatchSize: null, maxBsonObjectSize: null, primary: null, me: null, '$clusterTime': null, iscryptd: false } }, stale: false, compatible: true, heartbeatFrequencyMS: 10000, localThresholdMS: 15, setName: null, maxElectionId: null, maxSetVersion: null, commonWireVersion: 0, logicalSessionTimeoutMinutes: null }, code: undefined, cause: TopologyDescription { type: 'Single', servers: Map(1) { 'dbconn.sealosbja.site:48354' => ServerDescription { address: 'dbconn.sealosbja.site:48354', type: 'Unknown', hosts: [], passives: [], arbiters: [], tags: {}, minWireVersion: 0, maxWireVersion: 0, roundTripTime: -1, minRoundTripTime: 0, lastUpdateTime: 144818976, lastWriteDate: 0, error: MongoNetworkError: connect ECONNREFUSED 180.184.39.232:48354 at Socket.<anonymous> (D:\ExpressLearning\express-locallibrary-tutorial\node_modules\mongodb\lib\cmap\connect.js:285:44) at Object.onceWrapper (node:events:633:26) at Socket.emit (node:events:518:28) at emitErrorNT (node:internal/streams/destroy:170:8) at emitErrorCloseNT (node:internal/streams/destroy:129:3) at process.processTicksAndRejections (node:internal/process/task_queues:90:21) { errorLabelSet: Set(1) { 'ResetPool' }, beforeHandshake: false, [cause]: Error: connect ECONNREFUSED 180.184.39.232:48354 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1634:16) { errno: -4078, code: 'ECONNREFUSED', syscall: 'connect', address: '180.184.39.232', port: 48354 } }, topologyVersion: null, setName: null, setVersion: null, electionId: null, logicalSessionTimeoutMinutes: null, maxMessageSizeBytes: null, maxWriteBatchSize: null, maxBsonObjectSize: null, primary: null, me: null, '$clusterTime': null, iscryptd: false } }, stale: false, compatible: true, heartbeatFrequencyMS: 10000, localThresholdMS: 15, setName: null, maxElectionId: null, maxSetVersion: null, commonWireVersion: 0, logicalSessionTimeoutMinutes: null } } Node.js v22.14.0
06-03
### 解决 MongoDB 连接错误 ECONNREFUSED 当遇到 `ECONNREFUSED` 错误时,通常意味着客户端无法成功建立到指定地址和端口的连接。对于 MongoDB 来说,在尝试通过 `127.0.0.1:27017` 或者 `::1:27017` 地址访问时发生此问题可能是因为 MongoDB 服务未启动、配置文件设置不当或是防火墙阻止了请求。 #### 验证并启动 MongoDB 服务 确认 MongoDB 是否正在运行非常重要。如果服务没有启动,则需要手动开启它: - 使用 Windows PowerShell(以管理员身份打开),执行命令 `net start mongodb` 可以启动 MongoDB 服务[^3]。 另外一种方式是在 Linux 或 macOS 上使用终端来管理服务状态: ```bash sudo systemctl start mongod ``` 或者如果是较老版本的服务控制系统则可能是: ```bash sudo service mongod start ``` #### 修改 bindIp 设置 默认情况下,MongoDB 绑定的是 localhost (即 `127.0.0.1`) 接口。为了允许来自其他 IP 的连接,可以在 `/etc/mongod.conf` 文件中的 `net.bindIp` 参数后面加上额外的IP地址或将其设为 `0.0.0.0` 表示接受所有接口上的连接请求[^1]。 修改后的配置应类似于下面这样: ```yaml net: port: 27017 bindIp: 0.0.0.0 ``` 完成更改后记得重启 MongoDB 服务使新配置生效。 #### 检查防火墙规则 有时即使 MongoDB 正常工作也可能因为网络策略而被阻挡。确保系统的防火墙不会阻碍对外部世界的开放端口 27017 访问权限。可以临时关闭防火墙测试是否为此原因造成的问题;如果不是生产环境的话也可以考虑永久调整相关安全组/ACL 规则以便长期解决问题。 #### 测试连接 最后一步是验证新的设置是否有效。可以通过命令行工具如 mongo shell (`mongosh`) 尝试连接数据库实例来进行简单的 ping 请求或其他基本操作,以此判断问题是否已经得到解决[^4]: ```javascript use admin; db.runCommand({ping: 1}); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值