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

No suitable servers found (`serverSelectionTryOnce` set) #1469

Closed ajitdas123 closed 4 years ago

ajitdas123 commented 6 years ago

I am trying to connect to monogo db on localhost,but its giving me this error.


No suitable servers found (`serverSelectionTryOnce` set): [TLS handshake failed: error:00000000:lib(0)
:func(0):reason(0) calling ismaster on 'localhost:27017']
 at C:\Users\admin\Desktop\test\test\vendor\mongodb\mongodb\src\Database.php: 338
    334|      */
    335|     public function listCollections(array $options = [])
    336|     {
    337|         $operation = new ListCollections($this->databaseName, $options);
  > 338|         $server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
    339|
    340|         return $operation->execute($server);
    341|     }
    342|

  Exception trace:

  1   MongoDB\Driver\Manager::selectServer(Object(MongoDB\Driver\ReadPreference))
      C:\Users\admin\Desktop\test\test\vendor\mongodb\mongodb\src\Database.php : 338

  2   MongoDB\Database::listCollections()
      C:\Users\admin\Desktop\test\test\vendor\jenssegers\mongodb\src\Jenssegers\Mongodb\Schema\Builder.php : 44

  Please use the argument -v to see more details.

My settings are as follows for localhost :

'mongodb' => [
            'driver' => 'mongodb',
            'host' => 'localhost',
            'port'     => env('DB_MONGO_PORT', 27017),
            'database' => env('DB_MONGO_DATABASE'),
            'username' => env('DB_MONGO_USERNAME'),
            'password' => env('DB_MONGO_PASSWORD'),
        ],

I tried this with mongo alas with following settings

SETTING 01 :

'mongodb' => [
            'driver' => 'mongodb',
            'host' => [
                'example-shard-00-00-zhvkn.mongodb.net:27017',
                'example-shard-00-01-zhvkn.mongodb.net:27017',
                'example-shard-00-02-zhvkn.mongodb.net:27017'
            ],
            'database' => 'exampleDB',
            'username' => 'example',
            'password' => 'example',
            'options' => [
                'replicaSet' => 'example-shard-0',
                'ssl' => 'true',
                'authSource' => 'admin'
            ]
        ],

SETTING 02


 'mongodb' => [
            'driver' => 'mongodb',
            'dsn'=>'mongodb://username:password@host1,host2/database?ssl=true&replicaSet=replicaSet&authSource=admin',
            'database' => 'exampleDB',
        ],

Can anyone please guide me how can i fix this issue.

PS: i can access to these database with mongodb compass and can work with at ease so, my guessing is the database is working fine. its just the sttings or something.

Thanks

libasoles commented 6 years ago

Did you solve this? I'm in the same stage.

vitorhugoro1 commented 6 years ago

I have a same problem, but it's only happen in Laravel 5.6, in 5.5 and lower, it's don't happen

libasoles commented 6 years ago

Ok, for me, the solution was removing the user/pass:

  'username' => 'example',
  'password' => 'example',

It seems there's no need for it.

vitorhugoro1 commented 6 years ago

I use the second setting and don't work, I tried downgrade the mongodb composer lib, but only downgrade the Laravel and laravel-mongodb lib work to me. I dump the laravel-mongodb lib to get who sended to mongodb lib and was correct.

ajitdas123 commented 6 years ago

@libasoles Hi can you please share your complete settings :)) I hope that is ok. I still could not find the solution

george-viaud commented 5 years ago

Same problem here trying to connect to AWS DocumentDB - I believe the problem is the driver can't verify the cert vs a known PEM. I have been trying to use the 'driver_options' => [ 'context' => stream_context_create([ 'ssl' => [ block but this causes further issues for me stating the context isn't valid, though it really does appear to be. Anyone have any further luck?

george-viaud commented 5 years ago

Please see if this helps - if it does, please let me know / comment on the PR, would love to get it merged. GH-1751