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

问题描述 投票:1回答:7

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

    ],

];

我该如何解决?

laravel laravel-5 database-migration laravel-migrations
7个回答
4
投票

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

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


1
投票

将来的某个时候。如果使用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

如果你运行命令它应该顺利工作。


1
投票
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=YOUR_DATABASE_NAME
DB_USERNAME=root
DB_PASSWORD=

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


0
投票

您可以使用以下命令检查当前环境:php artisan env

下一个

  • 在config / development下创建|登台(文件夹)
  • database.php保存文件development | staging access
  • 编辑.env文件APP_ENV=development | staging | production

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

    ],

];

0
投票

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

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

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

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


0
投票

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

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

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

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


-2
投票

如果您在访问数据库时不需要登录,请尝试将数据库用户名设置为“root”,密码字段留空,这样可以解决我的问题

© www.soinside.com 2019 - 2024. All rights reserved.