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

Created on 7 Aug 2016  ·  20Comments  ·  Source: jenssegers/laravel-mongodb

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

use JenssegersMongodb\Eloquent\HybridRelations;
use JenssegersMongodb\EloquentModel 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
*/

Most helpful comment

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

All 20 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

Worked for me.
Thank you @jaimeescano

Cheers

Thanks @jaimeescano.

Perfect, is that correct!!!!

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

Hi oaigudmundsson,

Try defining the port as integer value. Don't use double quotes. Probably mongo require a number and no a string.

Regards
Jaime

Hello

I did use as string and stilö same issue.

Den 12 juni 2017 17:33 skrev "jaimeescano" notifications@github.com:

Hi oaigudmundsson,

Try defining the port as integer value. Don't use double quotes. Probably
mongo require a number and no a string.

Regards
Jaime


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/jenssegers/laravel-mongodb/issues/911#issuecomment-307826470,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAmzt9nunUkW2YK6yue7lU53gk6FNMZaks5sDVpIgaJpZM4JejBZ
.

You mean, that you used as INTEGER? I suggested the opposite, to use INTEGER

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

or for testing purposes, you could directly do the following:

database.php

'mongodb' => [
'driver' => 'mongodb',
'host' => env('MONGO_HOST'),
'port' => 21212,
'database' => env('MONGO_DATABASE'),
'username' => env('MONGO_USERNAME'),
'password' => env('MONGO_PASSWORD'),
],

Sorry, i meant as int ans str. I did test both cases and it still didn't
connect. Let me send some screenshot tomorrow. And yes, i did even type
direct to database.php. Weird!

Den 12 juni 2017 22:05 skrev "jaimeescano" notifications@github.com:

You mean, that you used as INTEGER? I suggested the opposite, to use
INTEGER

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

or for testing purposes, you could directly do the following:

database.php

'mongodb' => [
'driver' => 'mongodb',
'host' => env('MONGO_HOST'),
'port' => 21212,
'database' => env('MONGO_DATABASE'),
'username' => env('MONGO_USERNAME'),
'password' => env('MONGO_PASSWORD'),
],


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/jenssegers/laravel-mongodb/issues/911#issuecomment-307893822,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAmzt_2QAqAF0sUTMcAfI0wsHy5R_g51ks5sDZCtgaJpZM4JejBZ
.

Have you tried to specify the port at the host parameter??

MONGO_HOST="ds121212.mlab.com:21212/abc"

Not sure, but if that works .. then the "abc" will need to be defined as replica set attribute

yes, i did write on my first comment, that i got it work by ADDING the port
on host string.

Mongolabs host free sandbox and they don't have replica.

Cheers

Den 13 juni 2017 02:50 skrev "jaimeescano" notifications@github.com:

Have you tried to specify the port at the host parameter??

MONGO_HOST="ds121212.mlab.com:21212/abc"

Not sure, but if that works .. then the "abc" will need to be defined as
replica set attribute


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/jenssegers/laravel-mongodb/issues/911#issuecomment-307952682,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAmzt_nHzFJEUh4sR6RHmcFH5TIO0aQtks5sDbyKgaJpZM4JejBZ
.

Not working
MONGO_HOST="ds121212.mlab.com/ABC"
MONGO_PORT=21212 // this is useless?

Error message:
No suitable servers found (serverselectiontryonce set): [Failed connecting to 'ds121212.mlab.com:27017': Connection refused]

Working
MONGO_HOST="ds121212.mlab.com:21212/ABC"
MONGO_PORT=21212 // this is useless?

So ... if the second one is working, your configuration to use the port should be:

database.php
'mongodb' => [
'driver' => 'mongodb',
'host' => env('MONGO_HOST'),
'port' => env('MONGO_PORT', 27017),
'database' => env('MONGO_DATABASE'),
'username' => env('MONGO_USERNAME'),
'password' => env('MONGO_PASSWORD'),
]

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

I don't see where 27017 is and it must be set default mongodb port i guess?

Notice:
MONGO_HOST="ds121212.mlab.com:21212/abc"
_abc must be there, otherwise_

(1/1) AuthenticationException
Authentication failed.

Connection string must be like this:
mongodb://:@ds121212.mlab.com:21212/abc

Here is the files which are working:

My 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
MONGO_HOST="ds121212.mlab.com:21212/abc"
MONGO_PORT=21212
MONGO_DATABASE=abc
MONGO_USERNAME=abc_user
MONGO_PASSWORD=secret

This one works for me

       `DB_CONNECTION=mongodb
        DB_HOST=ianruhjkssel:[email protected]:15hjk701/express-tutorial-sandbox
        DB_DATABASE=express-tutorial-sandbox
        DB_USERNAME=ianruhjkssel
        DB_PASSWORD=riberrmayhjkjhka1`

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.

Thank you @zahirkelloud. php artisan config:clear has solved it. I was banging my head.

SELinux (CentOS7/8) can cause this error, applying the following rule can resolve it:

setsebool -P httpd_can_network_connect_db 1

i have same errors, please someone help me
image

Hello!I have the same issue.
MongoDB\Driver\Exception\ConnectionTimeoutException: No suitable servers found (serverSelectionTryOnce set): [connection refused calling ismaster on 'localhost:27017']
There is my config:
database:
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_MONGO_HOST', ''),
'port' => env('DB_MONGO_PORT', ''),
'database' => env('DB_MONGO_DATABASE', ''),
'username' => env('DB_MONGO_USERNAME', ''),
'password' => env('DB_MONGO_PASSWORD', ''),
'options' => [
'database' => env('DB_MONGO_DATABASE', 'admin'),
]
],

.env
DB_MONGO_CONNECTION=mongodb
DB_MONGO_HOST=localhost
DB_MONGO_PORT=27017

But I use DataGrip or command can connect and visit mongo.
微信截图_20220113111709
image

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pirmax picture pirmax  ·  3Comments

ricardofontanelli picture ricardofontanelli  ·  3Comments

tomartailored picture tomartailored  ·  3Comments

sebastiaanluca picture sebastiaanluca  ·  3Comments

kschethan picture kschethan  ·  3Comments