MongoDB Java连接问题 [英] MongoDB java connection issues

查看:82
本文介绍了MongoDB Java连接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行以下代码:

I am trying to run the following code:

public static void main(String args[]) throws UnknownHostException, MongoException{
        Mongo m = new Mongo( "localhost", 27017 );
        DB db = m.getDB( "test" );


        Set<String> coll = db.getCollectionNames();
    }

但是由于某些原因,我遇到了连接问题:

but for some reason I am getting connection issues :

bc:java.net.ConnectException:连接被拒绝:connect

bc:java.net.ConnectException: Connection refused: connect

Nvm我忘了在后台运行数据库

Nvm I forgot to run the database in the background

推荐答案

显然,请确保您可以在本地主机"端口27017上连接到MongoDB.您应该能够在不带参数的情况下打开MongoDB Shell,并获得提示:

Obviously, make sure you can connect to MongoDB on "localhost" port 27017. You should be able to open the MongoDB shell with no arguments and get a prompt back:

» mongo
MongoDB shell version: 1.8.1
connecting to: test
> 

如果不能,答案应该很明显:MongoDB没有运行.您需要确保启动MongoDB服务器进程. MongoDB文档中有一些快速入门指南,该指南应指导您在平台上进行该过程选择.

If you can't the answer should be obvious: MongoDB isn't running. You'll want to make sure you start up the MongoDB server process. There are some Quickstart Guides in the MongoDB Docs which should guide you to the process on your platform of choice.

否则,您不会指明正在使用什么平台,但是Java如何解析Mac OS X上的某些主机名(如"localhost")存在问题.这实际上是Java问题,而不是MongoDB Java驱动程序问题.

Otherwise, you don't indicate what platform you are on but there is an issue with how Java resolves certain hostnames like "localhost" on Mac OS X... This is actually a Java issue rather than a MongoDB Java Driver issue.

当要求将本地主机"从主机名解析为InetSocketAddr时,Java for Mac通常会返回计算机的外部接口的IP.由于Mac MongoDB的一些安装程序(如Homebrew)将侦听IP地址锁定为127.0.0.1,因此这可能导致在使用Java的Mac上完全无法连接到"localhost".

When asked to resolve "localhost" from a hostname to an InetSocketAddr, Java for Mac will typically return the IP for your machine's external interface. As a few installers for Mac MongoDB like Homebrew lock the listening IP Address to 127.0.0.1, this can cause connecting to "localhost" to fail completely on Mac with Java.

这篇关于MongoDB Java连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆