mongodb / laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
https://www.mongodb.com/docs/drivers/php/laravel-mongodb/
MIT License
7.01k stars 1.43k forks source link

Connection issue #1632

Closed f4r4zas closed 4 years ago

f4r4zas commented 6 years ago

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=
chintanbanugaria commented 5 years ago

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

mertcanekiz commented 5 years ago

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
ScottSpittle commented 4 years ago

@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?

whobutsb commented 4 years ago

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:password@companyname-xxxx.mongodb.net/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!

Smolevich commented 4 years ago

@whobutsb, set dsn to mongodb+srv://username:password@username-hehop.mongodb.net/database?replicaSet=username-shard-0&connectTimeoutMS=10000&authSource=admin&authMechanism=SCRAM-SHA-1 for example

whobutsb commented 4 years ago

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

Smolevich commented 4 years ago

Mongodb runned by replicaset?

whobutsb commented 4 years ago

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

Smolevich commented 4 years ago

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

whobutsb commented 4 years ago

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 whobutsb@gmail.com

Giacomo92 commented 4 years ago

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

whobutsb commented 4 years ago

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 whobutsb@gmail.com

vipinkavlar commented 4 years ago

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,

ScottSpittle commented 4 years ago

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

vipinkavlar commented 4 years ago

@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

divine commented 4 years ago

@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.