Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Discussion options

  • 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▕ }
You must be logged in to vote

You may change the authSource to your database name.

 'options' => [
 'authSource' => env('MONGO_DB_DATABASE'),
 ],

Replies: 3 comments 8 replies

Comment options

Did you manage to get it work ?

You must be logged in to vote
1 reply
Comment options

@lironesamoun have you managed to resolve this? If you recall, could you please assist me?

Comment options

have you managed to resolve this? If you recall, could you please assist me?

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
7 replies
Comment options

1.my laravel/mongodb version is 3.8.
Laravel version is 8
2.its remote
3. yup its working fine and i see all the collections via Compass.
4.

Comment options

What is your configuration? The documentation for your version is here: https://github.com/mongodb/laravel-mongodb/blob/3.8/README.md#configuration

Comment options

My default database is MySQL, However, I need to connect a second database (MongoDB).

config/database.php looks like this :


<?php
use Illuminate\Support\Str;
return [
 /*
 |--------------------------------------------------------------------------
 | Default Database Connection Name
 |--------------------------------------------------------------------------
 |
 | Here you may specify which of the database connections below you wish
 | to use as your default connection for all database work. Of course
 | you may use many connections at once using the Database library.
 |
 */
 'default' => env('DB_CONNECTION', 'mysql'),
 /*
 |--------------------------------------------------------------------------
 | Database Connections
 |--------------------------------------------------------------------------
 |
 | Here are each of the database connections setup for your application.
 | Of course, examples of configuring each database platform that is
 | supported by Laravel is shown below to make development simple.
 |
 |
 | All database work in Laravel is done through the PHP PDO facilities
 | so make sure you have the driver for your particular database of
 | choice installed on your machine before you begin development.
 |
 */
 'connections' => [
 'sqlite' => [
 'driver' => 'sqlite',
 'url' => env('DATABASE_URL'),
 'database' => env('DB_DATABASE', database_path('database.sqlite')),
 'prefix' => '',
 'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
 ],
 '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', ''),
 'charset' => 'utf8mb4',
 'collation' => 'utf8mb4_unicode_ci',
 'prefix' => '',
 'prefix_indexes' => true,
 'strict' => true,
 'engine' => null,
 'options' => extension_loaded('pdo_mysql') ? array_filter([
 PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
 ]) : [],
 ],
 'pgsql' => [
 'driver' => 'pgsql',
 'url' => env('DATABASE_URL'),
 'host' => env('DB_HOST', '127.0.0.1'),
 'port' => env('DB_PORT', '5432'),
 'database' => env('DB_DATABASE', 'forge'),
 'username' => env('DB_USERNAME', 'forge'),
 'password' => env('DB_PASSWORD', ''),
 'charset' => 'utf8',
 'prefix' => '',
 'prefix_indexes' => true,
 'schema' => 'public',
 'sslmode' => 'prefer',
 ],
 'sqlsrv' => [
 'driver' => 'sqlsrv',
 'url' => env('DATABASE_URL'),
 'host' => env('DB_HOST', 'localhost'),
 'port' => env('DB_PORT', '1433'),
 'database' => env('DB_DATABASE', 'forge'),
 'username' => env('DB_USERNAME', 'forge'),
 'password' => env('DB_PASSWORD', ''),
 'charset' => 'utf8',
 'prefix' => '',
 'prefix_indexes' => true,
 ],
 'mongodb' => [
 'driver' => 'mongodb',
 'dsn' => env('MONGO_DB_URI'),
 'host' => env('MONGO_DB_HOST', '127.0.0.1'),
 'port' => env('MONGO_DB_PORT', 27017),
 'database' => env('MONGO_DB_DATABASE'),
 'username' => env('MONGO_DB_USERNAME'),
 'password' => env('MONGO_DB_PASSWORD'),
 'options' => [
 'authSource' => 'admin', // authentication database if needed
 ],
 ],
 ],
 /*
 |--------------------------------------------------------------------------
 | Migration Repository Table
 |--------------------------------------------------------------------------
 |
 | This table keeps track of all the migrations that have already run for
 | your application. Using this information, we can determine which of
 | the migrations on disk haven't actually been run in the database.
 |
 */
 'migrations' => 'migrations',
 /*
 |--------------------------------------------------------------------------
 | Redis Databases
 |--------------------------------------------------------------------------
 |
 | Redis is an open source, fast, and advanced key-value store that also
 | provides a richer body of commands than a typical key-value system
 | such as APC or Memcached. Laravel makes it easy to dig right in.
 |
 */
 'redis' => [
 'client' => env('REDIS_CLIENT', 'phpredis'),
 'options' => [
 'cluster' => env('REDIS_CLUSTER', 'redis'),
 'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
 ],
 'default' => [
 'url' => env('REDIS_URL'),
 'host' => env('REDIS_HOST', '127.0.0.1'),
 'password' => env('REDIS_PASSWORD', null),
 'port' => env('REDIS_PORT', '6379'),
 'database' => env('REDIS_DB', '0'),
 ],
 'cache' => [
 'url' => env('REDIS_URL'),
 'host' => env('REDIS_HOST', '127.0.0.1'),
 'password' => env('REDIS_PASSWORD', null),
 'port' => env('REDIS_PORT', '6379'),
 'database' => env('REDIS_CACHE_DB', '1'),
 ],
 ],
];
Comment options

You may change the authSource to your database name.

 'options' => [
 'authSource' => env('MONGO_DB_DATABASE'),
 ],
Answer selected by GromNaN
Comment options

ohh man!!! Thank you so much you solved my problem!!! everything is working with Eloquent..

I am following you!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
Converted from issue

This discussion was converted from issue #2207 on February 23, 2021 04:09.

AltStyle によって変換されたページ (->オリジナル) /