在尝试连接到 mongodb 服务器时无法识别 mongo 命令

我按照 docs.mongodb.org 上的教程学习,我已经完成了第一个教程,那就是在 Windows 机器上安装 mongodb。我现在正处于第二阶段,即开始进行 mongodb 开发。

我卡在这一节的第一个阶段,它指示我键入 mongo到系统提示符。当我这样做的时候,我只是得到一个错误消息说:

'mongo' is not recognized as an internal or external command, operable program or batch file

我知道这可能是一件很简单的事情,我做错了,有人有什么想法吗?

178216 次浏览

You need to run mongod first in one cmd window then open another and type mongo. Make sure you updated your Windows Path environment variable too so that you don't have to navigate to the directory you have all of the mongo binaries in to start the application. To update the Path variable:

Go to Control Panel > System & Security > System > Advanced System Settings > Environment Variables > navigate to the Path variable hit Edit and add ;C:\mongodb to the Path (or whatever the directory name is where MongoDB is located (the semi-colon delimits each directory).

You need to be in the right directory, i.e.

C:\Program Files\MongoDB\bin

and the path you are installing into needs to be the correct one

i.e.

mongod --dbpath
C:\Users\Name\Documents\myWebsites\nodetest1

A folder named "data" must also exist in your project folder.

It is probably too late, but for the sake of others (like me) who faced the same problem. It is all about the little '\' at the end of the path variable. When you insert the path to MongoDB's bin directory at the end of the PATH windows variable, do not forget to put the '\' (Backslash) at the end, which tells windows it is a directory and not an executable named bin... e.g. I:\Program Files\MongoDB\Server\3.0\bin\

This worked for me:

.\mongod  --dbpath c:\......

Seems like, The environmental variable is not correctly set up.

Go to the mongodb installation folder and get the executable files (mongo.exe, mongod.exe etc) location. (In my case) Something like :

C:\Program Files\MongoDB\Server\3.2\bin

Then go to :

Panel > System & Security > System > Advanced System Settings > Environment Variables

Find the PATH variable and edit its value. Then add C:\Program Files\MongoDB\Server\3.2\bin and don't forget to separate each values with ;. Now confirm and exit.

putting forward slash "/" at the end of path to bin of mongodb solved my problem.

First, make sure you have the environment variable set up. 1. Right click on my computer 2. properties 3. advanced system settings 4. environment variables 5. edit the PATH variable. and add ;"C:\mongoDb\bin\" to the PATH variable.

Path in the quotes may differ depending on your installation directory. Do not forget the last '\' as it was the main problem in my case.

Adding MongoDb bin path in Environment path with \ worked for me

This is what my system path

C:\ProgramData\Oracle\Java\javapath;
...
...
Other path variables
...
;C:\Users\hitesh.sahu\AppData\Local\Android\sdk\platform-tools
;C:\Program Files\MongoDB\Server\3.2\bin\

Make sure:-

  • Environment path must not have space between them
  • Environment path must be saparated by ;

You need to add Mongo's bin folder to the "Path" Environment Variable

Here's how on Windows 10:

  1. Find Mongo's bin folder.

If you're not sure where it is, it's probably in C:\Program Files\MongoDB\Server\3.4\ 3.4 was the latest stable version at the time, this will be different for you probably.

It should look like this:

This is what Mongo's bin folder looked like for version 3.4, the important thing is it's whatever folder contains mongod.exe. Notice this is the path to mongo.exe and mongod.exe. Adding this folder to the Path variable is telling Windows to search in this folder for executables matching your command when you run something in cmd. The search starts with the current working dir, and if it doesn't find your exe, goes on to search all the paths in Path till it finds it or it doesn't and it gives you that error you saw.

  1. Copy the path to the bin folder. It should be C:\Program Files\MongoDB\Server\3.4\bin\ (Or whatever version you're using)

  2. Press win, type env, Windows will suggest "Edit the System Environment Variables", click that.

How to find the system environment variables.

  1. On the Advanced tab, click "Environment Variables"

The Advanced tab in System Properties contains the Environment Variables.

  1. Highlight the "Path" variable, click "Edit":

You want to edit the Path variable to add Mongo's bin folder to it.

  1. This will bring up the "Edit environment variable" window, click "New"

Add a new folder to the Path variable

  1. This will start a new line in the list of folders:

A new line in the Path variable.

  1. Paste your path to the bin folder. Make sure it ends with a \ like so:

Paste the location of the bin folder.

  1. Press "OK", "OK", "OK"

  2. Restart anything where you want to use the new path: CMD windows, Visual Studio terminals, etc. (Thanks to @id.ot for their comment)

Now you should be able to run mongod and mongo from anywhere in a command window.

Apart from having a Path variable, the directory C:\data\db is mandatory.

Create this and the error shall be solved.

if still not working for you then just close all of your command prompts and then again open and run mongo, mongoimport, mongodb from anywhere it ll work because after setting the path variable command prompt should be restarted.

For add environment variable please add \ after bin like below

C:\Program Files\MongoDB\Server\3.2\bin\

Then try below code in command prompt to run mongo server from parent folder of data folder.

mongod -dbpath ./data

For my case I am unable to run mongo from command prompt(normal mode). You should run as administrator. It also works on git bash.

1.To begin using MongoDB, Open CMD with admin privilege and type : "C:\Program Files\MongoDB\Server\4.2\bin\mongo.exe"

2.To create data directory(open another cmd terminal) cd C:
md "\data\db" 3. To start your mongo DB database, type in cmd the following lines: "C:\Program Files\MongoDB\Server\4.2\bin\mongod.exe" --dbpath="c:\data\db" (The --dbpath option points to your database directory.) [you can also do step 2 and 3 first and then step 1]

ref:https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/

before using MongoDB you have to run it locally to do that:

  • go to bin folder you will find at C:\Program Files\MongoDB\Server\4.2\bin
  • open mongod.exe. will open a new terminal with server details.
  • open mongo.exe. will open the shell which allows you to interact with the database.

mostly, this is not working because you haven't set the path, in my case too I haven't set a path. so this can be solved by following the steps.:

  1. Go to the control panel

  2. Click on system and security

  3. then click on the system

  4. click on the advanced system setting

  5. click on environment variable

  6. click on the path in user variables

  7. Edit the path

  8. Add a new path

  9. (copy the path from local discs c /program files/mongodb/bin), it should look similar to this C:\Program Files\MongoDB\Server\5.0\bin

  10. paste it there and click ok

  11. close the vs code and reopen it

Just use mongotop instead of mongod in your terminal.

I download MongoDB Community 6.0.0 and same issue occure.

mongo command not recognized

That's my bin folder by default and there is no mongo.exe present.

That's my bin folder, and there is no mongo.exe present

so Check This answer where you have to download it manually.

If still have confusion then first go to this link and download the file. After Extraction you see this.

enter image description here

Go inside bin folder. You will file 2 Files like

enter image description here

Copy them and add them in the following path.

C:\Program Files\MongoDB\Server\6.0\bin

Don't forget to add this path into Environment Variable Path and you are good to go...

Instead of typing mongo in terminal, type -> mongosh or to check version -> mongosh --version