Skip to content

Use MongoDB as a secondary databases and it is failed to connect. It said "authenticated failed" #2207

@achievement-sambo-visal

Description

  • Laravel-mongodb Version: "jenssegers/mongodb": "^3.8"
  • PHP Version: 7.4.15 (cli) (built: Feb 2 2021 20:47:36) ( NTS Visual C++ 2017 x64 )
  • Database Driver & Version:

Description:

In my application there are two databases for the first one I connected with localhost phpMyAdmin and it connects successfully. Now I am trying to add another database as a secondary in my application but it failed to connect.

Here are list of databases in .env file

DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=sql_dev
DB_USERNAME=sql_dev
DB_PASSWORD=my_password


MONGO_DB_HOST=127.0.0.1
MONGO_DB_PORT=27017
MONGO_DB_DATABASE=mongo_analytic
MONGO_DB_USERNAME=mongo_user
MONGO_DB_PASSWORD=password```

==================================================
And this is config `database.php`

```'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],
  'mongodb' => [
            'driver' => 'mongodb',
            'host' => env('MONGO_DB_HOST', 'localhost'),
            'port' => env('MONGO_DB_PORT', 27017),
            'database' => env('MONGO_DB_DATABASE',),
            'username' => env('MONGO_DB_USERNAME',),
            'password' => env('MONGO_DB_PASSWORD',),
            'options' => [
                'database' =>  env('MONGO_DB_DATABASE')
            ],
        ],

Initially, I created an account in MongoDB website
If you navigate to Database Access there will be a feature for you to create Database User, and then I created a database user as admin role and I made:
username = mongo_user
password: password
as you can see in my .env file.

I also downloaded MongoDB Compass and connect with username and password and it works fine. But now I am trying to connect with my application and it is not working.

I also followed the tutorial on how to connect multiple databases as well stackoverflow

I also named the connection as well.

public function up()
    {
        Schema::connection('mongodb')->create('analytics', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

php artisan migrate --database=mongodb after this command


  MongoDB\Driver\Exception\AuthenticationException 
  Authentication failed.

  at C:\project_path\vendor\mongodb\mongodb\src\Command\ListCollections.php:114
    110▕         if (isset($this->options['nameOnly'])) {
    111▕             $cmd['nameOnly'] = $this->options['nameOnly'];
    112▕         }
    113▕
  ➜ 114▕         $cursor = $server->executeReadCommand($this->databaseName, new Command($cmd), $this->createOptions());
    115▕         $cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
    116▕
    117▕         return new CachingIterator($cursor);
    118▕     }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions