php - SQLSTATE[HY000] [1045] 用户 'landon_app' @'localhost' 的访问被拒绝

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

    ],

];

最佳答案

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>

关于php - SQLSTATE[HY000] [1045] 用户 'landon_app' @'localhost' 的访问被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50916207/

相关文章:

php - 如何使用 Passport 刷新 API 调用的 laravel_token

php - 如何从 PHP 中存储在我的数据库中的文本中获取前 n 个单词?

php - ORDER BY rand() 并使用 group_id 进行分组

php - 仅将 ssl 应用于站点的管理路由

带有 Laravel Blade 模板循环和总和的 JavaScript

php - 除非浏览器完全关闭并重新打开,否则数据库条目不会显示在 IE 中

javascript - jQuery 全选和取消全选不起作用

php - CodeIgniter Active Record 多个

mysql - Web 应用程序数据库设计的替代方案

javascript - Laravel Blade @foreach 中的 Vue.js v-model 多文本输入问题