1

I am trying to set up an Amazon EB instance to work with DocumentDB. When I try to connect with amazon linux terminal, the connection just work fine. But when I try to connect with PHP it returns an error shown below

"No suitable servers found (serverSelectionTryOnceset): [socket timeout calling ismaster on 'docdb-XXXX-XX-XX-XX-XX-XX.cluster-cXXXXXXXXXX.eu-west-1.docdb.amazonaws.com:27017']"

I troubleshooted with terminal and got "succeeded!" which indicate successful connection again.

nc -zv docdb-docdb-XXXX-XX-XX-XX-XX-XX.cluster-cXXXXXXXXXX.eu-west-1.docdb.amazonaws.com 27017

Anyone has any idea what can be wrong?

Here is my code to connect to DocumentDB

$conn = new MongoClient("mongodb://username:password@docdb-docdb-XXXX-XX-XX-XX-XX-XX.cluster-cXXXXXXXXXX.eu-west-1.docdb.amazonaws.com:27017/?ssl_ca_certs=rds-combined-ca-bundle.pem&replicaSet=rs0");

Ashikur Rahman
  • 131
  • 4
  • 9

5 Answers5

0

In order for your PHP server to access DocumentDB your cluster must be in the same VPC. If your server is trying to connect from a different location, your authentication might fail.

https://docs.aws.amazon.com/documentdb/latest/developerguide/troubleshooting.html#troubleshooting.cannot-connect.public-endpoints

Also AWS does not support the latest MongoDB Drivers because they can't support the latest MongoDB API.

You can avoid both limitations by using MongoDB Atlas. You can deploy a MongoDB cluster in AWS in minutes and you would not be faced with the same limitations.

https://cloud.mongodb.com/

BigDataKid
  • 1,199
  • 6
  • 10
  • Thanks for your answer. I am running DB and Server in the same VPC and when I troubleshoot connection with the link you have given, it gives Success, But doesn't work with application. Still no idea what has been happening. – Ashikur Rahman Feb 24 '19 at 09:25
0

Try adding "ssl=true&" to your connection string. Please see below:

$conn = new MongoClient("mongodb://username:password@docdb-docdb-XXXX-XX-XX-XX-XX-XX.cluster-cXXXXXXXXXX.eu-west-1.docdb.amazonaws.com:27017/?ssl=true&ssl_ca_certs=rds-combined-ca-bundle.pem&replicaSet=rs0");

Joseph Idziorek
  • 4,853
  • 6
  • 23
  • 37
0

You might want to try specifying a database name with the cluster. Something like: mongodb://username:password@docdb-docdb-XXXX-XX-XX-XX-XX-XX.cluster-cXXXXXXXXXX.eu-west-1.docdb.amazonaws.com:27017/proddb?ssl_ca_certs=rds-combined-ca-bundle.pem&replicaSet=rs0

0

Please see the following example.

Also, in your connection string it looks like you have an extra "docdb-", i.e., "docdb-docdb-XXXX-XX-XX-XX-XX-XX.cluster..."

<?php
require 'vendor/autoload.php'; // include Composer's autoloader

$SSL_DIR = "/home/ubuntu";
$SSL_FILE = "rds-combined-ca-bundle.pem";

$ctx = stream_context_create(array(
    "ssl" => array( 
        "cafile" => $SSL_DIR . "/" . $SSL_FILE,
    ))
);

$client = new MongoDB\Client("mongodb://<yourUserName>:<yourPassword>@docdb-2019-01-29-02-53-18.cluster-ccuszbx3pn5e.us-east-1.docdb.amazonaws.com:27017", array("ssl" => true), array("context" => $ctx));

$col = $client->test->col;

$result = $col->insertOne( [ 'hello' => 'Amazon DocumentDB'] );

$result = $col->findOne(array('hello' => 'Amazon DocumentDB'));

print_r($result);

?>
Joseph Idziorek
  • 4,853
  • 6
  • 23
  • 37
  • This is almost accurate but you forgot to mention that you must download the ssl file using the following `wget https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem` and store it in the certs directory in Linux2 `/etc/ssl` and change the `$SSL_DIR` to point to that linux directory. – Mushangi Derrick Jul 31 '19 at 22:49
0

If you are behind firewall and maintains whitelist for network ports,then you should have to open port 27017 to connect to MongoDB cluster.