Skip to content
Advertisement

Connecting MongoDB to the project – error

I am trying to connect the MongoDB database to my project in Laravel 7.0 using Docker. I have followed this guide

LINK

Unfortunately, not everything goes as planned. I cannot run the command

php artisan migrate

because an error occurs

   MongoDBDriverExceptionConnectionTimeoutException 

  No suitable servers found (`serverSelectionTryOnce` set): [Invalid reply from server. calling ismaster on '127.0.0.1:50003']

  at vendor/mongodb/mongodb/src/functions.php:431
    427|         // TODO: PHPLIB-476: Read transaction read preference once PHPC-1439 is implemented
    428|         $readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
    429|     }
    430| 
  > 431|     return $manager->selectServer($readPreference);
    432| }
    433| 

      +24 vendor frames 
  25  artisan:37
      IlluminateFoundationConsoleKernel::handle()

The same error occurs when trying to connect to the database when using the registration or login options on the website.

My .env file

DB_CONNECTION=mongodb
DB_HOST=localhost
DB_PORT=50003
DB_DATABASE=dbname
DB_USERNAME=username
DB_PASSWORD=password

My database.php file

'mongodb' => [
            'driver' => 'mongodb',
            'host' => env('DB_HOST', 'localhost'),
            'port' => 50003,
            'database' => env('DB_DATABASE', 'dbname'),
            'username' => env('DB_USERNAME', 'username'),
            'password' => env('DB_PASSWORD', 'password'),
            'options' => [
                // here you can pass more settings to the Mongo Driver Manager
                // see https://www.php.net/manual/en/mongodb-driver-manager.construct.php under "Uri Options" for a list of complete parameters that you can use
        
                'database' => env('DB_AUTHENTICATION_DATABASE', 'admin'), // required with Mongo 3+
            ],
        ],

My User.php file

namespace App;

use IlluminateNotificationsNotifiable;
use JenssegersMongodbEloquentModel as Eloquent;
use IlluminateAuthAuthenticatable as AuthenticableTrait;
use IlluminateAuthPasswordsCanResetPassword;
use IlluminateContractsAuthAuthenticatable as AuthenticatableContract;
use IlluminateContractsAuthCanResetPassword as CanResetPasswordContract;

class User extends Eloquent implements AuthenticatableContract, CanResetPasswordContract
{
    use AuthenticableTrait;
    use Notifiable;
    use CanResetPassword;

    protected $connection = 'mongodb';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];
...

My docker-compose.yml file

version: '3.1'

services:
  mongo:
    image: mongo
    restart: always
    container_name: Mongo_DB
    environment:
      MONGO_INITDB_ROOT_USERNAME: username
      MONGO_INITDB_ROOT_PASSWORD: password
    ports:
      - '27018:27017'

  mongo-express:
    image: mongo-express
    restart: always
    container_name: Mongo_Express
    ports:
      - 50003:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: username
      ME_CONFIG_MONGODB_ADMINPASSWORD: password

EDIT: when I changed the port in the .env and database.php file to 27017 and deleted DB_USERNAME and DB_PASSWORD in the .env file, and in the database.php file I left a blank space between ” where you had to enter your username and password, I started to connect.

Now the problem is that when I want to enter 127.0.0.1: 27017 I have this message

It looks like you are trying to access MongoDB over HTTP on the native driver port.

What’s wrong?

Advertisement

Answer

Answer:

Port in .env & database.php must be 27018 not 50003

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement