我正在尝试将副本数据库部署到具有不同端口的一台服务器上并与之连接。 如果我只使用单个ServerAddress并直接连接到主数据库,则一切正常 像这样:
mongoClient =new MongoClient(new ServerAddress("104.236.106.53", 27000));
morphia = new Morphia();
ds = morphia.createDatastore(mongoClient, "morphiaDB");
一切正常。但是当我试图像这样使用List<ServerAddress>
时:
List<ServerAddress> lstServer = new ArrayList<ServerAddress>();
lstServer.add(new ServerAddress("104.236.106.53", 27000));
lstServer.add(new ServerAddress("104.236.106.53", 27002));
lstServer.add(new ServerAddress("104.236.106.53", 27001));
mongoClient = new MongoClient(lstServer);
morphia = new Morphia();
ds = morphia.createDatastore(mongoClient, "morphiaDB");
最终会出现此错误:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting for a server that matches {serverSelectors=[ReadPreferenceServerSelector{readPreference=primary}, LatencyMinimizingServerSelector{acceptableLatencyDifference=15 ms}]}. Client view of cluster state is {type=ReplicaSet, servers=[{address=G-Server-1:27000, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.UnknownHostException: G-Server-1}}, {address=G-Server-1:27001, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.UnknownHostException: G-Server-1}}, {address=G-Server-1:27002, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.UnknownHostException: G-Server-1}}]
at com.mongodb.BaseCluster.getServer(BaseCluster.java:82)
at com.mongodb.DBTCPConnector.getServer(DBTCPConnector.java:654)
at com.mongodb.DBTCPConnector.access$300(DBTCPConnector.java:39)
at com.mongodb.DBTCPConnector$MyPort.getConnection(DBTCPConnector.java:503)
at com.mongodb.DBTCPConnector$MyPort.get(DBTCPConnector.java:451)
at com.mongodb.DBTCPConnector.getPrimaryPort(DBTCPConnector.java:409)
at com.mongodb.DBCollectionImpl.insert(DBCollectionImpl.java:182)
at com.mongodb.DBCollectionImpl.insert(DBCollectionImpl.java:165)
at com.mongodb.DBCollection.insert(DBCollection.java:161)
at com.mongodb.DBCollection.insert(DBCollection.java:107)
at com.mongodb.DBCollection.save(DBCollection.java:965)
at org.mongodb.morphia.DatastoreImpl.save(DatastoreImpl.java:949)
at org.mongodb.morphia.DatastoreImpl.save(DatastoreImpl.java:1013)
at org.mongodb.morphia.DatastoreImpl.save(DatastoreImpl.java:1000)
at com.learn.DAO.AuthorBookDAO.main(AuthorBookDAO.java:18)
有人可以告诉我这个错误是什么,或者给我一些提示如何解决它?
答案 0 :(得分:0)
在谷歌搜索后。我发现java无法识别哪个是主数据库
GWReplication:PRIMARY> cfg=rs.conf();
{
"_id" : "GWReplication",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "G-Server-1:27000"
},
{
"_id" : 1,
"host" : "G-Server-1:27001"
},
{
"_id" : 2,
"host" : "G-Server-1:27002"
}
]
}
这是我的mongodb的默认配置。而java无法理解G-Server-1 所以我把它修改为我上面的ip
来修复它GWReplication:PRIMARY> rs.config();
{
"_id" : "GWReplication",
"version" : 2,
"members" : [
{
"_id" : 0,
"host" : "104.236.106.53:27000"
},
{
"_id" : 1,
"host" : "104.236.106.53:27001"
},
{
"_id" : 2,
"host" : "104.236.106.53:27002"
}
]
}
现在它工作正常。我知道这是一个非常白痴的修复方法但老实说我不知道如何让java识别我的域名或如何在ubuntu服务器上配置它(我在digitalocean的主机上部署mongodb这是我第一次使用ubuntu并自行配置服务器)-_-
答案 1 :(得分:0)
将副本集更改为ip地址实际上是错误的方法。
试试这个:
使用副本集域名更新您的mongo客户端配置。
List<ServerAddress> lstServer = new ArrayList<ServerAddress>();
lstServer.add(new ServerAddress("G-Server-1", 27000));
lstServer.add(new ServerAddress("G-Server-1", 27002));
lstServer.add(new ServerAddress("G-Server-1", 27001));
mongoClient = new MongoClient(lstServer);
那应该解决它。 Spring Mongo实际上使用主机名和端口号来确定副本集中配置的主副本。