MongoDB主机名/URI配置 [英] MongoDB HostName/URI Configuration

查看:349
本文介绍了MongoDB主机名/URI配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了使用Google Cloud Launcher创建Mongo数据库.这在两个单独的VM上创建了两个Mongo服务器(主服务器和辅助服务器)和一个仲裁器的副本集.我没有更改任何VM配置(除了打开防火墙外).我已经将SSH连接到这两个服务器中,并验证了Mongo是否可以按预期进行复制.我正在将外部IP从x.x.x.x更改为FIRST.EXTERNAL.IP和SECOND.EXTERNAL.IP,以使帖子更具可读性(和安全性).

I used the Google Cloud Launcher to create a Mongo database. This created a replica set of two Mongo servers (primary and secondary) and an arbiter on three separate VMs. I have not changed any VM configurations (other than opening the firewall). I have SSH'd into both of the servers and verified Mongo is working with replication as expected. I'm changing the external IPs from x.x.x.x to FIRST.EXTERNAL.IP and SECOND.EXTERNAL.IP to make the post more readable (and security).

我现在正尝试通过Java MongoClient连接到数据库:

I'm now trying to connect to the database through a Java MongoClient:

MongoClientOptions.Builder mongoClientOptions = MongoClientOptions.builder().connectionsPerHost(40)
  .writeConcern(WriteConcern.ACKNOWLEDGED).readPreference(ReadPreference.secondaryPreferred())
  .readConcern(ReadConcern.LOCAL).socketTimeout(60000).threadsAllowedToBlockForConnectionMultiplier(1500);
MongoClientURI mongoClientURI = new MongoClientURI("mongodb://FIRST.EXTERNAL.IP:27017,SECOND.EXTERNAL.IP:27017", mongoClientOptions);
mongoClient = new MongoClient(mongoClientURI);

我想验证我应该在URI中同时包含主要外部IP和次要外部IP .我不包括仲裁者,因为对于MongoClient直接与仲裁者进行交互没有意义.

I wanted to verify I should include both the primary external IP and the secondary external IP in the URI. I did not include the arbiter, since it doesn't make sense for the MongoClient to interact directly with the arbiter.

以下显示了当我尝试find()时的Java异常.除了用WritePreferenceServerSelector代替ReadPref之外,Insert()都会产生相同的错误.

The following shows the Java exception when I attempt a find(). Insert() creates the same error, except with WritePreferenceServerSelector instead of ReadPref..

1449 [main] INFO org.mongodb.driver.cluster - Cluster created with settings {hosts=[SECOND.EXTERNAL.IP:27017, FIRST.EXTERNAL.IP:27017], mode=MULTIPLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=60000}
1449 [main] INFO org.mongodb.driver.cluster - Adding discovered server SECOND.EXTERNAL.IP:27017 to client view of cluster
1558 [main] INFO org.mongodb.driver.cluster - Adding discovered server FIRST.EXTERNAL.IP:27017 to client view of cluster
1579 [main] INFO org.mongodb.driver.cluster - No server chosen by ReadPreferenceServerSelector{readPreference=ReadPreference{name=secondaryPreferred}} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=MULTIPLE, serverDescriptions=[ServerDescription{address=SECOND.EXTERNAL.IP:27017, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=FIRST.EXTERNAL.IP:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
1743 [cluster-ClusterId{value='588b214418786c23f4fcf845', description='null'}-FIRST.EXTERNAL.IP:27017] INFO org.mongodb.driver.connection - Opened connection [connectionId{localValue:2, serverValue:12}] to FIRST.EXTERNAL.IP:27017
1766 [cluster-ClusterId{value='588b214418786c23f4fcf845', description='null'}-SECOND.EXTERNAL.IP:27017] INFO org.mongodb.driver.connection - Opened connection [connectionId{localValue:1, serverValue:9}] to SECOND.EXTERNAL.IP:27017
1790 [cluster-ClusterId{value='588b214418786c23f4fcf845', description='null'}-FIRST.EXTERNAL.IP:27017] INFO org.mongodb.driver.cluster - Monitor thread successfully connected to server with description ServerDescription{address=FIRST.EXTERNAL.IP:27017, type=REPLICA_SET_PRIMARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 4, 1]}, minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, roundTripTimeNanos=41160155, setName='rs0', canonicalAddress=my-project-name-mongo-server-1:27017, hosts=[my-project-name-mongo-server-1:27017, my-project-name-mongo-server-2:27017], passives=[], arbiters=[my-project-name-mongo-arbiter-1:27017], primary='my-project-name-mongo-server-1:27017', tagSet=TagSet{[]}, electionId=7fffffff0000000000000003, setVersion=3, lastWriteDate=Fri Jan 27 02:30:26 PST 2017, lastUpdateTimeNanos=165366848909779}
1791 [cluster-ClusterId{value='588b214418786c23f4fcf845', description='null'}-FIRST.EXTERNAL.IP:27017] INFO org.mongodb.driver.cluster - Discovered cluster type of REPLICA_SET
1791 [cluster-ClusterId{value='588b214418786c23f4fcf845', description='null'}-FIRST.EXTERNAL.IP:27017] INFO org.mongodb.driver.cluster - Adding discovered server my-project-name-mongo-server-1:27017 to client view of cluster
1793 [cluster-ClusterId{value='588b214418786c23f4fcf845', description='null'}-FIRST.EXTERNAL.IP:27017] INFO org.mongodb.driver.cluster - Adding discovered server my-project-name-mongo-server-2:27017 to client view of cluster
1794 [cluster-ClusterId{value='588b214418786c23f4fcf845', description='null'}-FIRST.EXTERNAL.IP:27017] INFO org.mongodb.driver.cluster - Adding discovered server my-project-name-mongo-arbiter-1:27017 to client view of cluster
1796 [cluster-ClusterId{value='588b214418786c23f4fcf845', description='null'}-FIRST.EXTERNAL.IP:27017] INFO org.mongodb.driver.cluster - Server SECOND.EXTERNAL.IP:27017 is no longer a member of the replica set.  Removing from client view of cluster.
1797 [cluster-ClusterId{value='588b214418786c23f4fcf845', description='null'}-FIRST.EXTERNAL.IP:27017] INFO org.mongodb.driver.cluster - Server FIRST.EXTERNAL.IP:27017 is no longer a member of the replica set.  Removing from client view of cluster.
1798 [cluster-ClusterId{value='588b214418786c23f4fcf845', description='null'}-FIRST.EXTERNAL.IP:27017] INFO org.mongodb.driver.cluster - Canonical address my-project-name-mongo-server-1:27017 does not match server address.  Removing FIRST.EXTERNAL.IP:27017 from client view of cluster
1799 [cluster-ClusterId{value='588b214418786c23f4fcf845', description='null'}-my-project-name-mongo-server-1:27017] INFO org.mongodb.driver.cluster - Exception in monitor thread while connecting to server my-project-name-mongo-server-1:27017
com.mongodb.MongoSocketException: my-project-name-mongo-server-1: unknown error
   at com.mongodb.ServerAddress.getSocketAddress(ServerAddress.java:188)
   at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57)
   at com.mongodb.connection.SocketStream.open(SocketStream.java:58)
   at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
   at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113)
   at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.UnknownHostException: my-project-name-mongo-server-1: unknown error
//(then it just repeats the socket exception through connection retries I believe)

我当前的理论是最初由Google Cloud Launcher设置的主机名配置需要使用外部IP更新.这意味着将my-project-name-mongo-server-1更改为FIRST.EXTERNAL.IP,对于第二个服务器,将其更改为

My current theory is the hostname configuration originally set by the Google Cloud Launcher needs to be updated with the external IPs. This would mean changing my-project-name-mongo-server-1 to FIRST.EXTERNAL.IP and the same for the second server as seen in the Mongo Documentation. If this is correct, should I just leave the arbiter hostname? (Side note - how does my-project-name-mongo-server-1:27017 resolve? Is it an internal IP to the Google Cloud?)

据我所知,这将解决我的问题和异常Canonical address my-project-name-mongo-server-1:27017 does not match server address.

As far as I can tell, this would fix the my issue and the exception Canonical address my-project-name-mongo-server-1:27017 does not match server address.

重温一下有关我的问题的每个问题:

To recap each of my questions about my issue:

  1. 我应在Java MongoClient URI中包括哪些IP?
  2. 是否应该将成员主机名重新配置为外部IP?如果是这样,哪个?
  3. (可选)解释my-project-name-mongo-server-1:27017是否为本地主机名会很有帮助.
  4. 既然您知道我的应用程序设计,是否会有更好的方法来连接我的外部连接?

推荐答案

将主机名更新为非内部IP可以解决规范地址问题.我仍然不确定是否最适合URI,但从我的判断中可以看出,将副本集的所有服务器都包括在内是最安全的.

Updating the hostnames to non-internal IPs fixed the canonical address problem. I'm still not completely sure what's best for the URI, but from what I can tell it's safest to include all servers of the replica set.

这篇关于MongoDB主机名/URI配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆