Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gabssama's avatar

Laravel MongoDB - command delete requires authentication

Developing a Laravel app using Mongodb as database, I came accross a weird issue today.

Somehow, when I use the app, anywhere I need to use a mongo command I get a "command requires authentication". Which obviously means I am not connected to the db but "php artisan migrate --database=mongodb" does work with no problem, all indexes etc are created.

Using the same credentials as in my .env I can connect to the DB using Compass.

Is there any reason the app would use a different config than the migrate command?

For reference:

config/database.php
'mongodb' => [
            'driver' => 'mongodb',
            'host' => env('MONGO_DB_HOST', 'localhost'),
            'dsn' => env('MONGO_DB_DSN', 'mongodb://') . env('MONGO_DB_HOST', 'localhost'),
            'port' => 27017,
            'database' => env('MONGO_DB_DATABASE', 'rte_golf'),
            'username' => env('MONGO_DB_USERNAME'),
            'password' => env('MONGO_DB_PASSWORD'),
            'options' => [
                'database' => env('DB_AUTHENTICATION_DATABASE', 'admin'), // required with Mongo 3+
            ],
        ],
.env
MONGO_DB_HOST=mongodb
MONGO_DB_DATABASE=rte_golf
MONGO_DB_USERNAME=username
MONGO_DB_PASSWORD=password
docker-compose creating the mongo image
mongodb:
    image: mongo
    restart: always
    ports:
      - "27017:27017"
    environment:
      MONGO_INITDB_ROOT_USERNAME: username
      MONGO_INITDB_ROOT_PASSWORD: password
    networks:
      - backoffice
0 likes
0 replies

Please or to participate in this conversation.