SQLSTATE[HY000] [1045] Access denied for user ‘databasename’@’localhost’ (using password: YES)

Welcome to Programming Tutorial official website. Today - we are going to cover how to solve / find the solution of this error SQLSTATE[HY000] [1045] Access denied for user ‘databasename’@’localhost’ (using password: YES) on this date .

In connection.php Shows error undefined method getDoctrineDriver().

    public function getDoctrineSchemaManager() {
        return $this->getDoctrineDriver()->getSchemaManager($this->getDoctrineConnection());
    }

    /**
     * Get the Doctrine DBAL database connection instance.
     *
     * @return DoctrineDBALConnection
     */
    public function getDoctrineConnection() {
        if (is_null($this->doctrineConnection)) {
            $data = ['pdo' => $this->getPdo(), 'dbname' => $this->getConfig('database')];

            $this->doctrineConnection = new DoctrineConnection(
                $data, $this->getDoctrineDriver()
            );
        }
        return $this->doctrineConnection;
    }

config/database.php

    <?php
    return [
        'default' => env('DB_CONNECTION', 'mysql'),
        'connections' => [
            /*'sqlite' => [
            'driver' => 'sqlite',
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
        ],*/

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'mydatabasename'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

        'pgsql' => [
            'driver' => 'pgsql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'schema' => 'public',
            'sslmode' => 'prefer',
        ],

    ],

    'redis' => [
        'cluster' => false,

        'default' => [
            'host' => env('REDIS_HOST', 'localhost'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
        ],

    ],

];

.env

    DB_CONNECTION=mysql
    DB_PORT=3306
    DB_DATABASE=mydatabasename
    DB_USERNAME=root
    DB_PASSWORD=

getclass() is deprecated in PHP 8 so i managed to use gettype() instead of getclass() in container.php next run my server its shows a login page and enters the username and password after shows this error I don’t know how to fix it!

Answer

This error occurs when changes applied to .envfile, first, make sure your DB credential and DB Host is set correctly:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE="DB NAME"
DB_USERNAME="root"
DB_PASSWORD=""

And if your problem is not solved, try to clear your configuration cache:

php artisan config:clear

If not solve, restart your server.