laravel “用户 ''@'localhost' 对数据库 'forge' 的访问被拒绝”随机出现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35187814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:10:19  来源:igfitidea点击:

"Access denied for user ''@'localhost' to database 'forge" appears randomly

phplaravellaravel-5.2

提问by cre8

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.

在十次调用中的一次中,我收到了这条 500 错误消息,但我不知道为什么。其他调用给出了正确的结果。

.env

.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

数据库.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.

我没有修改 database.php 文件,所有其他调用都很好用。

采纳答案by Vikas Katariya

PLEASE RUN THIS COMMAND

请运行此命令

php artisan cache:clear 

THEN

然后

php artisan config:cache

回答by dExIT

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

这似乎包含大部分答案: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.

作为一种解决方法,您可以将所有权限添加到您的数据库伪造帐户。

回答by anan karki

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

尝试php artisan clear:cache清除缓存并重新配置缓存,php artisan config:cache它可能对您有用。

回答by Arsii Rasheed

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

我曾经遇到过同样的问题。我发现我的默认连接设置为 PostgreSQL 而不是 MySQL,所以我更改了 database.php 中的默认连接

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

other wise do add following to your .env

其他明智的做法是将以下内容添加到您的 .env

DB_CONNECTION=mysql

回答by rohit prasain

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

尝试在 .env 文件中添加剩余的方法