PHPSocialNetwork / phpfastcache

A high-performance backend cache system. It is intended for use in speeding up dynamic web applications by alleviating database load. Well implemented, it can drops the database load to almost nothing, yielding faster page load times for users, better resource utilization. It is simple yet powerful.
https://www.phpfastcache.com
MIT License
2.36k stars 452 forks source link

Mongodb Driver issue :: Authentification failed #883

Closed kekefreedog closed 1 year ago

kekefreedog commented 1 year ago

Problem

When I use mongodb driver with bellow config I have error log : Authentification failed

      host: 172.26.0.4
      database: myapp
      collection: cache
      port: 27017
      root:
          login: admin
          password: password

Solution

Proposed changes

I discover the issue was link to buildConnectionURI, the method return mongodb://admin:password@127.0.0.1/myapp, whereas I'm waiting this : mongodb://user:password@164.152.09.84:27017 (this second option works for me)

Types of changes

What types of changes does your code introduce to Phpfastcache?

Agreement

I have read the CONTRIBUTING and CODING GUIDELINE docs

Error log

PHP 8.1 (Ubuntu) mongo:6.0

The full log message :

 Fatal error: Uncaught MongoDB\Driver\Exception\AuthenticationException: Authentication failed. in /Users/toto/Sites/myapp/vendor/mongodb/mongodb/src/Command/ListCollections.php:112 Stack trace: #0 /Users/toto/Sites/myapp/vendor/mongodb/mongodb/src/Command/ListCollections.php(112): MongoDB\Driver\Server->executeReadCommand() #1 /Users/toto/Sites/myapp/vendor/mongodb/mongodb/src/Operation/ListCollections.php(83): MongoDB\Command\ListCollections->execute() #2 /Users/toto/Sites/myapp/vendor/mongodb/mongodb/src/Database.php(466): MongoDB\Operation\ListCollections->execute() #3 /Users/toto/Sites/myapp/vendor/phpfastcache/phpfastcache/lib/Phpfastcache/Drivers/Mongodb/Driver.php(340): MongoDB\Database->listCollections() #4 /Users/toto/Sites/myapp/vendor/phpfastcache/phpfastcache/lib/Phpfastcache/Drivers/Mongodb/Driver.php(93): Phpfastcache\Drivers\Mongodb\Driver->collectionExists() #5 /Users/toto/Sites/myapp/vendor/phpfastcache/phpfastcache/lib/Phpfastcache/Core/Pool/DriverBaseTrait.php(81): Phpfastcache\Drivers\Mongodb\Driver->driverConnect() #6 /Users/toto/Sites/myapp/vendor/phpfastcache/phpfastcache/lib/Phpfastcache/Core/Pool/CacheItemPoolTrait.php(65): Phpfastcache\Drivers\Mongodb\Driver->__driverBaseConstruct() #7 /Users/toto/Sites/myapp/vendor/phpfastcache/phpfastcache/lib/Phpfastcache/CacheManager.php(114): Phpfastcache\Drivers\Mongodb\Driver->__construct() #8 /Users/toto/Sites/CrazyPHP/src/Library/Cache/Cache.php(101): Phpfastcache\CacheManager::getInstance() #9 /Users/toto/Sites/CrazyPHP/src/Core/Router.php(80): CrazyPHP\Library\Cache\Cache->__construct() #10 /Users/toto/Sites/CrazyPHP/src/Core/Core.php(130): CrazyPHP\Core\Router->pushCollection() #11 /Users/toto/Sites/myapp/app/Core/App.php(51): CrazyPHP\Core\Core->runRoutersPreparation() #12 /Users/toto/Sites/myapp/app/Index.php(28): App\Core\App->__construct() #13 /Users/toto/Sites/myapp/public/index.php(24): require_once('...') #14 {main} Next Phpfastcache\Exceptions\PhpfastcacheDriverConnectException: (MongoDB\Driver\Exception\AuthenticationException) Mongodb failed to connect with the following error message: "Authentication failed." line 112 in /Users/toto/Sites/myapp/vendor/mongodb/mongodb/src/Command/ListCollections.php in /Users/toto/Sites/myapp/vendor/phpfastcache/phpfastcache/lib/Phpfastcache/Core/Pool/DriverBaseTrait.php:84 Stack trace: #0 /Users/toto/Sites/myapp/vendor/phpfastcache/phpfastcache/lib/Phpfastcache/Core/Pool/CacheItemPoolTrait.php(65): Phpfastcache\Drivers\Mongodb\Driver->__driverBaseConstruct() #1 /Users/toto/Sites/myapp/vendor/phpfastcache/phpfastcache/lib/Phpfastcache/CacheManager.php(114): Phpfastcache\Drivers\Mongodb\Driver->__construct() #2 /Users/toto/Sites/CrazyPHP/src/Library/Cache/Cache.php(101): Phpfastcache\CacheManager::getInstance() #3 /Users/toto/Sites/CrazyPHP/src/Core/Router.php(80): CrazyPHP\Library\Cache\Cache->__construct() #4 /Users/toto/Sites/CrazyPHP/src/Core/Core.php(130): CrazyPHP\Core\Router->pushCollection() #5 /Users/toto/Sites/myapp/app/Core/App.php(51): CrazyPHP\Core\Core->runRoutersPreparation() #6 /Users/toto/Sites/myapp/app/Index.php(28): App\Core\App->__construct() #7 /Users/toto/Sites/myapp/public/index.php(24): require_once('...') #8 {main} thrown in /Users/toto/Sites/myapp/vendor/phpfastcache/phpfastcache/lib/Phpfastcache/Core/Pool/DriverBaseTrait.php on line 84
Geolim4 commented 1 year ago

That's, strange, I'm using authentication with database path and it's working oO, I need to investigate first before merging your PR đŸ¤•

Geolim4 commented 1 year ago

I can't merge your code as it breaks the code:

"E:\Program Files (x86)\php8\8.1\php.exe" E:\wamp\www\phpfastcache\tests\Mongodb.test.php
[Begin Test: Mongodb driver]
[PHPFASTCACHE: CORE v9.1.2#c2dfc471 | API v4.2.0]
[PHP v8.1.7 with: apcu, bcmath, calendar, Core, couchbase, ctype, curl, date, dom, fileinfo, filter, gd, gettext, grpc, hash, iconv, json, libxml, mbstring, memcached, mongodb, mysqlnd, openssl, pcre, PDO, pdo_mysql, pdo_sqlite, Phar, readline, redis, Reflection, session, SimpleXML, SPL, standard, tokenizer, xml, xmlreader, xmlwriter, zip, zlib]
---
"mongodb://travis:test@127.0.0.1:27017"
[SKIP] A driver could not be initialized due to network/authentication issue: (MongoDB\Driver\Exception\AuthenticationException) Mongodb failed to connect with the following error message: "Authentication failed." line 120 in E:\wamp\www\phpfastcache\vendor\mongodb\mongodb\src\Command\ListCollections.php
Test results:  0 assertions failed, 1 assertion skipped and 0 assertions passed out of a total of 1 assertion.
Test duration: 0.159s

Process finished with exit code 2

Removing the path actually break the code. Can you provide me more information about you Phpfastcache configuration and composer.lock ?

Geolim4 commented 1 year ago

Current working code in tests/Mongodb.test.php as per official Mongodb Doc

<?php

/**
 *
 * This file is part of Phpfastcache.
 *
 * @license MIT License (MIT)
 *
 * For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
 *
 * @author Georges.L (Geolim4)  <contact@geolim4.com>
 * @author Contributors  https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
 */

use Phpfastcache\CacheManager;
use Phpfastcache\Drivers\Mongodb\Config;
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
use Phpfastcache\Tests\Helper\TestHelper;

chdir(__DIR__);
require_once __DIR__ . '/../vendor/autoload.php';
$testHelper = new TestHelper('Mongodb driver');
$config = new Config();
$config->setItemDetailedDate(true)
    ->setDatabaseName('pfc_test')
    ->setCollectionName('pfc_' . str_pad('0', 3, random_int(1, 100)))
    ->setUsername('travis')
    ->setPassword('test');

$cacheInstance = CacheManager::getInstance('Mongodb', $config);
$testHelper->runCRUDTests($cacheInstance);
$testHelper->terminateTest();
kekefreedog commented 1 year ago

Hello Georges.L,

Thank you for your qui response. I'm using Mongo into docker container, I will try to change port and host and test again.

We keep in touch !

Geolim4 commented 1 year ago

Hello @kekefreedog is your issue solved in an other way ?