Laravel数据库访问被拒绝的根用户

时间:2020-08-31 04:27:06

标签: php mysql linux laravel

我正在尝试用我的数据库mysql首次连接我的laravel项目,但变得不可能了。我有ubuntu20。每当我尝试迁移(php artisan迁移)时,我都会不断收到此消息:

Illuminate\Database\QueryException 

  SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO) (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations and table_type = 'BASE TABLE')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
    667|         // If an exception occurs when attempting to run a query, we'll format the error
    668|         // message to include the bindings with SQL, which will make this exception a
    669|         // lot more helpful to the developer instead of just the database's errors.
    670|         catch (Exception $e) {
  > 671|             throw new QueryException(
    672|                 $query, $this->prepareBindings($bindings), $e
    673|             );
    674|         }
    675| 

      +34 vendor frames 
  35  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

我尝试了所有...我认为是正确的.ENV文件:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laravel1
DB_USERNAME=root
DB_PASSWORD=1234

我有一个用户“ root”并传递“ 1234”。在控制台中它可以正常工作-> sudo mysql -u root -p(然后是1234)... 我还在phpmyadmin中创建了数据库laravel1,所以没关系。 帮助

1 个答案:

答案 0 :(得分:0)

sudo mysql -u root -p这是问题,它应该像mysql -u root -p(没有sudo)一样工作

如果没有root用户访问权限就无法访问数据库

  • 第1步sudo mysql -u root -p ----以此登录,然后运行cmd

  • 步骤2> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '1234'; ----在这里,您可以根据需要更改密码

  • 步骤3> FLUSH PRIVILEGES;

它应该工作