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
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:
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:
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
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, runphp artisan config:clear
every time you make a change in your config file.