我无法从Groovy脚本连接到MongoDB。
的Env :
trnls-mbpro:test trnl$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.6.5
BuildVersion: 10H574
trnls-mbpro:test trnl$ java -version
java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04-307-10M3261)
Java HotSpot(TM) 64-Bit Server VM (build 17.1-b03-307, mixed mode)
trnls-mbpro:test trnl$ groovy -version
Groovy Version: 1.7.5 JVM: 1.6.0_22
trnls-mbpro:test trnl$ mongod -version
db version v1.6.4, pdfile version 4.5
Sun Dec 12 03:34:32 git version: 4f5c02f8d92ff213b71b88f5eb643b7f62b50abc
trnls-mbpro:test trnl$
trnls-mbpro:env trnl$ mongod -noauth -vvv
脚本test.groovy :
Mongo m = new Mongo("localhost", 27107);
DB db = m.getDB("test");
BasicDBObject info = new BasicDBObject();
info.put("x", 203);
info.put("y", 102);
DBCollection collection = db.getCollection('user');
collection.insert(info);
结果:
trnls-mbpro:test trnl$ groovy -cp mongo-2.1.jar test.groovy
Caught: com.mongodb.MongoException$Network: can't say something
at test.run(test.groovy:16)
trnls-mbpro:test trnl$ groovy -cp mongo-2.2.jar test.groovy
Dec 12, 2010 3:30:24 AM com.mongodb.DBTCPConnector$MyPort error
SEVERE: MyPort.error called
java.io.IOException: couldn't connect to [trnls-mbpro.local/192.168.1.3:27107] bc:java.net.ConnectException: Connection refused
at com.mongodb.DBPort._open(DBPort.java:172)
at com.mongodb.DBPort.go(DBPort.java:79)
at com.mongodb.DBPort.say(DBPort.java:60)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:151)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:154)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:110)
at com.mongodb.DBCollection.insert(DBCollection.java:73)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoCachedMethodSite.invoke(PojoMetaMethodSite.java:188)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:52)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:124)
at test.run(test.groovy:16)
at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:264)
at groovy.lang.GroovyShell.run(GroovyShell.java:227)
at groovy.lang.GroovyShell.run(GroovyShell.java:157)
at groovy.ui.GroovyMain.processOnce(GroovyMain.java:496)
at groovy.ui.GroovyMain.run(GroovyMain.java:311)
at groovy.ui.GroovyMain.process(GroovyMain.java:297)
at groovy.ui.GroovyMain.processArgs(GroovyMain.java:112)
at groovy.ui.GroovyMain.main(GroovyMain.java:93)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:108)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:130)
Caught: com.mongodb.MongoException$Network: can't say something
at test.run(test.groovy:16)
trnls-mbpro:test trnl$ groovy -cp mongo-2.3.jar test.groovy
Caught: com.mongodb.MongoInternalException: DBPort.findOne failed
at test.run(test.groovy:6)
trnls-mbpro:test trnl$
感谢任何帮助。
-vova
答案 0 :(得分:2)
MongoDB的默认端口是27017。 在文件27107中。
答案 1 :(得分:0)
我既不是OSX用户也不是Groovy用户,但对我来说,看起来你的localhost有点搞笑了。 我认为localhost应解析为127.0.0.1而不是198.168.1.3。
你的/ etc / hosts中有什么奇怪的东西导致这个吗?