laravel-mongodb: No suitable servers found (`serverselectiontryonce` set): [connection timeout calling ismaster on '127.0.0.1:3306']

No suitable servers found (serverselectiontryonce set): [connection timeout calling ismaster on ‘127.0.0.1:3306’]

use Jenssegers\Mongodb\Eloquent\HybridRelations; use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class Goods extends Eloquent { use HybridRelations; protected $connection = ‘mongodb’; // protected $connection = ‘mysql’; protected $collection = ‘tbke_goods’; // zk_final_price 价格 // volume 销量 // tk_rate //佣金比例 /** * 批量往mongo中存入数据 * @param $datas * @return bool */

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 1
  • Comments: 20

Most upvoted comments

Hi lipengyihao,

I had the same issue, at the end … I found that the issue was coming from the database.php file. I suppose, you are using a MySQL server as default connection, so most likely, you have your .env file pointing to the mysql settings.

As you can see from the database.php, it will try to load the following settings for the mongodb connection:

    'mongodb' => [
        'driver'   => 'mongodb',
        'host'     => env('DB_HOST', 'localhost'),
        'port'     => env('DB_PORT', 27017),

So, it will try to check if there is a DB_PORT defined at the .env file (so … laravel will find the port 3306) and load it from there, that’s way you are getting the MySQL port.

So you can modify your database.php and set it as:

    'mongodb' => [
        'driver'   => 'mongodb',
        'host'     => env('DB_HOST', 'localhost'),
        'port'     => 27017,

Or define a new parameter at the .env file like: DB_PORT_MONGO=27017 And then modify the database.php to load that parameter from your .env file.

Hope that helps. Regards, Jaime

This one works for me

       `DB_CONNECTION=mongodb
        DB_HOST=ianruhjkssel:riberrmayhjkjhka1@ds115kjhk01.mlab.com:15hjk701/express-tutorial-sandbox
        DB_DATABASE=express-tutorial-sandbox
        DB_USERNAME=ianruhjkssel
        DB_PASSWORD=riberrmayhjkjhka1`

Hello Not sure why but the solution above is not working.

database.php

‘mongodb’ => [ ‘driver’ => ‘mongodb’, ‘host’ => env(‘MONGO_HOST’), ‘port’ => env(‘MONGO_PORT’), ‘database’ => env(‘MONGO_DATABASE’), ‘username’ => env(‘MONGO_USERNAME’), ‘password’ => env(‘MONGO_PASSWORD’), ],

.env file MONGO_HOST=“ds121212.mlab.com/abc” MONGO_PORT=“21212” MONGO_DATABASE=abc MONGO_USERNAME=abc_user MONGO_PASSWORD=secret

Here is the error msg: No suitable servers found (serverselectiontryonce set): [Failed connecting to ‘ds121212.mlab.com:27017’: Connection refused]

The port doesn’t parse right as you can see. I can however make it connect by change the adding the port to HOST: ds121212.mlab.com/abc => ds121212.mlab.com:21212/abc

So, why did it ignore the port?

Cheers

I have been pulling my hair off because of a similar issue. I was sure that I have everything correct in my connection config. I spent hours trying to figure out where the issues is. It turns out that Laravel caches the configs so I had to run php artisan config:clear to clear the cached configuration. Only then I was able to make the connection to mongodb server. So for those who tried everything and didn’t work, run php artisan config:clear every time you make a change in your config file.