You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Node.js连接MongoDB遇getaddrinfo ENOTFOUND错误求助

Fixing "MongoError: getaddrinfo ENOTFOUND admin admin:27017" in Bitnami MEAN Stack

Hey there! Let's break down what's going on here and get your Node.js-MongoDB connection working smoothly.

The Root Cause

That error message gives away the issue immediately: your Node.js code is trying to treat admin as a MongoDB server/host address, and it can't resolve that name. Worse, the duplicate admin in your connection string is confusing MongoDB—it’s interpreting the string as "connect to a host named admin on port admin:27017" (which doesn’t exist, hence the ENOTFOUND error).

Bitnami MEAN Stack MongoDB Defaults

First, let’s recap the standard setup for MongoDB in Bitnami’s AWS EC2 MEAN stack:

  • MongoDB runs locally on your EC2 instance, listening on 127.0.0.1 (localhost) port 27017
  • Authentication is enabled by default for security
  • The default admin username is root
  • You can find your MongoDB password by running this command on your EC2 instance:
    cat /home/bitnami/bitnami_credentials
    
    Look for the line labeled MongoDB Password.

Correct Connection String Format

You need to fix your connection string in database.js to follow MongoDB’s proper URI structure. Here are the two common use cases:

Since Bitnami enables auth by default, use this format (replace the placeholders with your actual details):

const mongoose = require('mongoose'); // If using Mongoose
mongoose.connect('mongodb://root:YOUR_BITNAMI_MONGO_PASSWORD@127.0.0.1:27017/YOUR_DATABASE_NAME?authSource=admin')
  .then(() => console.log('MongoDB connected successfully!'))
  .catch(err => console.error('MongoDB connection failed:', err));
  • The authSource=admin parameter tells MongoDB to use the admin database to verify your credentials—this is required for Bitnami’s setup.

If you turned off auth (not advised for production), your string is simpler:

mongoose.connect('mongodb://127.0.0.1:27017/YOUR_DATABASE_NAME')

Quick Validation Checks

  • Confirm MongoDB is listening on localhost: Run netstat -plnt | grep mongod on your EC2 instance. You should see output like tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 1234/mongod
  • Ensure you’re not using a remote host address: Since your Node.js app runs on the same EC2 instance as MongoDB, 127.0.0.1 is the correct host to connect to.

Once you update your connection string with the right values, restart your Node.js server and the connection should work as expected!

内容的提问来源于stack exchange,提问作者Arun-

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:16:06