15

I have a search function for my database but sometimes I get this message:

[2016-02-04 07:03:18] local.ERROR: PDOException: SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'forge' in C:\xampp\htdocs\reko\api\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:55
Stack trace:
#0 C:\xampp\htdocs\reko\api\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php(55): PDO->__construct('mysql:host=loca...', 'forge', '', Array)
...

In one of ten calls I get this 500 error message, but I don't know why. The other calls give the right result.

.env

APP_ENV=local
APP_DEBUG=true
APP_KEY=bJM6O0MnrIPaTNwKKOqNJkGinRDv1fnc

DB_HOST=localhost
DB_DATABASE=reko
DB_USERNAME=root
DB_PASSWORD=

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

Search function:

public function search(Modul $modul, Request $request)
{
    $question = Question::whereModulId($modul->id)
        ->where('value', 'LIKE', '%' . $request->get('keywords') . '%')
        ->with('tags')
        ->whereHas('exams', function ($query) use ($request) {
            $query->where('date', '>=', $request->get('year').'-01-01');
        });
    if (!$request->get('parent'))
        $question->where('type', '<>', 'parent');
    if (!$request->get('own'))
        $question->where('type', '<>', 'own');
    if (!$request->get('normal'))
        $question->where('type', '<>', 'normal');
    if ($request->get('answered'))
        $question->has('answers');
    return $question->paginate(10);
}

database.php

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

I haven't modified the database.php file and all other calls work great.

cre8
  • 13,012
  • 8
  • 37
  • 61
  • I have never used it, the user doesn't even exists. There is nothing written in my code – cre8 Feb 03 '16 at 21:18
  • Check your database's logs for errors. The fact that its inconsistently failing means it's probably the database's fault, not PHP's. – Mr. Llama Feb 03 '16 at 21:20
  • I think I found the error. The `forge` user is the default value of laravel in the `database.php`. So it can happen that laravel forgets to load the `.env` file and uses this wrong user. I updated the file and now it seems to work. – cre8 Feb 03 '16 at 21:22
  • You will need to provide some more information to trace the issue. You might want to check whether correct environment is being loaded when you're trying search in your browser. There could be a race condition or perhaps another virtual host is being called (which likely means a different ENV altogether). Intercept the query and check that environment is correct. – Tim Hysniu Feb 03 '16 at 21:23
  • 2
    I believe this is a common problem. You may want to cache your config files by running `php artisan config:cache`, but it is recommended you do this in production mode only since you're likely to change your environment variables often during development. – Thomas Kim Feb 03 '16 at 21:30
  • Caching seems to be the best solution. – cre8 Feb 03 '16 at 21:34
  • You may be temporarily running out of max available client connections - something I experienced with Postgres, although the error message is explicit enough there about such problem. I'd turn on more verbose logging in mysql server first and then look for error messages in the logs. – LetMeSOThat4U Feb 03 '16 at 22:31
  • 1
    It's funny cuz this happens once every so often for me... I'm only developing in local windows environment with .env file. But why does laravel not load the env file sometimes? – Thomas Cheng Sep 28 '16 at 05:52
  • Do you have any other database.php? – Haseena P A Mar 10 '17 at 05:44
  • Did you check the connection limit of your mysql server? – xatzistnr Mar 11 '17 at 08:48

6 Answers6

4

PLEASE RUN THIS COMMAND

php artisan cache:clear 

THEN

php artisan config:cache
thisiskelvin
  • 4,136
  • 1
  • 10
  • 17
VIKAS KATARIYA
  • 5,867
  • 3
  • 17
  • 34
2

This seems to hold most of the answers : Laravel 5.2 not reading env file

Remember to not to edit your core files as one of the users said. Just do the safe checks, clear cache, should work.

Hope it helps.

As a workaround you can add to your DB forge account with all privileges.

Community
  • 1
  • 1
dExIT
  • 222
  • 2
  • 12
1

Try php artisan clear:cache to clear your cache and re-configure your cache php artisan config:cache it might works for you .

anan karki
  • 19
  • 4
0

I had the same issue once. what I discovered was that my default connection was set to PostgreSQL instead of MySQL so I changed my default connection in database.php

'default' => env('DB_CONNECTION', 'mysql'),

other wise do add following to your .env

DB_CONNECTION=mysql
Arsii Rasheed
  • 324
  • 1
  • 5
  • 18
0
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=root
DB_PASSWORD=

try adding remaining methods in your .env file

Clint
  • 2,696
  • 23
  • 42
0

Too often we have to face this error when hosting.

You should see that you have given Privileged Users permission to the database in the hosting.

First

First I need to see if the database name is correct.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=zhara_demo
DB_USERNAME=zhara_admin
DB_PASSWORD=zhara@2021 

or

  • Try php artisan clear:cache to clear your cache and re-configure
  • your cache php artisan config:cache it might works for you .