【问题标题】:SQLSTATE[HY000] [1045] Access denied for user 'landon_app'@'localhost'SQLSTATE[HY000] [1045] 用户'landon_app'@'localhost'的访问被拒绝
【发布时间】:2018-06-19 11:37:14
【问题描述】:

我目前正在学习 lynda 上的 laravel/mysql 教程,并按照以下步骤创建数据库并将其连接到我的 laravel 框架。

以下步骤帮助命名我的数据库,更改我的用户名和密码(其中不再是用户:root,密码:root):

1. CREATE DATABASE landon_app;
2. CREATE USER 'landon_app'@'localhost' IDENTIFIED BY 'landon_app';
3. GRANT ALL ON landon_app.* TO 'landon_app'@'localhost';

这样做之后,我可以在命令行上运行它:mysql -u -landon_app -plandon_app

mysql 已启动并运行。

但问题是,在我创建迁移并设置表架构和所有内容后,我收到一条错误消息

     SQLSTATE[HY000] [1045] Access denied for user 'landon_app'@'localhost' (using password: YES) (SQL: select * from information_schema.tables where table_sc  
  hema = landon_app and table_name = migrations)

  SQLSTATE[HY000] [1045] Access denied for user 'landon_app'@'localhost' (using password: YES)  

我已经更新了我的 .env 文件:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=landon_app
DB_USERNAME=landon_app
DB_PASSWORD=landon_app

为什么会这样,还有其他人遇到过类似的事情吗?我想既然命令行mysql -u -landon_app -plandon_app可以工作,php artisan migrate应该没有问题

任何事情都会有帮助!

谢谢!

编辑:config/database.php

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | 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', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

        'pgsql' => [
            'driver' => 'pgsql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'schema' => 'public',
            'sslmode' => 'prefer',
        ],

        'sqlsrv' => [
            'driver' => 'sqlsrv',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '1433'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            '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' => [

        'client' => 'predis',

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

    ],

];

【问题讨论】:

  • 如果该命令有效,那么用户没有设置密码,所以您应该将 DB_PASSWORD 设置为空白
  • 那么,您可以从 CLI 运行查询吗?
  • @Derek 我只是将 DB_PASSWORD 设置为空白,但仍然收到相同的错误:(
  • @Kevin_Kinsey: mysql -u -landon_app -plandon_app 有效,php artisan migrate 无效
  • 是的,你说“有效”,但在 CLI 中,这是特定的行:“select * from information_schema.tables where table_schema = landon_app and table_name = migrations”?你在 CLI 中得到结果?

标签: php mysql laravel eloquent


【解决方案1】:

可能 PHP 使用的是 TCP 连接而不是套接字连接。在这种情况下,您需要127.0.0.1 的权限。

1. CREATE USER 'landon_app'@'127.0.0.1' IDENTIFIED BY 'landon_app';
2. GRANT ALL ON landon_app.* TO 'landon_app'@'127.0.0.1';

或者您需要指定DB_SOCKET 来定义您的套接字。您可以使用以下命令获取此信息:

mysql > show variables like '%socket%';
+-----------------------------------------+------------------------------+
| Variable_name                           | Value                        |
+-----------------------------------------+------------------------------+
| performance_schema_max_socket_classes   | 10                           |
| performance_schema_max_socket_instances | -1                           |
| socket                                  | **/tmp/mysql_sandbox45007.sock** |
+-----------------------------------------+------------------------------+
3 rows in set (0.01 sec)

这是一个例子:

 mysql -uroot -pmsandbox -S /tmp/mysql_sandbox45007.sock

还有你的.env

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=landon_app
DB_USERNAME=landon_app
DB_PASSWORD=landon_app
DB_SOCKET=<socket>

【讨论】:

    【解决方案2】:

    您还需要为information_schema 数据库添加权限:

    GRANT ALL ON `information_schema`.* TO 'landon_app'@'localhost';
    

    (后跟:FLUSH PRIVILEGES; 以应用更改。)

    【讨论】:

    • 我返回并为信息模式添加了权限,然后是 FLUSH PRIVILEGES,我收到以下错误:FLUSH PRIVILEGES;错误 1227 (42000):访问被拒绝;您需要(至少一个)执行此操作的 RELOAD 权限
    猜你喜欢
    • 2018-07-22
    • 2019-12-01
    • 1970-01-01
    • 2017-08-27
    • 2015-06-15
    • 2014-09-22
    • 2021-02-27
    • 2015-09-18
    相关资源
    最近更新 更多