I have launch mongodb server:
[demas@arch.local.net][~]% mongod --dbpatmongod --dbpath /home/demas/temp/
Mon Apr 19 09:44:18 Mongo DB : starting : pid = 4538 port = 27017 dbpath = /home/demas/temp/ master = 0 slave = 0 32-bit
** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
** see http://blog.mongodb.org/post/137788967/32-bit-limitations for more
Mon Apr 19 09:44:18 db version v1.4.0, pdfile version 4.5
Mon Apr 19 09:44:18 git version: nogitversion
Mon Apr 19 09:44:18 sys info: Linux arch.local.net 2.6.33-ARCH #1 SMP PREEMPT Mon Apr 5 05:57:38 UTC 2010 i686 BOOST_LIB_VERSION=1_41
Mon Apr 19 09:44:18 waiting for connections on port 27017
Mon Apr 19 09:44:18 web admin interface listening on port 28017
I have created documents by console client:
[demas@arch.local.net][~]% mongo
MongoDB shell version: 1.4.0
url: test
connecting to: test
type "help" for help
> db.some.find();
{ "_id" : ObjectId("4bcbef3c3be43e9b7e04ef3d"),开发者_JAVA百科 "name" : "mongo" }
{ "_id" : ObjectId("4bcbef423be43e9b7e04ef3e"), "x" : 3 }
Now I am trying to work with MongoDb from Java:
import com.mongodb.*;
import java.net.UnknownHostException;
public class test1 {
public static void main(String[] args) {
System.out.println("Start");
try {
Mongo m = new Mongo("localhost", 27017);
DB db = m.getDB("test");
DBCollection coll = db.getCollection("some");
coll.insert(makeDocument(10, "James", "male"));
System.out.println("Finish");
}
catch (UnknownHostException ex) {
ex.printStackTrace();
}
catch (MongoException ex) {
ex.printStackTrace();
}
}
public static BasicDBObject makeDocument(int id, String name, String gender) {
BasicDBObject doc = new BasicDBObject();
doc.put("id", id);
doc.put("name", name);
doc.put("gender", gender);
return doc;
}
}
But execution stops on line coll.insert():
[demas@arch.local.net][~/dev/study/java/mongodb]% javac test1.java
[demas@arch.local.net][~/dev/study/java/mongodb]% java test1
Start
There are not messages from mogodb server regarding accepted connection. Why?
Hm...
If I change the line:
Mongo m = new Mongo("localhost", 27017);
to:
Mongo m = new Mongo();
all works fine.
Check your /etc/hosts file to make sure that localhost resolves to 127.0.0.1.
Typically, it will contain a line like:
127.0.0.1 localhost.localdomain localhost
Try to change localhost to 127.0.0.1
. May be it will be ok.
精彩评论