mongodb / laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
https://www.mongodb.com/compatibility/mongodb-laravel-integration
MIT License
6.99k stars 1.42k forks source link

Issue with Failed to look up SRV record #1879

Closed tw0325795 closed 4 years ago

tw0325795 commented 4 years ago

I meet an issue of mongodb.atlas I used +srv connect to my cluster But I get this error MongoDB\Driver\Exception\ConnectionTimeoutException: Failed to look up SRV record "_mongodb._tcp.xxxxxxxxx-xxxxx.mongodb.net": Operation not permitted in file /home/vagrant/Code/xxxxxxxx/vendor/mongodb/mongodb/src/Collection.php on line 866 My mongodb is Version 4.0.13 My controller

DB::collection('users')->insert([
        'name'  =>  'tom',
        'age'   =>  18
]);
$res = DB::collection('users')->all();
dd($res);

My database.php

'mongodb' => [
        'driver'   => 'mongodb',
        'dsn' => env('DB_DSN'),
        'database' => env('DB_DATABASE'),
],

My .env

DB_CONNECTION=mongodb
DB_DATABASE=xxxxxxx
DB_DSN=mongodb+srv://xxxxxxxx:xxxxxxxxx@xxxxxx-xxxx.mongodb.net/test?retryWrites=true&w=majority

How can I do?

Giacomo92 commented 4 years ago

Hi @tw0325795, I think you are trying to connect with MongoDBAtlas.

Does your password contains "@" or other symbols? They conflict in the dsn parsing on MongoPHP Driver. Try to change the password with one that only contains number and letter.

Moreover you have to change test

mongodb+srv://xxxxxxxx:xxxxxxxxx@xxxxxx-xxxx.mongodb.net/test?retryWrites=true&w=majority

with the name of your database env('DB_DATABASE')

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!

mit-tact commented 4 years ago

@whobutsb did you able to solve the issue? I'm getting same errors.

mit-tact commented 4 years ago

Hi The issue is solved. Remember never to add "@" in the password or username of cluster. This will resolve "Failed to look up SRV record " error.

Then I got this error "No suitable servers found (serverSelectionTryOnce set)". As I created and use a connection string from another project/cluster with password containing only albhabet and number. So, I solved it by using the previous connection string but with edited password. I solved the issue

divine commented 4 years ago

Glad this has been resolved.

Closing.