MongoDB命令行连接失败求助:无法连接至127.0.0.1:27017
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:
- Open Command Prompt as Administrator (right-click Command Prompt > Run as administrator).
- 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.exeagain—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.
- If you see a message saying "The MongoDB service was started successfully", wait 10 seconds then try running
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):
- First, create a
datafolder for MongoDB to store its data files. Make it atD:\mongo-bd\data(since your bin folder is inD:\mongo-bd—keeping things organized). - In Command Prompt, navigate to your MongoDB bin directory:
cd D:\mongo-bd\bin - Run
mongod.exeand point it to your new data folder:mongod.exe --dbpath D:\mongo-bd\data - 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!).
- Open a new Command Prompt window, go back to
D:\mongo-bd\bin, and runmongo.exeagain. 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:
- In Command Prompt, run:
netstat -ano | findstr :27017 - 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.
- Alternatively, you can start MongoDB on a different port to avoid the conflict:
Then connect using that new port:mongod.exe --dbpath D:\mongo-bd\data --port 27018mongo.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

