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

MongoDB命令行连接失败求助:无法连接至127.0.0.1:27017

Fixing MongoDB Connection Failed Error (v3.6.4)

Hey there, let's break down what's going on here and get your MongoDB connection sorted out. First, here's the error you shared for reference:

D:\mongo-bd\bin>mongo.exe
MongoDB shell version v3.6.4
connecting to: mongodb://127.0.0.1:27017
2018-04-27T18:50:28.340+0530 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017 after 5000ms milliseconds, giving up.
2018-04-27T18:50:28.340+0530 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:251:13
@(connect):1:6
exception: connect failed

This error almost always boils down to one of three things: the MongoDB server process (mongod) isn't running, it's not set up correctly, or its default port is being used by another program. Let's fix this step by step:

Step 1: Check if the MongoDB Windows Service is running

Most folks set up MongoDB as a Windows service for easy access. Let's verify its status:

  1. Open Command Prompt as Administrator (right-click Command Prompt > Run as administrator).
  2. Run this command to start or check the service:
    net start MongoDB
    
    • If you see a message saying "The MongoDB service was started successfully", wait 10 seconds then try running mongo.exe again—it should connect.
    • If you get "The service name is invalid", that means you didn't install MongoDB as a service. No worries, skip to Step 2.

Step 2: Manually start the MongoDB server

If you haven't set up the service, you need to launch mongod.exe first (this is the actual database server, while mongo.exe is just the shell to interact with it):

  1. First, create a data folder for MongoDB to store its data files. Make it at D:\mongo-bd\data (since your bin folder is in D:\mongo-bd—keeping things organized).
  2. In Command Prompt, navigate to your MongoDB bin directory:
    cd D:\mongo-bd\bin
    
  3. Run mongod.exe and point it to your new data folder:
    mongod.exe --dbpath D:\mongo-bd\data
    
  4. You'll see logs scrolling by. Wait until you see a line that says "waiting for connections on port 27017"—leave this Command Prompt window open (don't close it, that would stop the server!).
  5. Open a new Command Prompt window, go back to D:\mongo-bd\bin, and run mongo.exe again. This time it should connect without issues.

Step 3: Check if port 27017 is occupied by another program

If Steps 1 and 2 don't work, another app might be using MongoDB's default port. Let's check:

  1. In Command Prompt, run:
    netstat -ano | findstr :27017
    
  2. If you see a result with a PID (a number at the end), that's the process hogging the port. Open Task Manager, find the process by PID, and end it. Then restart MongoDB.
  3. Alternatively, you can start MongoDB on a different port to avoid the conflict:
    mongod.exe --dbpath D:\mongo-bd\data --port 27018
    
    Then connect using that new port:
    mongo.exe --port 27018
    

Give these steps a shot—one of them should get you connected. Let me know if you hit any snags!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:05:46