简体   繁体   English

mongod 和 mongo 命令在 windows 10 上不起作用

[英]mongod and mongo commands not working on windows 10

I've installed mongoDB on my windows 10 OS.我已经在我的 windows 10 操作系统上安装了 mongoDB。 Then I tried setting its database path to some directory by moving to it and typing mongod --datapath=data in cmd, where data is the folder which is to contain the db(I've used the relative path because I'm in that directory).然后我尝试通过移动到它并在 cmd 中键入mongod --datapath=data来将其数据库路径设置为某个目录,其中数据是包含数据库的文件夹(我使用了相对路径,因为我在那个目录)。 But message comes that mongod is unrecognized command.但是有消息说 mongod 是无法识别的命令。 After some searching I found that by specifying mongod path, ie "C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe" --datapath=data works.经过一番搜索后,我发现通过指定 mongod 路径,即"C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe" --datapath=data有效。 Similar thing happens for mongo .类似的事情发生在mongo

I want to directly run mongod and mongo commands, I have seen people directly using it(without going to the directory or specifying the path).我想直接运行mongodmongo命令,我看到有人直接使用它(不去目录或指定路径)。

For a Windows installation, by default you have to use the full path to the exe unless you add it to the PATH.对于 Windows 安装,默认情况下您必须使用 exe 的完整路径,除非您将其添加到 PATH。

To add it to the PATH:要将其添加到 PATH:

01) Get path to bin, something like: C:\\Program Files\\MongoDB\\Server\\4.0\\bin 01)获取 bin 的路径,例如: C:\\Program Files\\MongoDB\\Server\\4.0\\bin
02) Press the Windows key, type env , select Edit the system environment variables 02)按 Windows 键,输入env ,选择Edit the system environment variables
03) On the Advanced tab, click Environment Variables 03)Advanced选项卡上,单击Environment Variables
04) In the User variables for xxxx section, select path and then click the Edit... button 04)User variables for xxxxUser variables for xxxx部分,选择path ,然后单击Edit...按钮
05) Click New and paste your path with a trailing slash, eg: 05)单击New并用斜杠粘贴您的路径,例如:
C:\\Program Files\\MongoDB\\Server\\4.0\\bin\\
06) Click OK , OK , OK and restart your command window. 06)单击OKOKOK并重新启动命令窗口。
Source 来源

The examples you have seen are probably based on UNIX installations which I think by default install mongo as a service (which Windows doesn't) and that is what is called in those examples.您看到的示例可能基于 UNIX 安装,我认为默认情况下将 mongo 作为服务安装(Windows 没有),这就是这些示例中的名称。

To simplify startup and configuration on Windows, you can also install it as a service.为了简化 Windows 上的启动和配置,您还可以将其安装为服务。 See the Mongo documentation here and the "Configure Windows Service for MongoDB' section".请参阅此处的 Mongo 文档和“为 MongoDB 配置 Windows 服务”部分。

This will then allow you to start and stop Mongo by simply calling这将允许您通过简单地调用来启动和停止 Mongo

net start MongoDB

Or或者

net stop MongoDB

reference : Microsoft document参考: 微软文档

set your path like this像这样设置你的路径

;C:\Program Files\MongoDB\Server\4.0\bin

this is worked for me.这对我有用。

If installed MongoDB version is 6.0 or above, mongo command will not work on Powershell/cmd.如果安装的 MongoDB 版本6.0或以上,mongo 命令将无法在 Powershell/cmd 上运行。 If you run the command you will get the following error:如果运行该命令,您将收到以下错误:

'mongo' is not recognized as an internal or external command, operable program or batch file. 'mongo' 不是内部或外部命令,也不是可运行的程序或批处理文件。

To run mongo commands, you have to install MongoDB Shell from要运行 mongo 命令,您必须安装MongoDB Shell

After installing the shell, extract the zip file, you can rename the extracted folder (mongosh-1.6.0-win32-x64) as "MongoDB Shell" and move that folder to Windows(:C) > Program Files安装 shell 后,提取 zip 文件,您可以将提取的文件夹 (mongosh-1.6.0-win32-x64) 重命名为“MongoDB Shell”,并将该文件夹移动到Windows(:C) > Program Files

Now open the folder, go to bin and copy the path:现在打开文件夹 go 到 bin 并复制路径:

C:\Program Files\MongoDB Shell\mongosh-1.6.0-win32-x64\bin (or C:\Program Files\mongosh-1.6.0-win32-x64\mongosh-1.6.0-win32-x64\bin) C:\Program Files\MongoDB Shell\mongosh-1.6.0-win32-x64\bin(或 C:\Program Files\mongosh-1.6.0-win32-x64\mongosh-1.6.0-win32-x64\bin)

Go to Go转

Settings > System > About > Advanced system settings > Environment Variables > Under System Variables, click on 'Path' then 'Edit' > Click 'New' and paste the above copied path > Click 'Ok' 'Ok' 'Ok'设置>系统>关于>高级系统设置>环境变量>在系统变量下,单击“路径”,然后单击“编辑”>单击“新建”并粘贴上面复制的路径>单击“确定”“确定”“确定”

Now open Powershell/cmd, run the command 'mongosh'现在打开 Powershell/cmd,运行命令“mongosh”

You're all set to work with MongoDB你已经准备好使用 MongoDB

Based on welshGaz answer above, I edited the User Path variable but it did not work for me yet.根据上面的 welshGaz 答案,我编辑了 User Path 变量,但它对我不起作用。 I wasn't able to access the System Path variables.我无法访问系统路径变量。

What I noticed from the errors on the command prompt is that it what missing the "C:\\data\\db" directory to store its files (I don't know what those files are for just yet).我从命令提示符上的错误中注意到它缺少“C:\\data\\db”目录来存储其文件(我还不知道这些文件的用途)。 So I created that directory myself and it worked.所以我自己创建了那个目录并且它起作用了。

Same problem here.同样的问题在这里。 I installed through the .msi file provided for windows X64bit.我通过为 Windows X64bit 提供的 .msi 文件安装。 In the installer instructions from MongoDB ( https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/ ), I read that you can add C:\\Program Files\\MongoDB\\Server\\4.2\\bin to the System Path.在 MongoDB ( https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/ ) 的安装程序说明中,我读到您可以添加C:\\Program Files\\MongoDB\\Server\\4.2\\bin到系统路径。 Then it asks to omit the full path to the the MongoDB binaries.然后它要求省略 MongoDB 二进制文件的完整路径。 That is where I think some information is missing.这就是我认为缺少一些信息的地方。 How are we supposed to omit the full path to the MongoDB binaries?我们应该如何省略 MongoDB 二进制文件的完整路径?

Currently I can get MongoDB to run mongod using:目前,我可以使用以下方法让 MongoDB 运行 mongod:

"C:\\Program Files\\MongoDB\\Server\\4.2\\bin\\mongod.exe" --dbpath="c:\\data\\db"

For --dbpath="c:\\data\\db" you can replace "c:\\data\\db" with the path to your database.对于--dbpath="c:\\data\\db"您可以将"c:\\data\\db"替换为您的数据库路径。

I can also run mongo using:我还可以使用以下命令运行 mongo:

"C:\\Program Files\\MongoDB\\Server\\4.2\\bin\\mongo.exe"

Another reason to it if you enabled any property in YAML file and it is not formatted properly.如果您在 YAML 文件中启用了任何属性并且格式不正确,则另一个原因。 YAML looks for specific syntax like colon":"+space" ". YAML 查找特定语法,如冒号":"+space" "。

Eg-例如-

security:
    authorization: enabled

To add it to the PATH:要将其添加到 PATH:

Add Mongo's bin folder to the Path Environment Variable Kindly check the link: here After adding bin folder to the path Environment Variable then simply type mongo in terminal it will start working将 Mongo 的 bin 文件夹添加到 Path Environment Variable请检查链接: here将 bin 文件夹添加到路径环境变量后,只需在终端中键入mongo即可开始工作

use mongosh command from your terminal.从您的终端使用mongosh命令。 mongo command no longer works for 6.0 and above. mongo命令不再适用于 6.0 及更高版本。

if you are trying to connect from connection url eg mongodb://localhost:27017/yourdb try changing it to something like mongodb://127.0.0.1/yourdb如果您尝试从连接 url 进行连接,例如mongodb://localhost:27017/yourdb尝试将其更改为类似mongodb://127.0.0.1/yourdb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM