PDOException (1044) SQLSTATE[HY000] [1044] 用户''@'localhost'拒绝访问数据库'forge'

Posted

技术标签:

【中文标题】PDOException (1044) SQLSTATE[HY000] [1044] 用户\'\'@\'localhost\'拒绝访问数据库\'forge\'【英文标题】:PDOException (1044) SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'forge'PDOException (1044) SQLSTATE[HY000] [1044] 用户''@'localhost'拒绝访问数据库'forge' 【发布时间】:2015-07-11 06:30:00 【问题描述】:

我正在使用 Laravel 5 并遇到以下异常:

PDOException (1044) SQLSTATE[HY000] [1044] 拒绝用户访问 ''@'localhost' 到数据库'forge'

我的数据库配置文件是:

<?php

return [
/*
|--------------------------------------------------------------------------
| PDO Fetch Style
|--------------------------------------------------------------------------
|
| By default, database results will be returned as instances of the PHP
| stdClass object; however, you may desire to retrieve records in an
| array format for simplicity. Here you can tweak the fetch style.
|
*/

'fetch' => PDO::FETCH_CLASS,

/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/

'default' => 'mysql',

/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/

'connections' => [

    'sqlite' => [
        'driver'   => 'sqlite',
        'database' => storage_path().'/database.sqlite',
        'prefix'   => '',
    ],

    '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,
    ],

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

    'sqlsrv' => [
        'driver'   => 'sqlsrv',
        'host'     => env('DB_HOST', 'localhost'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'prefix'   => '',
    ],

],

/*
|--------------------------------------------------------------------------
| Migration Repository Table
|--------------------------------------------------------------------------
|
| This table keeps track of all the migrations that have already run for
| your application. Using this information, we can determine which of
| the migrations on disk haven't actually been run in the database.
|
*/

'migrations' => 'migrations',

/*
|--------------------------------------------------------------------------
| Redis Databases
|--------------------------------------------------------------------------
|
| Redis is an open source, fast, and advanced key-value store that also
| provides a richer set of commands than a typical key-value systems
| such as APC or Memcached. Laravel makes it easy to dig right in.
|
*/

'redis' => [

    'cluster' => false,

    'default' => [
        'host'     => '127.0.0.1',
        'port'     => 6379,
        'database' => 0,
    ],

    ],

];

我该如何解决?

【问题讨论】:

你是用宅基地做开发环境吗?您还使用.env 文件作为环境吗?如果您可以共享您的数据库配置文件,那就更好了。 我添加了配置文件。当前应用环境:生产 【参考方案1】:

.env 文件放在您的根目录中并将此代码粘贴到那里。

APP_ENV=local
APP_DEBUG=true
APP_KEY=1CaND3OKKvOGSBAlCg6IyrRmTQWwZjOO

DB_HOST = localhost
DB_DATABASE = YOUR_DATABASE_NAME
DB_USERNAME = USER_NAME
DB_PASSWORD = PASSWORD

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

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

在此处更新您的数据库、用户名和密码字段,它应该可以解决您的问题。在您的配置文件中env() 函数正在从此处查找此文件和变量。

更新:您必须在运行应用程序或迁移之前创建一个空白数据库。

【讨论】:

这个 .env 文件不在我的根目录中.. 但创建后问题仍然相同.. 我解决了。我的数据库和用户名都是“伪造的”。我更改了我的数据库名称和用户名并解决了它......谢谢@Ariful Haque【参考方案2】:

我们忘记在 .env 文件中设置DB_USERNAME=,所以我们得到了这个错误:

SQLSTATE[HY000] [1044] 拒绝用户 ''@'localhost' 访问 数据库'伪造'

打开 .env 文件并进行编辑。只需设置正确的数据库凭据:

DB_USERNAME=         //Your Database Username           

如果您在安装时没有默认用户名,则应将DB_USERNAME 设置为root

.env 修改后在终端输入此命令清除缓存:php artisan config:cache


注意:如果仍有错误

如果您使用的是 PHP 的默认 Web 服务器(例如 php artisan serve),您需要重新启动您的服务器

如果您使用过 XAMPP,请重新启动您的 Apache 服务器

【讨论】:

感谢php artisan config:cache --> +1【参考方案3】:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=YOUR_DATABASE_NAME
DB_USERNAME=root
DB_PASSWORD=

如果您在 XAMPP 中安装 MySQL 时没有默认用户名,则应将 DB_USERNAME= 设置为 root

【讨论】:

【参考方案4】:

未来的某个时候。如果使用 MySQL 而不是 Homestead,这将是一个基本设置。 Config->database.php 文件

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

在你的 .env 文件中

APP_ENV=local
APP_DEBUG=true
APP_KEY=ruA9CAKRJCFgLOD1nc5o1BmvaTGokasi

DB_HOST=localhost
DB_DATABASE=yourDatabaseName


CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

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

如果您运行该命令,它应该可以顺利运行。

【讨论】:

不错的答案@Krishneil。【参考方案5】:

更改帐户所有权后遇到同样的问题,所以我最终做了以下操作:

(这对我有用)

记下您的实际用户名和密码 转到您的 cPanel(如果您使用的是 cPanel) 从数据库选项卡中,选择 MySQL 数据库。 导航到当前用户 删除与您有冲突的数据库对应的用户。 删除后,进入 Add User to Database 选项卡并重新输入用户名和密码,这将重新生成具有当前所有者权限的连接。

【讨论】:

【参考方案6】:

您可以通过以下方式检查当前环境:php artisan env

下一个

在配置/开发下创建 |暂存(文件夹) 用development | staging access保存文件database.php 编辑.env文件APP_ENV=development | staging | production

【讨论】:

【参考方案7】:

尝试去database.php文件更正你的数据库名称、用户名和密码

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | PDO Fetch Style
    |--------------------------------------------------------------------------
    |
    | By default, database results will be returned as instances of the PHP
    | stdClass object; however, you may desire to retrieve records in an
    | array format for simplicity. Here you can tweak the fetch style.
    |
    */

    'fetch' => PDO::FETCH_OBJ,

    /*
    |--------------------------------------------------------------------------
    | Default Database Connection Name
    |--------------------------------------------------------------------------
    |
    | Here you may specify which of the database connections below you wish
    | to use as your default connection for all database work. Of course
    | you may use many connections at once using the Database library.
    |
    */

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

    /*
    |--------------------------------------------------------------------------
    | Database Connections
    |--------------------------------------------------------------------------
    |
    | Here are each of the database connections setup for your application.
    | Of course, examples of configuring each database platform that is
    | supported by Laravel is shown below to make development simple.
    |
    |
    | All database work in Laravel is done through the PHP PDO facilities
    | so make sure you have the driver for your particular database of
    | choice installed on your machine before you begin development.
    |
    */

    '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', 'msm'),----database name
            'username' => env('DB_USERNAME', 'root'),--localhost username
            'password' => env('DB_PASSWORD', ''),--localhost 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', 'msm'),----database name
            'username' => env('DB_USERNAME', 'root'),--localhost username
            'password' => env('DB_PASSWORD', ''),--localhost password
            'charset' => 'utf8',
            'prefix' => '',
            'schema' => 'public',
            'sslmode' => 'prefer',
        ],

    ],

    /*
    |--------------------------------------------------------------------------
    | Migration Repository Table
    |--------------------------------------------------------------------------
    |
    | This table keeps track of all the migrations that have already run for
    | your application. Using this information, we can determine which of
    | the migrations on disk haven't actually been run in the database.
    |
    */

    'migrations' => 'migrations',

    /*
    |--------------------------------------------------------------------------
    | Redis Databases
    |--------------------------------------------------------------------------
    |
    | Redis is an open source, fast, and advanced key-value store that also
    | provides a richer set of commands than a typical key-value systems
    | such as APC or Memcached. Laravel makes it easy to dig right in.
    |
    */

    'redis' => [

        'cluster' => false,

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

    ],

];

【讨论】:

【参考方案8】:

此问题可能是由于文件没有预期的“.env”扩展名造成的。

如果您在 Windows 中隐藏了文件扩展名(默认情况下启用),那么它很容易看起来就像扩展名是“.env”,而实际上它类似于“.env”。 env.txt”。

这意味着程序将无法找到您的“.env”文件,因为它没有正确的扩展名。

使用 Sublime Text 之类的编辑器打开文件,您会看到 actual 扩展名是什么。如果它具有“.txt”扩展名,请删除该部分并重新保存。

【讨论】:

【参考方案9】:

我收到了相同的消息,但用户名为空。用户名始终为空。

问题出在 MySQL 上。 SQLSTATE 错误消息直接从 MySQL 传递。

然后我删除了数据库和用户。在 PhpMyAdmin 的 Databases 选项卡中重新创建了数据库。重要的是分别添加了用户和权限。这解决了这个问题。

我可以从 MySQL 客户端连接并运行迁移。

【讨论】:

【参考方案10】:

当您在同一个项目/查询中使用多个数据库时也会出现此问题

我通过对所有数据库使用相同的 DB_USERNAME 解决了同样的问题, 因为我在同一个项目中使用了多个数据库,并且每个数据库 DB_USERNAME 都不同,所以当我从多个数据库中运行查询选择、更新、删除等时,就会出现这个错误。

因为如果您在查询中使用多个表并且数据库 DB_USERNAME 不同,您将收到此错误。

解决方案:

如果您使用/连接多个数据库,那么您应该为所有数据库使用相同的 DB_USERNAME

【讨论】:

【参考方案11】:

如果您在访问数据库时不需要登录

尝试将数据库用户名设为root密码字段留空

这解决了我的问题

【讨论】:

以上是关于PDOException (1044) SQLSTATE[HY000] [1044] 用户''@'localhost'拒绝访问数据库'forge'的主要内容,如果未能解决你的问题,请参考以下文章

docker-compose 中的“无法连接到本地 MySQL 服务器”

sql语句报错,DB2 SQL Error: SQLCODE=-203, SQLST

带有消息“找不到驱动程序”的 PostgreSql 'PDOException'

Drupal 安装 PDOException

PDOException: SQLSTATE[08004] [1040] 连接太多

php中的PDOException“找不到驱动程序”