連線問題 - Amazon DocumentDB

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

連線問題

連線時遇到問題嗎? 以下是一些常見的情況以及如何解決它們。

主題

無法連接到 Amazon DocumentDB 端點

當您嘗試連線到 Amazon DocumentDB 時,下列是您可能會收到的最常見錯誤訊息之一。

connecting to: mongodb://docdb-2018-11-08-21-47-27.cluster-ccuszbx3pn5e.us-east- 1.docdb.amazonaws.com:27017/ 2018-11-14T14:33:46.451-0800 W NETWORK [thread1] Failed to connect to 172.31.91.193:27017 after 5000ms milliseconds, giving up. 2018-11-14T14:33:46.452-0800 E QUERY [thread1] Error: couldn't connect to server docdb-2018-11-08-21-47-27.cluster-ccuszbx3pn5e.us-east-1.docdb.amazonaws.com:27017, connection attempt failed : connect@src/mongo/shell/mongo.js:237:13 @(connect):1:6 exception: connect failed

此錯誤訊息通常意味著您的用戶端 (本範例中的 mongo 殼層) 無法存取 Amazon DocumentDB 端點。這可能有幾個原因:

從公用端點連線

您嘗試直接從筆記型電腦或本機開發機器連線到 Amazon DocumentDB 叢集。

嘗試直接從公用端點 (例如筆記型電腦或本機開發機器) 連線到 Amazon DocumentDB 叢集將會失敗。Amazon DocumentDB 僅限虛擬私有雲端 (VPC),目前不支援公有端點。因此,您無法從筆記型電腦或 VPC 以外的本機開發環境直接連線到 Amazon DocumentDB 叢集。

若要從亞馬遜 VPC 擬私人雲端外部連線到 Amazon 文件資料庫叢集,您可以使用安全殼層通道。如需詳細資訊,請參閱 從亞馬遜 VPC 外部連接到 Amazon DocumentDB 集群。此外,如果您的開發環境位於不同的 Amazon VPC 中,您也可以使用 VPC 對等互連,並從相同區域或不同區域的另一個 Amazon VPC 連接到 Amazon DocumentDB 叢集。

跨區域連接

您嘗試連線到另一個區域中的 Amazon DocumentDB 叢集。

如果您嘗試從叢集區域以外的區域 (例如,嘗試從美國西部 (奧勒岡) 區域 (us-east-1) 連線到美國東部 (維吉尼 Amazon EC2 北部) 區域 (us-east-1) 中的叢集,連線將會失敗。

若要驗證 Amazon DocumentDB 叢集的區域,請執行下列命令。區域在端點中。

aws docdb describe-db-clusters \ --db-cluster-identifier sample-cluster \ --query 'DBClusters[*].Endpoint'

此操作的輸出將會如下所示。

[ "sample-cluster.node.us-east-1.docdb.amazonaws.com" ]

為了驗證您的 EC2 執行個體的區域,請執行以下命令。

aws ec2 describe-instances \ --query 'Reservations[*].Instances[*].Placement.AvailabilityZone'

此操作的輸出將會如下所示。

[ [ "us-east-1a" ] ]

從不同的 Amazon VPC 連接

您嘗試從虛擬私人雲端連線到 Amazon DocumentDB 叢集,該虛擬私人雲端與叢集部署到的 Amazon VPC 不同。

如果您的 Amazon DocumentDB 叢集和 Amazon EC2 執行個體位於相同的執行個體 AWS 區域,但不在同一個 Amazon VPC 中,則除非在兩個 Amazon VPC 之間啟用了 VPC 對等互連,否則您無法直接連線到 Amazon DocumentDB 叢集。

若要驗證 Amazon 文件資料庫執行個體的亞馬遜 VPC,請執行以下命令。

aws docdb describe-db-instances \ --db-instance-identifier sample-instance \ --query 'DBInstances[*].DBSubnetGroup.VpcId'

若要驗證 Amazon EC2 執行個體的亞馬遜 VPC,請執行以下命令。

aws ec2 describe-instances \ --query 'Reservations[*].Instances[*].VpcId'

安全群組封鎖輸入連線

您嘗試連線到 Amazon DocumentDB 叢集,且叢集的安全群組不允許在叢集的連接埠上進行輸入連線 (預設連接埠:27017)。

假設您的 Amazon DocumentDB 叢集和 Amazon EC2 執行個體都位於同一個區域和 Amazon VPC,並且使用相同的 Amazon VPC 安全群組。如果您無法連線到 Amazon DocumentDB 叢集,可能的原因是叢集的安全群組 (也就是防火牆) 不允許在您為 Amazon DocumentDB 叢集選擇的連接埠上輸入連線 (預設連接埠為 27017)。

若要驗證 Amazon DocumentDB 叢集的連接埠,請執行下列命令。

aws docdb describe-db-clusters \ --db-cluster-identifier sample-cluster \ --query 'DBClusters[*].[DBClusterIdentifier,Port]'

若要取得叢集的 Amazon DocumentDB 安全群組,請執行下列命令。

aws docdb describe-db-clusters \ --db-cluster-identifier sample-cluster \ --query 'DBClusters[*].[VpcSecurityGroups[*],VpcSecurityGroupId]'

若要檢查安全群組的輸入規則,請參閱 Amazon EC2 文件中的下列主題:

Java 蒙戈驅動程序讀取首選項問題

不會遵守用戶端讀取喜好設定,且有些用戶端在容錯移轉後無法寫入 Amazon DocumentDB,除非他們重新開機。

這個問題,首先在 Java 蒙戈驅動程式 3.7.x 中發現,當用戶端建立連接到 Amazon DocumentDB 使用MongoClientSettings和,特別是,鏈接方法時發生。applyToClusterSettings您可以使用幾種不同的方法來定義 MongoClient 叢集設定hosts(),例如requiredReplicaSetName()、和mode()

當用戶端在hosts()方法中僅指定一台主機時,模式會設定為ClusterConnectionMode.SINGLE而非ClusterConnectionMode.MULTIPLE這會導致用戶端忽略讀取偏好設定,而只連線至中hosts()設定的伺服器。因此,即使客戶端設置如下所示初始化,所有讀取仍然會轉到主讀取而不是次要讀取。

final ServerAddress serverAddress0 = new ServerAddress("cluster-endpoint", 27317)); final MongoCredential credential = MongoCredential.createCredential("xxx", "admin", "xxxx".toCharArray()); final MongoClientSettings settings = MongoClientSettings.builder() .credential(credential) .readPreference(ReadPreference.secondaryPreferred()) .retryWrites(false) .applyToSslSettings(builder -> builder .enabled(false)) .applyToClusterSettings(builder -> builder.hosts( Arrays.asList(serverAddress0 )) .requiredReplicaSetName("rs0")) .build(); MongoClient mongoClient = MongoClients.create(settings);

容錯移轉案

使用上述用戶端連線設定時,如果叢集寫入器端點有容錯移轉和延遲 DNS 記錄更新,用戶端仍會嘗試寫入舊寫入器 (現在容錯移轉後的讀取器)。這會導致服務器端錯誤(不是 master),Java 驅動程序未正確處理(這仍在調查中)。因此,例如,在重新啟動應用程式伺服器之前,用戶端可能會處於不良狀態。

有兩種解決方法:

  • 透過連接字串連線至 Amazon DocumentDB 的用戶端將不會出現此問題,因為在設定讀取偏好設定MULTIPLEClusterConnectionMode會設為。

    MongoClientURI mongoClientURI = new MongoClientURI("mongodb://usr:pass:cluster-endpoint:27317/test?ssl=false&replicaSet=rs0&readpreference=secondaryPreferred"); MongoClient mongoClient = MongoClients.create(mongoClientURI.getURI());

    MongoClientSettings者使用構建器與applyConnectionString方法。

    final MongoClientSettings settings = MongoClientSettings.builder() .credential(credential) .applyConnectionString(new ConnectionString("usr:pass:cluster-endpoint:27317/test?ssl=false&replicaSet=rs0&readpreference=secondaryPreferred")) .retryWrites(false) .applyToSslSettings(builder → builder .enabled(false)) .build(); MongoClient mongoClient = MongoClients.create(settings);
  • 明確設定ClusterConnectionModeMULTIPLE。這只有在使用applyToClusterSettings和時才需要hosts().size() == 1

    final ServerAddress serverAddress0 = new ServerAddress("cluster-endpoint", 27317)); final MongoCredential credential = MongoCredential.createCredential("xxx","admin", "xxxx".toCharArray()); final MongoClientSettings settings = MongoClientSettings.builder() .credential(credential) .readPreference(ReadPreference.secondaryPreferred()) .retryWrites(false) .applyToSslSettings(builder → builder .enabled(false)) .applyToClusterSettings(builder → builder .hosts(Arrays.asList(serverAddress0)) .requiredReplicaSetName("rs0")) .mode(ClusterConnectionMode.MULTIPLE)) .build(); MongoClient mongoClient = MongoClients.create(settings);

測試與 Amazon DocumentDB 執行個體的連接

您可以使用通用的 Linux 或 Windows 工具來測試您的叢集連線。

從 Linux 或 Unix 終端機測試連線,方法是輸入下列命令 (以端點取代 cluster-endpoint,以及以執行個體的連接埠取代 port):

nc -zv cluster-endpoint port

以下是範例操作和傳回值的範例:

nc -zv docdbTest.d4c7nm7stsfc0.us-west-2.docdb.amazonaws.com 27017 Connection to docdbTest.d4c7nm7stsfc0.us-west-2.docdb.amazonaws.com 27017 port [tcp/*] succeeded!

連線至無效的端點

連線到 Amazon DocumentDB 叢集並使用無效的叢集端點時,會出現類似下列內容的錯誤。

mongo --ssl \ --host sample-cluster.node.us-east-1.docdb.amazonaws.com:27017 \ --sslCAFile global-bundle.pem \ --username <user-name> \ --password <password>

輸出如下:

MongoDB shell version v3.6 connecting to: mongodb://sample-cluster.node.us-east-1.docdb.amazonaws.com:27017/ 2018-11-14T17:21:18.516-0800 I NETWORK [thread1] getaddrinfo("sample-cluster.node.us-east-1.docdb.amazonaws.com") failed: nodename nor servname provided, or not known 2018-11-14T17:21:18.537-0800 E QUERY [thread1] Error: couldn't initialize connection to host sample-cluster.node.us-east-1.docdb.amazonaws.com, address is invalid : connect@src/mongo/shell/mongo.js:237:13@(connect):1:6 exception: connect failed

若要取得有效的叢集端點,請執行下列命令:

aws docdb describe-db-clusters \ --db-cluster-identifier sample-cluster \ --query 'DBClusters[*].[Endpoint,Port]'

若要取得有效的執行個體端點,請執行下列命令:

aws docdb describe-db-instances \ --db-instance-identifier sample-instance \ --query 'DBInstances[*].[Endpoint.Address,Endpoint.Port]'

如需詳細資訊,請參閱 了解亞馬遜文檔數據庫端點

影響連線數目的驅動程式組態

使用用戶端驅動程式連線到 Amazon DocumentDB 叢集時,請務必考慮maxPoolSize組態參數。此maxPoolSize設定會決定用戶端驅動程式在其連線集區中維護的連線數目上限。