Here is the code that I used:
package testing;
import com.mongodb.*;
public class MongoTest {
public static void main(String args[])
{
try
{
MongoClient cli=new MongoClient("10.6.9.227",27017);
DB dtbs=cli.getDB("test");
System.out.println("Connection to DB successful");
DBCollection col=dtbs.getCollection("newcol");
BasicDBObject record=new BasicDBObject("name","student1").append("sem","6");
col.insert(record);
System.out.println("Successfully Inserted into collection");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}}
This code inserts perfectly into the same machine from which this eclipse program is run(which is when 10.6.9.57 is specified while initializing cli object).
But, when I try to do the same thing in a remote machine in the same LAN (which is given by ip 10.6.9.227), this is what is displayed in eclipse console:
Connection to DB successful Timed out after 10000 ms while waiting for a server that matches AnyServerSelector{}. Client view of cluster state is {type=Unknown, servers=[{address=10.6.9.227:28017, type=Unknown, state=Connecting, exception={java.lang.NullPointerException}}]
The machine 10.6.9.57 runs on windows OS. I run my eclipse program from here. The remote machine 10.6.9.227 runs on Debian Squeeze OS.
I checked to see if the required ports are open through netstat
command in the system 227:
:~$ sudo netstat -lntup
>Active Internet connections (only servers)
>Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
>tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 848/portmap
>tcp 0 0 127.0.0.1:28017 0.0.0.0:* LISTEN 2421/mongod
>tcp 0 0 0.0.0.0:49396 0.0.0.0:* LISTEN 860/rpc.statd
>tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1566/sshd
>tcp 0 0 127.0.0.1:5984 0.0.0.0:* LISTEN 1163/beam.smp
>tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 2421/mongod
>tcp6 0 0 :::22 :::* LISTEN 1566/sshd
>udp 0 0 0.0.0.0:54866 0.0.0.0:* 860/rpc.statd
>udp 0 0 0.0.0.0:612 0.0.0.0:* 860/rpc.statd
>udp 0 0 0.0.0.0:32870 0.0.0.0:* 1260/avahi-daemon:
>udp 0 0 0.0.0.0:5353 0.0.0.0:* 1260/avahi-daemon:
>udp 0 0 0.0.0.0:111 0.0.0.0:* 848/portmap
>udp 0 0 10.6.9.227:123 0.0.0.0:* 1610/ntpd
>udp 0 0 127.0.0.1:123 0.0.0.0:* 1610/ntpd
>udp 0 0 0.0.0.0:123 0.0.0.0:* 1610/ntpd
>udp6 0 0 :::5353 :::* 1260/avahi-daemon:
>udp6 0 0 :::123 :::* 1610/ntpd
>udp6 0 0 :::50061 :::* 1260/avahi-daemon:
These ports are open only and I'm also able to insert in that local machine from it's own command line(through putty):
> db.newcol.insert({name:"firststudent"},{sem:"5"});
> db.newcol.find()
{ "_id" : ObjectId("5571366e1031fc034ac63013"), "name" : "firststudent" }
Also, this is what the Mongo DB log file at /var/log/mongodb looks like:
Fri Jun 5 10:39:02 Mongo DB : starting : pid = 2421 port = 27017 dbpath = /var/lib/mongodb master = 0 slave = 0 64-bit
Fri Jun 5 10:39:02 db version v1.4.4, pdfile version 4.5
Fri Jun 5 10:39:02 git version: nogitversion
Fri Jun 5 10:39:02 sys info: Linux bobek-a0 2.6.32-trunk-amd64 #1 SMP Sun Jan 10 22:40:40 UTC 2010 x86_64 BOOST_LIB_VERSION=1_42
Fri Jun 5 10:39:02 waiting for connections on port 27017
Fri Jun 5 10:39:02 web admin interface listening on port 28017
I am just not able to resolve why a java program run from eclipse of a machine connected to the same LAN is not able to do it. I have included the mongo-java-driver-2.13.0-rc1.jar. I have even tried stopping and restarting the mongo db in the remote machine and repeated this process several times. But still not able to insert. Any help?
telnet
from your box to the remote mongo box to the port 27017
. See if you can. /etc/mongod.conf
, and the value for the bind_ip setting.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.