MongoDb 连接被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6499268/
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
MongoDb connection refused
提问by Russ Bateman
This is my first attempt to consume MongoDB. I've got Mongo running:
这是我第一次尝试使用 MongoDB。我让 Mongo 运行:
ps -ef | grep [m]ongo
mongodb 11023 1 0 Jun24 ? 00:00:03 /usr/lib/mongodb/mongod --config /etc/mongodb.conf
And the error comes as the result of doing
错误是因为这样做
Datastore.save( stuff ); // (pseudo code)
The error:
错误:
Jun 27, 2011 3:20:29 PM com.mongodb.DBTCPConnector fetchMaxBsonObjectSize WARNING: Exception determining maxBSON size using0 java.io.IOException: couldn't connect to [russ-elite-book/127.0.1.1:27017] bc:java.net.ConnectException: Connection refused at com.mongodb.DBPort._open(DBPort.java:206) at com.mongodb.DBPort.go(DBPort.java:94) at com.mongodb.DBPort.go(DBPort.java:75) at com.mongodb.DBPort.findOne(DBPort.java:129) at com.mongodb.DBPort.runCommand(DBPort.java:138) ...
Jun 27, 2011 3:20:29 PM com.mongodb.DBTCPConnector fetchMaxBsonObjectSize WARNING: Exception determining maxBSON size using0 java.io.IOException: couldn't connect to [russ-elite-book/127.0.1.1:27017] bc:java.net.ConnectException: Connection refused at com.mongodb.DBPort._open(DBPort.java:206) at com.mongodb.DBPort.go(DBPort.java:94) at com.mongodb.DBPort.go(DBPort.java:75) at com.mongodb.DBPort.findOne(DBPort.java:129) at com.mongodb.DBPort.runCommand(DBPort.java:138) ...
Note that I'm using 127.0.0.1:27017for my connection, which works to the Mongo shell. Also, I get the admin page in the browser using http://localhost:28017
.
请注意,我使用127.0.0.1:27017进行连接,它适用于 Mongo shell。另外,我在浏览器中使用http://localhost:28017
.
Profuse thanks for any and all ideas!
非常感谢您的任何想法!
采纳答案by Russ Bateman
(I think it slightly bad form to answer one's own question, but in fact, the answer turns out to be none of those suggested. Nevertheless, my profuse thanks to all of them. When answering a question, one needs to be able to assume it's based on correctly installed and working software. I did not have that.)
(我认为回答自己的问题有点糟糕,但实际上,答案并不是所建议的。不过,我非常感谢他们所有人。在回答问题时,人们需要能够假设它基于正确安装和运行的软件。我没有那个。)
I installed MongoDB using the Ubuntu Software Center. It worked from the shell and from the browser as noted elsewhere in this question. However, it did not work from Java (nor from Django either).
我使用 Ubuntu 软件中心安装了 MongoDB。如本问题中其他地方所述,它在 shell 和浏览器中工作。但是,它不适用于 Java(也不适用于 Django)。
The problem, despite what it said in the Java stack trace, was simply "connection refused."
尽管在 Java 堆栈跟踪中说明了问题,但问题只是“连接被拒绝”。
The solution is to install it from proper Mongo sources and not to trust the Ubuntu repository.
解决方案是从适当的 Mongo 来源安装它,而不是信任 Ubuntu 存储库。
(Yes, this also frequently happens to other products obtain from there too, like Eclipse, but you know it's such a nice service that you want to trust it.)
(是的,从那里获得的其他产品也经常发生这种情况,例如 Eclipse,但您知道这是一项非常好的服务,您希望信任它。)
If you want to read how I installed what then worked, check out http://www.javahotchocolate.com/tutorials/mongodb.html.
如果您想了解我是如何安装然后工作的,请查看http://www.javahotchocolate.com/tutorials/mongodb.html。
回答by nids
I ran into the same issue because I upgraded my mongo using brew. To fix this issue. Look for the conf file(which might not be located in the bin directory where you start your mongodb from)
我遇到了同样的问题,因为我使用 brew 升级了我的 mongo。为了解决这个问题。查找 conf 文件(它可能不在您启动 mongodb 的 bin 目录中)
/usr/local/Cellar/mongodb-2.2.whatever/mongod.conf, and comment out the "bind_ip" property.
/usr/local/Cellar/mongodb-2.2.whatever/mongod.conf,并注释掉“bind_ip”属性。
回答by Daniel Flower
I had the same problem, but my solution was different. I was using "localhost" as the host name and changing it to "127.0.0.1" fixed it.
我有同样的问题,但我的解决方案不同。我使用“localhost”作为主机名并将其更改为“127.0.0.1”修复了它。
回答by agirbal
Most likely the Java driver cannot connect to the address specified.
Make sure that you can connect to that address 127.0.1.1:27017
using the shell and run "db.isMaster()
".
Maybe it is an issue of 127.0.1.1
vs 127.0.0.1
.
Java 驱动程序很可能无法连接到指定的地址。确保您可以127.0.1.1:27017
使用 shell连接到该地址并运行“ db.isMaster()
”。也许这是127.0.1.1
vs的问题127.0.0.1
。
回答by Jingwen
I ran into the same issue because I installed my mongo using apt-get. Here's how to fix it:
我遇到了同样的问题,因为我使用 apt-get 安装了我的 mongo。以下是修复方法:
- Find your mongod.conf file. In Ubuntu 14.04, it's /etc/mongod.conf
- Open mongod.conf file and command the line bindIp = 127.0.0.1
- Restart mongod
- 找到您的 mongod.conf 文件。在 Ubuntu 14.04 中,它是 /etc/mongod.conf
- 打开 mongod.conf 文件并命令 bindIp = 127.0.0.1
- 重启mongod
回答by Rodrigue
It turns out that it is an issue with the Java MongDB driver.
事实证明这是Java MongDB 驱动程序的问题。
There are two solutions:
有两种解决方案:
- Using version 2.8or higher of the Java driver.
- Edit the configuration files and if it contains the line
bind_ip = 127.0.0.1
orbind_ip = localhost
, comment it out.
Eating humble pie...
吃着卑微的馅饼...
As I showed in my comment to Russ Bateman's own answer, it seemed like an issue with the Ubuntu package at first. The real reason is that the config file that comes with it does define a value for bind_ip
...
正如我在对Russ Bateman 自己的回答的评论中所显示的那样,起初这似乎是 Ubuntu 软件包的问题。真正的原因是它附带的配置文件确实为bind_ip
...定义了一个值。
回答by zbugs
It appears that this is a security issue or an invalid url. If you used the default configuration, you should be able to access the http://yourmachine:28017. See if you can navigate to the admin page from this url. If you are able to navigate to the admin url, just replace the port number with 27017 in your app. It should work.
看来这是一个安全问题或无效的 url。如果您使用默认配置,您应该能够访问http://yourmachine:28017。看看您是否可以从此网址导航到管理页面。如果您能够导航到管理 url,只需在您的应用程序中将端口号替换为 27017。它应该工作。
回答by dm.
It's hard to say without seeing your .conf file contents. One thing I would recommend is running the mongo shell and seeing if you can connect, query, and write from it. This will help isolate server vs. a java client issue. Additionally it may give you a different form of the error which may be a hint.
如果没有看到您的 .conf 文件内容,很难说。我推荐的一件事是运行 mongo shell 并查看您是否可以连接、查询和写入它。这将有助于隔离服务器与 Java 客户端问题。此外,它可能会为您提供不同形式的错误,这可能是一个提示。
Are you setting --port in your .conf file?
您是否在 .conf 文件中设置 --port ?
回答by Damir Olejar
For me, it was a completely different solution. I am using Spring MVC framework, and all I had to do is to confirm that the configuration XML files are saying the same thing in the /target folder as well as the non-target folders. Once that was adjusted, everything worked. The problem was that everything worked when run from a STS, but when deployed, I had the mentioned error.
对我来说,这是一个完全不同的解决方案。我正在使用 Spring MVC 框架,我所要做的就是确认配置 XML 文件在 /target 文件夹以及非目标文件夹中都说了同样的话。调整好后,一切正常。问题是从 STS 运行时一切正常,但是在部署时,我遇到了提到的错误。