SQLSTATE [HY000] [1045]拒绝访问用户'landon_app'@'localhost'

时间:2018-06-18 19:22:57

标签: php mysql laravel eloquent

我目前正在关注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,
        ],

    ],

];

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

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

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

(后跟:FLUSH PRIVILEGES;申请更改。)