我正在尝试创建Mongo复制,并在通过应用程序访问时出现失败时查看它是如何工作的
我正在使用Docker创建3个Mongo实例并将它们添加到复制集中。
我可以通过RoboMongo访问每个mongo。而复制工作也很好。
这是应用程序我试图在我的Ubuntu 16.04上运行。
public static void main(String[] args) {
System.out.println("Started");
MongoClient client = new MongoClient(Arrays.asList(new ServerAddress(
"localhost", 30001), new ServerAddress("localhost", 30002),
new ServerAddress("localhost", 30003)));
MongoDatabase database = client.getDatabase("ecodedb");
System.out.println("Connected");
FindIterable<Document> mydatabaserecords = database.getCollection("test").find();
MongoCursor<Document> iterator = mydatabaserecords.iterator();
System.out.println("Fetching");
while (iterator.hasNext()) {
Document doc = iterator.next();
System.out.println(doc.getString("test"));
}
System.out.println("closing");
client.close();
}
当我执行此操作时,我得到以下错误
Started
Feb 21, 2018 6:05:03 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[localhost:30001, localhost:30002, localhost:30003], mode=MULTIPLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
Feb 21, 2018 6:05:03 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Adding discovered server localhost:30001 to client view of cluster
Feb 21, 2018 6:05:03 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Adding discovered server localhost:30002 to client view of cluster
Feb 21, 2018 6:05:03 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Adding discovered server localhost:30003 to client view of cluster
Connected
Feb 21, 2018 6:05:04 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster description not yet available. Waiting for 30000 ms before timing out
Feb 21, 2018 6:05:04 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:1, serverValue:14}] to localhost:30001
Feb 21, 2018 6:05:04 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:3, serverValue:8}] to localhost:30003
Feb 21, 2018 6:05:04 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:2, serverValue:7}] to localhost:30002
Feb 21, 2018 6:05:04 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Monitor thread successfully connected to server with description ServerDescription{address=localhost:30002, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 6, 2]}, minWireVersion=0, maxWireVersion=6, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=6861873, setName='my-mongo-set1', canonicalAddress=mongo22:27017, hosts=[mongo33:27017, mongo11:27017, mongo22:27017], passives=[], arbiters=[], primary='mongo11:27017', tagSet=TagSet{[]}, electionId=null, setVersion=1, lastWriteDate=Wed Feb 21 18:04:48 IST 2018, lastUpdateTimeNanos=20772559246323}
Feb 21, 2018 6:05:04 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Discovered cluster type of REPLICA_SET
Feb 21, 2018 6:05:04 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Monitor thread successfully connected to server with description ServerDescription{address=localhost:30003, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 6, 2]}, minWireVersion=0, maxWireVersion=6, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=5936115, setName='my-mongo-set1', canonicalAddress=mongo33:27017, hosts=[mongo33:27017, mongo11:27017, mongo22:27017], passives=[], arbiters=[], primary='mongo11:27017', tagSet=TagSet{[]}, electionId=null, setVersion=1, lastWriteDate=Wed Feb 21 18:04:48 IST 2018, lastUpdateTimeNanos=20772554078487}
Feb 21, 2018 6:05:04 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Monitor thread successfully connected to server with description ServerDescription{address=localhost:30001, type=REPLICA_SET_PRIMARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 6, 2]}, minWireVersion=0, maxWireVersion=6, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=7623888, setName='my-mongo-set1', canonicalAddress=mongo11:27017, hosts=[mongo33:27017, mongo11:27017, mongo22:27017], passives=[], arbiters=[], primary='mongo11:27017', tagSet=TagSet{[]}, electionId=7fffffff0000000000000001, setVersion=1, lastWriteDate=Wed Feb 21 18:05:01 IST 2018, lastUpdateTimeNanos=20772555718524}
Feb 21, 2018 6:05:04 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Adding discovered server mongo33:27017 to client view of cluster
Feb 21, 2018 6:05:04 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Adding discovered server mongo11:27017 to client view of cluster
Feb 21, 2018 6:05:04 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Adding discovered server mongo22:27017 to client view of cluster
Feb 21, 2018 6:05:04 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Canonical address mongo22:27017 does not match server address. Removing localhost:30002 from client view of cluster
Feb 21, 2018 6:05:04 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Server localhost:30003 is no longer a member of the replica set. Removing from client view of cluster.
Feb 21, 2018 6:05:04 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Server localhost:30001 is no longer a member of the replica set. Removing from client view of cluster.
Feb 21, 2018 6:05:04 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Canonical address mongo11:27017 does not match server address. Removing localhost:30001 from client view of cluster
Feb 21, 2018 6:05:04 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: No server chosen by com.mongodb.Mongo$4@cb644e from cluster description ClusterDescription{type=REPLICA_SET, connectionMode=MULTIPLE, serverDescriptions=[ServerDescription{address=localhost:30003, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=mongo33:27017, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=mongo11:27017, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=mongo22:27017, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=localhost:30001, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
Feb 21, 2018 6:05:08 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Exception in monitor thread while connecting to server mongo22:27017
com.mongodb.MongoSocketException: mongo22: Name or service not known
at com.mongodb.ServerAddress.getSocketAddress(ServerAddress.java:188)
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:59)
at com.mongodb.connection.SocketStream.open(SocketStream.java:57)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:126)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.UnknownHostException: mongo22: Name or service not known
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323)
at java.net.InetAddress.getAllByName0(InetAddress.java:1276)
at java.net.InetAddress.getAllByName(InetAddress.java:1192)
at java.net.InetAddress.getAllByName(InetAddress.java:1126)
at java.net.InetAddress.getByName(InetAddress.java:1076)
at com.mongodb.ServerAddress.getSocketAddress(ServerAddress.java:186)
... 5 more
Feb 21, 2018 6:05:08 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Exception in monitor thread while connecting to server mongo22:27017
com.mongodb.MongoSocketException: mongo22
at com.mongodb.ServerAddress.getSocketAddress(ServerAddress.java:188)
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:59)
at com.mongodb.connection.SocketStream.open(SocketStream.java:57)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:126)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.UnknownHostException: mongo22
at java.net.InetAddress.getAllByName0(InetAddress.java:1280)
at java.net.InetAddress.getAllByName(InetAddress.java:1192)
at java.net.InetAddress.getAllByName(InetAddress.java:1126)
at java.net.InetAddress.getByName(InetAddress.java:1076)
at com.mongodb.ServerAddress.getSocketAddress(ServerAddress.java:186)
... 5 more
Feb 21, 2018 6:05:26 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Exception in monitor thread while connecting to server mongo33:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:62)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:126)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:59)
at com.mongodb.connection.SocketStream.open(SocketStream.java:57)
... 3 more
Feb 21, 2018 6:05:26 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Exception in monitor thread while connecting to server mongo11:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:62)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:126)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:59)
at com.mongodb.connection.SocketStream.open(SocketStream.java:57)
... 3 more
Exception in thread "main" com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches com.mongodb.Mongo$4@cb644e. Client view of cluster state is {type=REPLICA_SET, servers=[{address=mongo33:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}, {address=mongo11:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}, {address=mongo22:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: mongo22}, caused by {java.net.UnknownHostException: mongo22}}]
at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:389)
at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:108)
at com.mongodb.Mongo.getConnectedClusterDescription(Mongo.java:883)
at com.mongodb.Mongo.createClientSession(Mongo.java:873)
at com.mongodb.Mongo$3.getClientSession(Mongo.java:862)
at com.mongodb.Mongo$3.execute(Mongo.java:819)
at com.mongodb.MongoIterableImpl.execute(MongoIterableImpl.java:130)
at com.mongodb.MongoIterableImpl.iterator(MongoIterableImpl.java:77)
at com.main.java.MongoTest.main(MongoTest.java:25)
当我看到日志时,它看起来像应用程序能够检测哪个是主要的,哪个是辅助但无法查询。
答案 0 :(得分:1)
问题是,Mongo Driver正在寻找主机名&#34; Mongo22&#34;和它与ip的映射。我在Mongo Containers主机名文件中进行映射。
相反,我必须在我的主机的主机名文件中,即在我的Ubuntu中。
所以我添加了这样的条目
mongo22 172.134.55.66
mongo11 172.44.55.34
之后我的复制工作正常。