Mongodb连接失败(连接被拒绝)

3

我第一次在OSX上创建一个基本的Web应用程序。

我已经安装了MongoDB,并且可以使用终端中的“mongo”命令,但是我无法使用它进入mongodb shell。

SOE-SOE01492:evnt humadshah$ mongo
MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27017
2017-07-13T13:29:20.917+0500 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2017-07-13T13:29:20.917+0500 E QUERY    [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:237:13
@(connect):1:6
exception: connect failed

我完全不知道是什么原因导致了这个问题。在Windows上,我以前从未遇到过这个错误。


你能提供Mongo配置文件的内容吗? 它应该位于/usr/local/etc/mongod.conf。 - Adil Shaikh
那个目录似乎没有任何内容... - Human Cyborg Relations
你能告诉我你是如何在你的Mac上安装Mongo的吗?你使用了Homebrew吗? - Adil Shaikh
它具有逐步说明。在Mac上安装Mongo只需从命令行运行“brew install mongodb”即可。 - Adil Shaikh
这个有效。谢谢! - Human Cyborg Relations
显示剩余2条评论
2个回答

10

我尝试了这段代码,它对我起作用。请确保您完整地安装了MongoDB。

$ brew services start mongodb

那就检查一下你的 MongoDB 连接吧

$ mongo

结果:

Server has startup warnings: 
2017-10-01T10:54:13.444+0700 I CONTROL  [initandlisten] 
2017-10-01T10:54:13.445+0700 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-10-01T10:54:13.445+0700 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-10-01T10:54:13.445+0700 I CONTROL  [initandlisten] 

执行以下命令 sudo chown -R mongodb /var/lib/mongodb,然后再运行sudo service mongod restart。 - Amol Suryawanshi

4

mongo命令只是连接到mongodb的客户端。如果mongodb进程未运行,则mongo命令无法连接到mongodb。

您可以尝试手动启动mongodb服务吗?我不知道如何在OSX中启动它,但在Linux中,它应该是这样的;

$ sudo service mongodb start

或者

$ sudo systemctl start mongodb

针对OSX系统

$ cd mongo-db-directory
$ ./bin/mongod

1
它告诉我'service'和'systemctl'未被识别。 - Human Cyborg Relations
你能试一下更新的答案吗?记得在运行mongod之后尝试使用mongo - Memduh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接