Laravel-mongodb: Connection issue

Created on 24 Oct 2018  Â·  16Comments  Â·  Source: jenssegers/laravel-mongodb

I am trying to connect to mongodb to my live server

ec2-xx-xxx-xxx-xx.compute-1.amazonaws.com

but i get this error 

No suitable servers found (`serverSelectionTryOnce` set): [connection refused calling ismaster on '127.0.0.1:27017']

As it should be connecting to my EC2 server not localhost i have config on ENV and Database.php

my database.php

'mongodb' => [
            'driver'   => 'mongodb',
             'host'     => 'ec2-xx-xxx-xxx-xx.compute-1.amazonaws.com',
            'port'     => env('DB_PORT', 27017),
            'database' => "xxxx",
            'username' => "",
            'password' => "",

        ],

and my env 


DB_CONNECTION=mongodb
DB_HOST=ec2-xx-xxx-xxx-xx.compute-1.amazonaws.com
DB_PORT=27017
DB_DATABASE=xxxx
DB_USERNAME=
DB_PASSWORD=
question awaiting-author

Most helpful comment

I was having the same issue with MongoDB Atlas, and I solved this issue by using the 'dsn' option in my database configuration rather than setting host, port, username and password separately:

database.php

'mongodb_atlas' => [
    'driver' => 'mongodb',
    'dsn' => 'mongodb+srv://admin:<password>@clusterxx-xxxxx.mongodb.net/test?retryWrites=true&w=majority',
    'database' => env('DB_DATABASE'),
    'options' => [
        'database' => 'admin'
    ]
]

.env

DB_CONNECTION=mongodb_atlas
DB_DATABASE=xxxx

All 16 comments

Did it worked ? I am having the same issue with MongoDB Atlas.

I was having the same issue with MongoDB Atlas, and I solved this issue by using the 'dsn' option in my database configuration rather than setting host, port, username and password separately:

database.php

'mongodb_atlas' => [
    'driver' => 'mongodb',
    'dsn' => 'mongodb+srv://admin:<password>@clusterxx-xxxxx.mongodb.net/test?retryWrites=true&w=majority',
    'database' => env('DB_DATABASE'),
    'options' => [
        'database' => 'admin'
    ]
]

.env

DB_CONNECTION=mongodb_atlas
DB_DATABASE=xxxx

@mertcanekiz Thank you! I searched the internet for hours before I decided to check the issues list here.

Your solution did indeed resolve my issue!

Just curious, are there docs on this?

Has anyone been able to get this to work? I can't seem to connect using the following config:

[
  "driver" => "mongodb",
  "dsn" => "mongodb+srv://username:[email protected]/database-production?retryWrites=true&w=majority",
  "database" => "database-production",
  "options" => [
    "database" => "database-production",
    "ssl" => true,
  ],
]

The error message I receive is:

No suitable servers found (`serverSelectionTryOnce` set): [Failed to receive length header from server. calling ismaster on 'companyname-shard-xxxxxxx.mongodb.net:27017'] [Failed to receive length header from server. calling ismaster on 'companyname-shard-xxxxx.mongodb.net:27017']

I'm using PHP 7.2 and MongoDB-1.7.0. Thanks!

@whobutsb, set dsn to mongodb+srv://username:[email protected]/database?replicaSet=username-shard-0&connectTimeoutMS=10000&authSource=admin&authMechanism=SCRAM-SHA-1 for example

Thanks @Smolevich for the reply. Unfortunately its still giving me the same error message.

Mongodb runned by replicaset?

I believe so, I'm new to Mongo Atlas (moving from mLab), I just setup a M2 General Cluster and it shows to have 3 nodes in the replica set. Example address company-shard-00-02-0ybuy.mongodb.net:27017

Does work Connect to mongo by MongoDb Compass or 3T Studio ?

Just tried to connect using Robo3T and received the message: Set's primary
is unreachable. Reason: No member of the set is reachable

On Mon, Feb 10, 2020 at 3:32 PM Stas notifications@github.com wrote:

Does work Connect to mongo by MongoDb Compass or 3T Studio ?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/jenssegers/laravel-mongodb/issues/1632?email_source=notifications&email_token=AAGT6XU5MKNT47ORZOKHDVLRCHIZNA5CNFSM4F67ERD2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELKRRJA#issuecomment-584390820,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAGT6XX3MQMBAUUGEU6FOU3RCHIZNANCNFSM4F67ERDQ
.

--


Steve Barbera
[email protected]

You have to allow connection from your ip to Mongo Atlas. See here

Yup, and I allowed the whole world 0.0.0.0, just to be safe. Still no
dice. I'm working with mLab/Atlas support now. I really appreciate the
feedback and support!

On Mon, Feb 10, 2020 at 4:28 PM Giacomo Fabbian notifications@github.com
wrote:

You have to allow connection from your ip to Mongo Atlas. See here
https://docs.atlas.mongodb.com/security-whitelist/

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/jenssegers/laravel-mongodb/issues/1632?email_source=notifications&email_token=AAGT6XQ6NPAFCS3MBCSBNYDRCHPLJA5CNFSM4F67ERD2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELKWCXA#issuecomment-584409436,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAGT6XSQTP6SDBQ6NSOYMXDRCHPLJANCNFSM4F67ERDQ
.

--


Steve Barbera
[email protected]

Hi,
I am in the process of migrating mongodb from mLab to Mongo Atlas. I am using Laravel 5.2 and Jenssegers package. After Migration, I am unable to connect the MongoDB.
I am using the connection details as follows (config/database.php) :

    'mongodb' => [
        'driver'   => 'mongodb',
        'dsn' => "mongodb://<user>:<password>@<name>-shard-00-00.x9igi.mongodb.net:27017,<name>-shard-00-01.x9igi.mongodb.net:27017,<name>-shard-00-02.x9igi.mongodb.net:27017/<dbname>?ssl=true&replicaSet=atlas-4bdl0p-shard-0&authSource=admin&retryWrites=true&w=majority",
        'database' => '<dbname>',
    ],

I am getting the following error:

No suitable servers found (serverselectiontryonce set): [connection timeout calling ismaster on 'project-shard-00-00.x9igi.mongodb.net:27017'] [connection timeout calling ismaster on 'project-shard-00-01.x9igi.mongodb.net:27017'] [connection timeout calling ismaster on 'project-shard-00-02.x9igi.mongodb.net:27017']"

Can someone help me out of this?

I am using PHP 5.5 (This is an old project and is LIVE).

Thanks,

@vipinkavlar to connect to Atlas you have to use +srv "mongodb+srv://...."

@ScottSpittle That is for latest version I think, If I give +srv then it shows parse error. As said, I am using PHP 5.5 and Laravel 5.2

@ScottSpittle That is for latest version I think, If I give +srv then it shows parse error. As said, I am using PHP 5.5 and Laravel 5.2

@vipinkavlar I'm not sure what do you want exactly?

Migrating EOL software that doesn't receive security fixes 4 years?

Fixed version? It's already fixed just upgrade.

Can't upgrade? Well, you had pretty good time to upgrade to newer versions, question should be asked to your project why it still LIVE using EOL software risking your users/system?

--
Anyways, fork old version - apply fixes - done.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

HassanIbrahim picture HassanIbrahim  Â·  3Comments

naveedyasin picture naveedyasin  Â·  3Comments

pirmax picture pirmax  Â·  3Comments

BlakeGardner picture BlakeGardner  Â·  3Comments

yupangestu picture yupangestu  Â·  3Comments