MySQL Empty User Won't Connect to DB

Hello!

I am new to Laravel and I spent the last 6 hours trying to get my application to connect to MySQL. Everything was running smoothly on localhost and upon a refresh, MySQL wouldn't connect anymore. The error message I'm getting is:

PDOException in Connector.php line 55: SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'mmfa'

I traced this "empty username" back all the way to DatabaseManager.php and the function getConfig(). When I print out the $connections variable, I notice the following output under the mysql key:

Array
(
    [mysql] => Array
        (
            [driver] => mysql
            [host] => 127.0.0.1
            [port] => 3306
            [database] => mmfa
            [0] => root
            [charset] => utf8
            [collation] => utf8_unicode_ci
            [prefix] => 
            [strict] => 
            [engine] => 
        )
  )

Under MySQL, the username field is [0] (not [username]) and [password] doesn't exist.

For another test, I hardcoded the username/password in Connector.php on line 55: new PDO($dsn, 'root', 'root', $options); and that allowed the app to run (thus confirming MySQL/PHP are running correctly).

I have read through this thread (https://laravel.io/forum/07-02-2014-sqlstatehy000-1044-access-denied-for-user-localhost-to-database-forge?page=1) and have tried everything they have to offer:

  • php artisan cache:clear
  • php artisan config:clear
  • creating a new DB
  • creating a new DB user
  • deleting current DB and running migrate (even migrate throws the same "empty username" error)
  • php artisan tinker --> App::environment(); --> "local"

Any help would be greatly appreciated!

Current .env file

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:1l5O1zFGX5v6EsuudFJobSdLz0cbhZQ5X1G2hhs88kU=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost:8000

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mmfa
DB_USERNAME=root
DB_PASSWORD=root

BROADCAST_DRIVER=log
CACHE_DRIVER=array
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=base64:1l5O1zFGX5v6EsuudFJobSdLz0cbhZQ5X1G2hhs88kU=
PUSHER_APP_SECRET=

Current database.php

<?php

return [


    'fetch' => PDO::FETCH_CLASS,

    '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', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'mmfa'),
-           'username' => env('DB_USERNAME', 'root'),
-           'password' => env('DB_PASSWORD', 'root'),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            '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',
        ],

    ],



    'migrations' => 'migrations',



    'redis' => [

        'cluster' => false,

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

    ],

];

chrislafay
chrislafay
  • 2 weeks ago
  1. What does your config/database.php file look like?
  2. What Laravel Version?
  3. What PHP Version?

You've got minuses before 'username' and 'password' delete them. That's causing the issue.

thecrypticace said:

You've got minuses before 'username' and 'password' delete them. That's causing the issue.

As soon as I pasted it in, I saw it. I never noticed it in PHPStorm... Sometimes, you can't even notice your own stupidity when it's staring at you in the face. For 6 hours. Thanks for the quick responses folks!

No problem. Happy to help!