Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rochapabloricardo's avatar

Access denied for user ''@'localhost'

First let me show how I'm working;

I have an Angular JS (front-end) and Laravel (back-end). The front-end always request the back-end getting all kind of data.

Now, my issue is that SOMETIMES, SOME of the requests just fail; Where at the front-end I got:

GET http://dev.mywebsite.com/preview/nZR?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1N 500 (Internal Server Error)

And the log from back-end says:

[2016-05-13 19:47:16] production.ERROR: exception 'PDOException' with message 'SQLSTATE[HY000] [1045] Access denied for user ''@'localhost' (using password: NO)' in mywebsite\admin\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:55
Stack trace:
#0 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php(55): PDO->__construct('mysql:host=;dbn...', '', '', Array)
#1 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Database\Connectors\MySqlConnector.php(22): Illuminate\Database\Connectors\Connector->createConnection('mysql:host=;dbn...', Array, Array)
#2 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Database\Connectors\ConnectionFactory.php(60): Illuminate\Database\Connectors\MySqlConnector->connect(Array)
#3 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Database\Connectors\ConnectionFactory.php(49): Illuminate\Database\Connectors\ConnectionFactory->createSingleConnection(Array)
#4 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Database\DatabaseManager.php(175): Illuminate\Database\Connectors\ConnectionFactory->make(Array, 'mysql')
#5 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Database\DatabaseManager.php(67): Illuminate\Database\DatabaseManager->makeConnection('mysql')
#6 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(3224): Illuminate\Database\DatabaseManager->connection(NULL)
#7 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(3190): Illuminate\Database\Eloquent\Model::resolveConnection(NULL)
#8 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1870): Illuminate\Database\Eloquent\Model->getConnection()
#9 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1813): Illuminate\Database\Eloquent\Model->newBaseQueryBuilder()
#10 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1787): Illuminate\Database\Eloquent\Model->newQueryWithoutScopes()
#11 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Auth\EloquentUserProvider.php(47): Illuminate\Database\Eloquent\Model->newQuery()
#12 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Auth\Guard.php(502): Illuminate\Auth\EloquentUserProvider->retrieveById(15)
#13 [internal function]: Illuminate\Auth\Guard->onceUsingId(15)
#14 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Support\Manager.php(137): call_user_func_array(Array, Array)
#15 mywebsite\admin\vendor\tymon\jwt-auth\src\Providers\Auth\IlluminateAuthAdapter.php(50): Illuminate\Support\Manager->__call('onceUsingId', Array)
#16 mywebsite\admin\vendor\tymon\jwt-auth\src\Providers\Auth\IlluminateAuthAdapter.php(50): Illuminate\Auth\AuthManager->onceUsingId(15)
#17 mywebsite\admin\vendor\tymon\jwt-auth\src\JWTAuth.php(126): Tymon\JWTAuth\Providers\Auth\IlluminateAuthAdapter->byId(15)
#18 mywebsite\admin\app\Http\Middleware\JwtAuthenticateMedia.php(19): Tymon\JWTAuth\JWTAuth->authenticate()
#19 [internal function]: App\Http\Middleware\JwtAuthenticateMedia->handle(Object(Illuminate\Http\Request), Object(Closure))
#20 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(124): call_user_func_array(Array, Array)
#21 mywebsite\admin\vendor\barryvdh\laravel-cors\src\HandleCors.php(34): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#22 [internal function]: Barryvdh\Cors\HandleCors->handle(Object(Illuminate\Http\Request), Object(Closure))
#23 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(124): call_user_func_array(Array, Array)
#24 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#25 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#26 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(710): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#27 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(675): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#28 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(635): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#29 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(236): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#30 [internal function]: Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#31 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(139): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#32 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php(50): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#33 [internal function]: Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#34 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(124): call_user_func_array(Array, Array)
#35 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\EncryptCookies.php(59): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#36 [internal function]: Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#37 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(124): call_user_func_array(Array, Array)
#38 mywebsite\admin\vendor\laravel\framework\src\Illuminate\View\Middleware\ShareErrorsFromSession.php(49): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#39 [internal function]: Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#40 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(124): call_user_func_array(Array, Array)
#41 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php(62): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#42 [internal function]: Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#43 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(124): call_user_func_array(Array, Array)
#44 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse.php(37): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#45 [internal function]: Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#46 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(124): call_user_func_array(Array, Array)
#47 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php(44): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#48 [internal function]: Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#49 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(124): call_user_func_array(Array, Array)
#50 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#51 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#52 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(122): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#53 mywebsite\admin\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(87): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#54 mywebsite\admin\public\index.php(59): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#55 {main}  

But if I access the same URL using postman, IT JUST WORK.

So, why this happen only SOMETIMES and only through front-end?

  • Could that be my .env?
  • Is it the back-end not ready to receive many requests?
  • Why am I on development env but the log is "production.ERROR"

I'm really lost here.

0 likes
6 replies
nickywest's avatar

This could be a connection limit, but I think your error message would have reflected that. I'm pretty sure MySQL will give a too many connections error in that case.

The error message you do have says:

Access denied for user ''@'localhost' (using password: NO)

Your stack trace and the SQL error makes it look like it's not getting your DB connection config (no password or username). Check the permissions on your .env file, make sure the apache (or whatever user is resolving your front-end requests) user has access to read it. It definitely looks like a DB connection config isn't being loaded properly.

rochapabloricardo's avatar

This is my database.php

'mysql' => [
    'driver'    => 'mysql',
    'host'      => env('DB_HOST', ''),
    'database'  => env('DB_DATABASE', ''),
    'username'  => env('DB_USERNAME', ''),
    'password'  => env('DB_PASSWORD', ''),
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
]

I've set empty because I want the system always using my env files.

I'll check the DB.

Thanks for now.

nickywest's avatar

It's very much correct to keep your connection info (username, password, etc) out of the config and in your .env file. Because the error you're getting is saying the username was blank and there was no password, it's definitely that your .env isn't loading. This could be due to permissions, or it could be a path difference if your postman request was a different domain (ip vs name?).

Could be lots of things, but the .env isn't loading so that's where to dig for sure.

nickywest's avatar

@rochapabloricardo I was just putzing around the forums and stumbled on this post.

If you're running multiple environments on the same server it's possible that values in your .env files are being used by the wrong environment. It sounds like dotenv isn't thread safe resulting in values ending up in the wrong threads when multiple environments are hosted from the same server. The fix in that post is to generate a config cache

artisan config:cache
rochapabloricardo's avatar

@nickwest, I had already tried this and I always get

Fatal error: Call to undefined method Closure::__set_state() in bootstrap\cache\config.php on line 409

... and I didn't found an smart way to fix that, besides delete the bootstrap\cache\config.php. So I'm hoping for that is not been part of my issue.

It's sounds more reasonable what you said about the env file not been read (for some reason) or the database not been ready to received too many requests. I'll keep on those thoughts.

Thank you for now.

PS. Oh, about the postman I access the same url that the front-end API. Once I've heard something about that Laravel validate where the requests come from, so if It was an external access it would block. I'm not sure if this make any sense.

rochapabloricardo's avatar
Level 1

Well problem "quite" fixed.

After lot of attempts, I started all over again with a new empty project, using the same configuration (https://github.com/rochapablo/backfront-skeleton-laravel). And every attempt trying understand why sometimes the Laravel lose the env file configuration was in vain, having the same logs;

  • Access denied for user
  • No supported encrypter found. The cipher and / or key length are invalid.

Going further I read someone saying that read the env file couldn't be a good thing. "I don't know about that", but anyway I've tried to set the configuration in a different way, so every time the Laravel request any KEY env I would "reload" the value.

class Adeda
{
  public function __construct()
  {
    $host = (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '');
    
    switch($host)
    {
      case 'mywebsite.com':
      $this->tmp = [
        'APP_ENV' => 'production',
      ];
      break;
      
      default:
      $this->tmp = [
        'APP_ENV' => 'local',
      ];
      break;
    }
    
    foreach($this->tmp as $key => $val)
    {
      putenv(sprintf('%s=%s', $key, $val));
    }
  }
  
  public function get($key)
  {
    $value = getenv($key);
    if(empty($value)) {
      return $this->tmp[$key];
    }
    return $value;
  }
}
'mysql' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', Adeda::getInstance()->get('DB_HOST')),
],

And with this horrible (or not) idea, everything works.

I don't know if this is a good practice, I just would like to deploy the code in any server without be worry if I had created the f* .env file.

The only thing that I'm sure is that the problem wasn't the database.

Please or to participate in this conversation.