当前位置: 动力学知识库 > 问答 > 编程问答 >

maven - Cannot connect to mongodb replica set

问题描述:

I'm using the datanucleus mongodb maven plugin and "access platform" for connecting my java app to mongodb using JPA.

I've followed the instructions on http://docs.mongodb.org/manual/tutorial/deploy-replica-set/

on a ubuntu VM, added db1.mongo, db2.mongo and db3.mongo into the hosts file on both the guest vm and the host (Mac OS X).

I got a simple java app connecting to the servers, (as described in http://www.datanucleus.org/products/accessplatform_3_0/mongodb/support.html).

When I connect the app to the primary (connection url: mongodb:db1.mongo:27017/ops?replicaSet=rs0) everything works just fine, but when I add the other two mongodb's to the connection url, so it becomes mongodb:db1.mongo:27017/ops?replicaSet=rs0,db2.mongo:27018,db3.mongo:27019 I get the exception:

com.mongodb.MongoException: can't find a master

at com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:503)

at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:236)

at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:216)

...

I've searched for this error, but the ones I have found concerns use of localhost/127.0.0.1. I tried to mitigate that by running mongodb on a separate VM and thus a non-local IP as well as adding the names to the hosts file.

The primary goal with trying mongodb is to achieve availability so replication and being able to failover is extremely important. Transactions and consistency between nodes in case of failure is not a problem, neither are we concerned about loosing an update or two once in a while so mongodb looks like a good alternative using JPA (I'm utterly fed up with mysql :-)

Thanks in advance for your help!

网友答案:

I used multiple MongoDB servers when I originally wrote that support and worked back then. Not got time now, but you can look at the DataNucleus code that parses your datastore connection URL and converts it into MongoDB java API calls. Should strip the servers apart and then call "new Mongo(serverAddrs);". If its passing it in correctly (debugger?), then the problem is possibly Mongo-specific, as opposed to what DataNucleus does for you.

Also make sure you're using v3.1.2 (or later) of datanucleus-mongodb

网友答案:

I think you've misformatted your MongoDB URI. Instead of this:

mongodb:db1.mongo:27017/ops?replicaSet=rs0,db2.mongo:27018,db3.mongo:27019

Do this:

mongodb:db1.mongo:27017,db2.mongo:27018,db3.mongo:27019/ops?replicaSet=rs0
分享给朋友:
您可能感兴趣的文章:
随机阅读: