Closed elangosundar closed 4 years ago
The options you are mentioning are for the MongoDB shell and can be replicated using connection string options, which are listed in the MongoDB documentation.
For the authentication options (username
, password
, authSource
, and authMechanism
) we supply configuration options under the options
for a connection:
doctrine_mongodb:
connections:
default:
[...]
options:
username: "someUser"
password: "superSecret"
Please note that the MongoDB ODM is built on top of the MongoDB PHP driver, which doesn't support "mongodb-like" databases like CosmosDB or Amazon DocumentDB, so your mileage with these options may vary. That said, if my memory serves me correct you should be able to see a connection string that you can use to connect to the database in question.
@alcaeus thanks for your info. I can able to connect the db through mongo shell, but integrating to doctrine odm unable to connect.
I have attached the sample amazon mongodb connection script for below. https://docs.aws.amazon.com/documentdb/latest/developerguide/connect.html#connect-tls-enabled
<?php
//Include Composer's autoloader
require 'vendor/autoload.php';
//Create a MongoDB client and open connection to Amazon DocumentDB
$client = new MongoDB\Client("mongodb://<dbusername>:<dbpassword>@mycluster.node.us-east-1.docdb.amazonaws.com:27017");
//Specify the database and collection to be used
$col = $client->test->col;
//Insert a single document
$result = $col->insertOne( [ 'hello' => 'Amazon DocumentDB'] );
//Find the document that was previously written
$result = $col->findOne(array('hello' => 'Amazon DocumentDB'));
//Print the result to the screen
print_r($result);
How to integrated this script in symfony by using doctrine odm.
The documentation that you have linked contains one section on how to connect with TLS enabled and one with TLS disabled. I can see that you took the $client = ...
line from the second example. If you are trying to connect with TLS enabled, please use the code from the first example.
@alcaeus The both example are working fine for plain php script, but i didn't know how to integrate in symfony doctrine way.
Ah, I see. The new version of the documentation has a section on how to inject a context service: https://symfony.com/doc/master/bundles/DoctrineMongoDBBundle/config.html#specifying-a-context-service. This is equivalent to the documentation.
However, since we are in the process of deprecating the context
driver option (see https://github.com/mongodb/mongo-php-driver/pull/1040), you may want to use the tlscafile
URI option instead:
$SSL_DIR = "/home/ubuntu";
$SSL_FILE = "rds-combined-ca-bundle.pem";
$client = new MongoDB\Client("mongodb://<dbusername>:<dbpassword>@mycluster.node.us-east-1.docdb.amazonaws.com:27017?tls=true&tlscafile=" . $SSL_DIR . "/" . $SSL_FILE));
The resulting connection string can be used in the configuration for the bundle.
@alcaeus I have tried as you mentioned in plain php, but it is not working.
$client = new \MongoDB\Driver\Manager("mongodb://<dbusername>:<dbpassword>@mycluster.node.us-east-1.docdb.amazonaws.com:27017/?tls=true&tlscafile=" . $SSL_DIR . "/" . $SSL_FILE);
$query = new \MongoDB\Driver\Query(['name' => 'product']);
$cursor = $client->executeQuery('test', $query);
print_r($cursor->toArray());
Throwing error as
"Uncaught MongoDB\Driver\Exception\ConnectionTimeoutException: No suitable servers found (`serverSelectionTryOnce` set): [socket timeout calling".
$client = new \MongoDB\Driver\Manager("mongodb://<dbusername>:<dbpassword>@mycluster.node.us-east-1.docdb.amazonaws.com:27017', ["ssl" => true], ["context" => $ctx]);
$query = new \MongoDB\Driver\Query(['name' => 'product']);
$cursor = $client->executeQuery('test', $query);
print_r($cursor->toArray());
Note : i am using correct ssh db username and pwd, ssh ca file also Let me know, if i am missing anything...!
Which version of the driver are you using? The tls
options were only introduced in libmongoc 1.15.0, which was first used in ext-mongodb
1.6.0. If you are using an older version, please use ssl
and sslcertificateauthorityfile
instead of their TLS counterparts.
Also note that using the context will work fine for now, as it's being deprecated in the next minor release and won't be removed until 2.0.0 of the MongoDB driver for PHP is released. I've linked the docs about using a context service in the bundle in my previous comment.
@elangovanshanthi: Sorry to pile on, but if you're unable to upgrade the mongodb
extension to 1.6.0, you can still be avoid using the context
driver option. All five of the supported options within context
are fallbacks for other options that you can specify directly in the driver options (i.e. third argument to MongoDB\Driver\Manager
constructor, or MongoDB\Client
for the library). You can find a table of these fallbacks in the context
description within the Manager::__construct()
docs. For instance, you could specify a ca_file
driver option instead of using context
with a cafile
.
That said, upgrading the extension to 1.6.0 is surely preferable, as most of these can now be specified directly in the connection string or URI options (i.e. second argument to the constructor). The only exceptions there will be ca_dir
and crl_file
driver options, as they are still specific to libmongoc (used by PHP) and not supported across all drivers (as the URI options are).
Which version of the driver are you using? The
tls
options were only introduced in libmongoc 1.15.0, which was first used inext-mongodb
1.6.0. If you are using an older version, please usessl
andsslcertificateauthorityfile
instead of their TLS counterparts.Also note that using the context will work fine for now, as it's being deprecated in the next minor release and won't be removed until 2.0.0 of the MongoDB driver for PHP is released. I've linked the docs about using a context service in the bundle in my previous comment.
I have tried the ssl and sslcertificateauthorityfile options and it is works fine in plain php, but the same script below script execute in symfony service throwing no suitable server error
.
$client = new \MongoDB\Driver\Manager("mongodb://<dbusername>:<dbpassword>@mycluster.node.us-east-1.docdb.amazonaws.com:27017/?ssl=true&sslcertificateauthorityfile=" . $SSL_DIR . "/" . $SSL_FILE);
$query = new \MongoDB\Driver\Query(['name' => 'product']);
$cursor = $client->executeQuery('test', $query);
print_r($cursor->toArray());
I am using version mentioned in below:
"php": "^7.1.3"
"alcaeus/mongo-php-adapter": "^1.1",
"ext-mongo": "1.6.16"
"ext-mongoDB": "1.5.3"
Let you give some example for symfony with doctrine odm ?
Sorry for the delay, this issue managed to sneak under my radar.
I have tried the ssl and sslcertificateauthorityfile options and it is works fine in plain php, but the same script below script execute in symfony service throwing
no suitable server error
.
I'm not sure what you mean - if this snippet works in a plain PHP file, it should also work in a Symfony service, given the variables you use have the same value. Either way, you can use the mongodb.debug
ini setting to generate a debug log and examining the log. For one, this will show you the connection string the driver attempts to connect to, but will also show a trace of what's happening behind the scenes.
Please note that the debug log can contain sensitive information, such as credentials to the MongoDB server and full documents written to or read from the server. Please review any debug logs before sharing them here or in any other public forum.
Sorry for the delay, this issue managed to sneak under my radar.
I have tried the ssl and sslcertificateauthorityfile options and it is works fine in plain php, but the same script below script execute in symfony service throwing
no suitable server error
.I'm not sure what you mean - if this snippet works in a plain PHP file, it should also work in a Symfony service, given the variables you use have the same value. Either way, you can use the
mongodb.debug
ini setting to generate a debug log and examining the log. For one, this will show you the connection string the driver attempts to connect to, but will also show a trace of what's happening behind the scenes.Please note that the debug log can contain sensitive information, such as credentials to the MongoDB server and full documents written to or read from the server. Please review any debug logs before sharing them here or in any other public forum.
I have installed the package information in my server instance and please find in below:
php --ri mongodb
PHP Version => 7.2.18
MongoDB shell version v4.0.5
MongoDB extension version => 1.5.3
libbson bundled version => 1.13.0
libmongoc bundled version => 1.13.0
Config for composer.json
"doctrine/mongodb-odm-bundle": "^3.5",
"alcaeus/mongo-php-adapter": "^1.1"
I have used the connection code as below:
$client = new \MongoDB\Driver\Manager("mongodb://<dbusername>:<dbpassword>@mycluster.node.us-east-1.docdb.amazonaws.com:27017/?ssl=true&sslcertificateauthorityfile=" . $SSL_DIR . "/" . $SSL_FILE);
$query = new \MongoDB\Driver\Query(['name' => 'product']);
$cursor = $client->executeQuery('test', $query);
print_r($cursor->toArray());
I believe this infomrations will helpful to resolve my queries.
@elangovanshanthi: Your comment above doesn't address @alcaeus's original comment:
If this snippet works in a plain PHP file, it should also work in a Symfony service, given the variables you use have the same value.
To quote your own comment from two replies back:
I have tried the ssl and sslcertificateauthorityfile options and it is works fine in plain php, but the same script below script execute in symfony service throwing no suitable server error.
You need to clarify exactly what "plain php" refers to. Is that running the script via the PHP CLI process, or is using the same web SAPI as your Symfony app and just running a four-line PHP file accessed through the web browser? If there is any doubt whatsoever, you should run phpinfo()
within the working script the determine the exact PHP runtime and extension versions.
Secondly, @alcaeus referred you to the mongodb.debug
INI setting, which you can use to capture debug logs of the SSL handshake. You will need to redact any sensitive information from the logs, but sharing copies of a log generated by both the working and failing scripts would be helpful.
This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in a week if no further activity occurs. Thank you for your contributions.
I can able to connect using the aws mongodb connection using symfony4 doctrine..! Thanks for you help :)
Symfony - Connect mongo documentDB using ssl params
I am trying to connect amazon mongo documentDB using
ssl params
like--sslCAFile, --username, --password and --host
in symfony.Can you provide example for connecting mongo documentDB in symfony4?
version : "doctrine/mongodb-odm-bundle": "^3.5" Symfony4