接続の問題 - 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 クラスターに接続することはできません。

Amazon VPC の外部から Amazon DocumentDB クラスターに接続するには、SSH トンネルを使用します。詳細については、「Amazon VPC 外部から Amazon DocumentDB クラスターへの接続」を参照してください。さらに、開発環境が別の Amazon VPC にある場合は、VPC ピアリングを使用して、同じリージョンまたは別のリージョンにある別の Amazon VPC から Amazon DocumentDB クラスターに接続することもできます。

リージョン間接続

他のリージョンにある Amazon DocumentDB クラスターに接続しようとしています。

クラスターのリージョン以外のリージョンにある Amazon EC2 インスタンスから Amazon DocumentDB クラスターに接続しようとするとします。例えば、米国西部 (オレゴン) リージョン (us-west-2) から米国東部 (バージニア北部) リージョン (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 VPC とは異なる VPC から Amazon DocumentDB クラスターに接続しようとしています。

Amazon DocumentDB クラスターと Amazon EC2 インスタンスの両方が同じ にあるが AWS リージョン、同じ Amazon VPC にない場合、2 つの Amazon VPCs 間で VPC ピアリングが有効になっていない限り、Amazon DocumentDB クラスターに直接接続することはできません。

Amazon DocumentDB インスタンスの Amazon VPC を確認するには、以下のコマンドを実行します。

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

Amazon ÉC2 インスタンスの Amazon 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 Mongo ドライバーの読み取り設定に関する問題

クライアントの読み取り設定は適用されず、一部のクライアントは再起動しない限りフェイルオーバー後に Amazon DocumentDB に書き込むことができません。

この問題は、Java Mongo ドライバー 3.7.x で初めて発見されましたが、クライアントが MongoClientSettings を使用して Amazon DocumentDB への接続を確立したとき、具体的には applyToClusterSettings メソッドをチェーニングしたときに発生します。 MongoClient クラスター設定は、、、 hosts() requiredReplicaSetName()など、いくつかの異なる方法を使用して定義できますmode()

クライアントが hosts() メソッドでホストを 1 つだけ指定した場合、モードは ClusterConnectionMode.MULTIPLE の代わりに ClusterConnectionMode.SINGLE に設定されます。これにより、クライアントは読み取り設定を無視し、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 レコードの更新が遅れた場合でも、クライアントは古いライター(フェイルオーバー後にリーダーになる)への書き込みを試みます。その結果、(マスターではない)サーバー側のエラーが発生し、Java ドライバーでは適切に処理されません(まだ調査中です)。そのため、たとえばアプリケーションサーバーが再起動されるまで、クライアントは不正な状態のままになる可能性があります。

これには次の 2 つの回避策があります。

  • 接続文字列を使用して Amazon DocumentDB に接続するクライアントは、読み込み設定を設定するときに ClusterConnectionModeMULTIPLE に設定されるため、この問題は発生しません。

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

    または、applyConnectionString メソッドで MongoClientSettings ビルダーを使用することもできます。

    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 に設定します。これは applyToClusterSettingshosts().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-endpointport をインスタンスのエンドポイントとポートに置き換えてください)。

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 エンドポイントについて」を参照してください。

接続数に影響するドライバー設定

クライアントドライバーを使用して Amazon DocumentDB クラスターに接続する場合は、 maxPoolSize設定パラメータを考慮することが重要です。maxPoolSize この設定により、クライアントドライバーが接続プールで維持する接続の最大数が決まります。