SQLSTATE [HY000] [1045]访问被拒绝用户’landon_app’@’localhost’?

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

登录mysql时出现:

ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: YES)

工具/原料

  • Ubuntu18
  • MySQL 8.0.14

方法/步骤

  1. 1命令行终端登录mysql时界面出现:ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: YES)
  2. 2需要跳过密码验证进入mysql进行修改密码命令行终端输入:sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf在文本最后添加(如果不知道怎么添加,可以留言给我)skip-grant-tables保存退出
  3. 3重启服务service mysql stopservice mysql start
  4. 4再次登录mysql密码填自己安装时设置的密码,或者直接回车进入
  5. 5输入修改密码的命令发现出现错误提示:ERROR 1290 (HY000): The MySQL server is running with the –skip-grant-tables option so it cannot execute this statement
  6. 6命令行终端输入flush privileges;然后再次输入修改密码命令ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘root’;修改成功
  7. 7退出mysql重新进入mysql配置文件sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf将添加的skip-grant-tables删除然后保存退出
  8. 8重启服务service mysql restart登录mysql成功

以下步骤帮助命名我的数据库,更改我的用户名和密码(不再是用户:root,password: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,
        ],

    ],

];

作者: 执着小钟

执着小钟

发表评论