如何在 Windows 中连接到 MongoDB?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9647561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect to MongoDB in Windows?
提问by marko
I'm trying to run Mongo from the Command-Line: What's wrong? (I've IIS on localhost:80). And Apache on port 8080. Are there any issues with this?
我正在尝试从命令行运行 Mongo:怎么了?(我在 localhost:80 上有 IIS)。Apache 在端口 8080 上。这有什么问题吗?
C:\MONGO\Project1\mongo\bin>mongo --port 27017
MongoDB shell version: 2.0.3
connecting to: 127.0.0.1:27017/test
Sat Mar 10 16:16:45 Error: couldn't connect to server 127.0.0.1:27017 shell/mong
o.js:86
exception: connect failed
采纳答案by JHN
Did you create the default db path?
It defaults to "/data/db
directory (or c:\data\db
on Windows)"
Source: http://www.mongodb.org/display/DOCS/Starting+and+Stopping+Mongo
您是否创建了默认的数据库路径?
它默认为“/data/db
目录(或c:\data\db
在 Windows 上)”
来源:http: //www.mongodb.org/display/DOCS/Starting+and+Stopping+Mongo
回答by Don Hogan
I found that when I got this error it wasn't because I didn't have my default db path set up. It was because I was trying to run mongo.exe beforerunning mongod.exe.
我发现当我收到此错误时,并不是因为我没有设置默认的 db 路径。这是因为我试图在运行 mongod.exe之前运行 mongo.exe。
回答by rahan
As Admin, create directory:
mkdir c:\mongo\data\db
As Admin, install service:
.\mongod.exe --install --logpath c:\mongo\logs --logappend --bind_ip 127.0.0.1 --dbpath c:\mongo\data\db --directoryperdb
Start MongoDB:
net start MongoDB
Start Mongo Shell:
c:\mongo\bin\mongo.exe
以管理员身份创建目录:
mkdir c:\mongo\data\db
作为管理员,安装服务:
.\mongod.exe --install --logpath c:\mongo\logs --logappend --bind_ip 127.0.0.1 --dbpath c:\mongo\data\db --directoryperdb
启动MongoDB:
net start MongoDB
启动 Mongo Shell:
c:\mongo\bin\mongo.exe
回答by Sameer Jadhav
Follow
跟随
Create default db folder.
c:\data\db
and also log folder
c:\data\log\mongo.log
or use following commands in command-prompt
mkdir c:\data\log mkdir c:\data\db
Create config file in bin folder of mongo (or you may in save your desired destination).
Add following in text file named "mongod" and save it as
mongod.cfg
dbpath=c:\data\db
logpath=c:\data\log\mongo.logor use following commands in command-prompt
echo dbpath=c:\data\db>> "mongod.cfg" echo logpath=c:\data\log\mongo.log>> "mongod.cfg"
Now open command-prompt (administrator) and run the following command to start mongo server
mongod
Open another command-prompt (don't close 1st prompt) and run client command:
mongo
创建默认 db 文件夹。
c:\数据\数据库
以及日志文件夹
c:\data\log\mongo.log
或在命令提示符中使用以下命令
mkdir c:\data\log mkdir c:\data\db
在 mongo 的 bin 文件夹中创建配置文件(或者您可以保存您想要的目的地)。
在名为“mongod”的文本文件中添加以下内容并将其保存为
mongod.cfg
dbpath=c:\data\db
logpath=c:\data\log\mongo.log或在命令提示符中使用以下命令
echo dbpath=c:\data\db>> "mongod.cfg" echo logpath=c:\data\log\mongo.log>> "mongod.cfg"
现在打开命令提示符(管理员)并运行以下命令启动 mongo 服务器
mongod
打开另一个命令提示符(不要关闭第一个提示符)并运行客户端命令:
mongo
Hope this will help or you have done this already.
希望这会有所帮助,或者您已经这样做了。
回答by user4166159
The error occurs when trying to run mongo.exe WITHOUT having executed mongod.exe. The following batch script solved the problem:
尝试在未执行 mongod.exe 的情况下运行 mongo.exe 时发生错误。以下批处理脚本解决了该问题:
@echo off
cd C:\mongodb\bin\
start mongod.exe
start mongo.exe
exit
回答by Abdul Mannan
If you are getting these type of errors when running mongodfrom command line or running mongodb server,
then follow these steps,
如果您在从命令行运行mongod或运行 mongodb 服务器时遇到这些类型的错误,请
按照以下步骤操作,
- Create dband logdirectories in C: drive
C:/data/db and C:data/log - Create an empty log file in logdir named mongo.log
- Run mongodfrom command line to run the mongodb server or create a batch file on desktop which can run the mongod.exe file from your mongodb installation direction. That way you just have to click the batch file from your desktop and mongodb will start.
- If you have 32-bit system, try using --journalwith mongodcommand.
- 在 C: 驱动器
C:/data/db 和 C:data/log 中
创建db和log目录
- 在名为mongo.log 的日志目录中创建一个空日志文件
- 从命令行运行mongod以运行 mongodb 服务器或在桌面上创建一个批处理文件,该文件可以从您的 mongodb 安装方向运行 mongod.exe 文件。这样你只需点击桌面上的批处理文件,mongodb 就会启动。
- 如果您有 32 位系统,请尝试将--journal与mongod命令一起使用。
回答by Vijay Chauhan
Create default db folder.
创建默认 db 文件夹。
c:\data\db
and also log folder
以及日志文件夹
c:\data\log\mongo.log
or use following
commands in command-prompt
或following
在命令提示符中使用命令
mkdir c:\data\log
mkdir c:\data\db
回答by Mohd Akram
- Go to C:\Program Files\MongoDB\Server\3.4\binusing cmd and write mongod.
- Open another cmd by right click and run as adminpoint to your
monogodb installed directory as mentioned above and then just like
write this
mongo.exe
- After that, write
db.test.save({Field:'Hello mongodb'})
this command will insert a field having name Field and value is Hello mongodb. - After, check the record
db.test.find()
and press enter you will find the record that you have recently entered.
- 使用 cmd转到C:\Program Files\MongoDB\Server\3.4\bin并编写 mongod。
- 通过右键单击打开另一个 cmd 并以管理员身份运行到您的 monogodb 安装目录,如上所述,然后就像写这个一样
mongo.exe
- 之后,编写
db.test.save({Field:'Hello mongodb'})
此命令将插入一个名为 Field 且值为Hello mongodb 的字段。 - 之后,检查记录
db.test.find()
并按回车键,您将找到您最近输入的记录。
回答by samba
you can use below command,
您可以使用以下命令,
mongod --dbpath=D:\home\mongodata
where D:\home\mongodatais the data storage path
其中D:\home\mongodata为数据存储路径
回答by Ritesh Choudhary
For this error, if you are using windows 7 or windows server 2008 R2, the problem could be that you have to install a microsoft hotfix.
对于此错误,如果您使用的是 Windows 7 或 Windows Server 2008 R2,则问题可能是您必须安装 Microsoft 修补程序。
Refer to this link: https://support.microsoft.com/en-us/kb/2731284