MongoDB Java Connection Issues

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 there are connection problems:

bc: java.net.ConnectException: connection rejected: connect

EDIT: Nvm I forgot to run the database in the background

+7
source share
1 answer

Obviously, you can connect to MongoDB on the "localhost" port 27017. You should be able to open the MongoDB shell without arguments and get a hint back:

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

If you cannot answer, this should be obvious: MongoDB is not working. You want you to start the MongoDB server process. There are several Quickstart Guides in MongoDB Docs that should help you choose a process on your platform.

Otherwise, you will not indicate which platform you are running on, but there is a problem with how Java resolves certain host names, such as "localhost" on Mac OS X ... This is actually a Java problem, not a Java problem MongoDB drivers,

When asked to allow "localhost" with the host name on InetSocketAddr , Java for Mac will usually return the IP address of your external interface. Since several installers for Mac MongoDB, such as Homebrew, block the listening IP address to 127.0.0.1, this may cause the connection to "localhost" to complete completely with the Mac with Java.

+6
source

All Articles