开发者

MongoDB & Java Connection Error

I am trying to run a java program with the java/mongo driver on a separate computer than the one running mongod. I only modified the java/mongo tutorial code to include an ip address.

package mongotest;
import com.mongodb.*;

public class Main {
static DBCursor cur;
static DBCollection coll;
public static void main(String[] args) {
    Mongo m;
    try{
        m = new Mongo("192.168.0.102");  // <---- This does not connect.  It will eventually time out
        DB db = m.getDB("playerdb");
        coll = db.getCollection("players");

        cur = coll.find();
        //while (cur.hasNext())
         //  coll.remove(cur.next());

        coll.ensureIndex(new BasicDBObject("playerID", 1).append("unique", true));

       开发者_运维问答 boolean unique = true; 

        cur = coll.find();
        printResults(cur, "Find All Records");


        boolean canCreate;
        canCreate = createAccount("Josh", "1", cur, coll);
        canCreate = createAccount("Jason", "1", cur, coll);
        canCreate = createAccount("Ryan", "1", cur, coll);
        canCreate = createAccount("Michael", "1", cur, coll);
        canCreate = createAccount("John", "1", cur, coll);
        canCreate = createAccount("Susan", "1", cur, coll);


         cur = coll.find();
         printResults(cur, "Find All Records After Insert");



    }//try
    catch(Exception e){
        System.out.println(e);
    }//catch
}

(Note: This will eventually time out and quit)

But when I run the same code on the computer running the database it's fine.

How can I get a connection between two computers on different networks to communicate?


First you need to ensure a network route:

can you ping computer b from computer a?

can you telnet to the mongo port from the second computer to the first?

If not, you have a networking problem not a programming one. In which case it might behoove you to ask this question on serverfault or superuser


Check if computer a can ping computer b. If it can, then check mongodb configuration parameters like auth and noauth and set the same according to your convinience.


Two computers on different networks? Because 192.168.0.102 sure looks like an internal address, not an external one.

You need to figure out what's the public IP address of the computer running mongodb, and use that.

What you're doing is almost like (but not quite as bad) as trying to connect to 127.0.0.1 and wondering why this only works when executed on the computer that hosts the service.


This is pretty much unrelated to MongoDB. Either your network connection is not working properly (firewall or routing issue) or your remote mongod daemon is not listening on the related external IP address (ensure that is bound to the proper IP address using the --bind_ip commandline option).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜