8

我正在使用 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,
    ],

    ],

];

我该如何解决?

4

11 回答 11

7

.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()中,函数正在从此处查找此文件和变量。

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

于 2015-05-01T14:06:15.357 回答
2
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

于 2018-10-25T12:04:43.730 回答
2

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

SQLSTATE[HY000] [1044] 拒绝用户“@”localhost”访问数据库“forge”

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

DB_USERNAME=         //Your Database Username           

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

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


注意:如果仍然有错误

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

或者

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

于 2019-07-19T05:43:29.287 回答
1

在未来的某个时候。如果使用 MySQL 而不是 Homestead,这将是一个基本设置。配置->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

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

于 2017-02-20T05:33:47.983 回答
1

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

(这对我有用)

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

于 2021-08-10T19:21:26.727 回答
0

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

下一个

  • 在配置/开发下创建 | 暂存(文件夹)
  • 保存一个database.php 文件development | staging access
  • 编辑.env 文件 APP_ENV=development | staging | production
于 2015-10-30T22:49:01.203 回答
0

尝试去 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,
        ],

    ],

];
于 2016-11-18T05:47:36.053 回答
0

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

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

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

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

于 2019-05-02T03:58:37.090 回答
0

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

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

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

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

于 2019-05-26T05:43:17.367 回答
0

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

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

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

解决方案:

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

于 2021-03-20T10:29:09.867 回答
-1

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

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

这解决了我的问题

于 2017-03-18T22:30:59.650 回答