When you try to connect mongoDB locally through java, its easy as below
MongoClient mongoClient = null ;
try {
mongoClient = new MongoClient( "localhost" , 27017 );
} catch (UnknownHostException e) {
e.printStackTrace();
}
DB db = mongoClient.getDB( "mongoDB" );
DBCollection documents = db.getCollection("Documents");
but when you try to connect remote mongoDB, It wont connect for the below code.
MongoClient mongoClient = null ;
try {
mongoClient = new MongoClient( "10.55.36.74" , 27017 );
} catch (UnknownHostException e) {
e.printStackTrace();
}
DB db = mongoClient.getDB( "mongoDB" );
DBCollection documents = db.getCollection("Documents");
To make th above code work, you need to do some changes in mongod.conf file.
Go to the location /usr/local/etc/mongod.conf .
You will find below option of bind_ip.
bind_ip is 127.0.0.1, which forces the server to only listen for requests on the localhost IP.
# Only accept local connections
bind_ip = 127.0.0.1
Chnage the same option to
bind_ip = 0.0.0.0
Once you change the same remote connection through JAVA as mentioned in above code will work.
No comments:
Post a Comment