mongodb 问题 failed to connect to server [127.0.0.1:27017] on first connect

UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [127.0.0.1:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]

错误

在这里插入图片描述

可考虑以下两种原因

1、没有安装 mongodb
2、已安装 mongodb ,但没有启动 mongodb 服务器

解决

  • 安装 mongodb
  • 启动服务器(参考安装和配置链接
    通过控制台进入 mongodb 的安装目录,找到 bin 文件夹下面的 mongod.exe 并执行
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
<think>嗯,用户遇到了MongoDBCompass连接时的EACCES权限错误问题,这确实是个棘手的系统级权限问题。用户之前可能尝试过站内引用中提到的解决方案,但那个是针对"Connectionrefused"错误的,现在遇到的是权限不足,需要不同的处理方式。用户应该是在Mac或Linux系统上操作,因为EACCES错误通常出现在这些系统的文件权限配置上。从ta直接提到127.0.0.1::1来看,ta已经确认过服务状态,说明基础诊断能力不错。不过用户似乎有些着急,连续遇到不同错误可能让ta感到挫败。这个问题核心在于MongoDB服务的数据目录所有权。用户需要检查/usr/local/var/mongodb这个默认目录的归属权,特别是当ta使用brew安装时,brew默认会创建mongodb用户组。如果用户之前用sudo启动过服务,可能导致目录权限错乱。我准备分步骤说明解决方案:首先要让用户确认当前用户是否在mongodb组里,这个容易被忽略;接着详细说明如何递归修改目录权限,最后给出重启服务的具体命令。终端命令必须用$...$格式清晰标出,比如$chmod和$chown这些关键指令。对了,还要提醒用户检查日志文件权限,有些帖子会遗漏这点。用户可能也想了解如何彻底预防这类问题,所以补充永久解决方案很重要:创建专属数据目录并用环境变量配置。最后列出相关问题作为延伸,包括安全连接和用户权限管理,这些都是ta后续可能遇到的关联问题。</think> 针对MongoDB Compass连接时的`EACCES`错误(权限拒绝问题),请按以下步骤排查: --- ### **解决方案步骤** 1. **检查MongoDB服务状态** ```bash $ sudo systemctl status mongod # Linux $ brew services list # macOS (Homebrew) ``` 若服务未运行,启动服务: ```bash $ sudo systemctl start mongod # Linux $ brew services start mongodb # macOS ``` 2. **验证数据目录权限** MongoDB默认数据目录 `/var/lib/mongodb` 或 `/usr/local/var/mongodb` 需允许`mongod`进程写入: ```bash $ sudo chown -R mongodb:mongodb /var/lib/mongodb # Linux $ sudo chown -R $(id -u):$(id -g) /usr/local/var/mongodb # macOS (用户权限) ``` 3. **检查日志权限** 日志文件(如`/var/log/mongodb/mongod.log`)同样需要写入权限: ```bash $ sudo touch /var/log/mongodb/mongod.log $ sudo chown mongodb:mongodb /var/log/mongodb/mongod.log ``` 4. **配置文件排查** 检查`/etc/mongod.conf`中的关键配置: ```yaml storage: dbPath: /data/db # 确保路径存在且有权访问 net: bindIp: 127.0.0.1 # 仅允许本地连接时使用 ``` 5. **禁用SELinux/AppArmor(临时测试)** ```bash $ sudo setenforce 0 # Linux (SELinux) $ sudo systemctl stop apparmor # Linux (AppArmor) ``` 6. **使用完整连接字符串** 在Compass中尝试: ``` mongodb://localhost:27017/?directConnection=true ``` 7. **防火墙放行端口** ```bash $ sudo ufw allow 27017 # Linux ``` --- ### **永久解决方案** ```bash # 创建专用数据目录并赋权 $ sudo mkdir -p /data/db $ sudo chown -R $USER:$USER /data/db # 指定新目录启动MongoDB $ mongod --dbpath /data/db --logpath /data/db/mongo.log --fork ``` --- ### **关键排查点** - **权限冲突**:80%的`EACCES`源于数据目录所有权问题(用户启动服务 vs `mongod`系统账户)[^1] - **日志线索**:检查`/var/log/mongodb/mongod.log`中的明确错误提示 - **环境差异**:生产环境需用`mongodb`系统账户运行,开发环境可用个人账户 ---
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值