【问题标题】:SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' at ../laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70SQLSTATE[HY000] [1045] 用户 'root'@'localhost' 在 ../laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70 的访问被拒绝
【发布时间】:2020-02-27 21:41:20
【问题描述】:

我正在使用带有 virtualbox 的 vagrant。将数据插入数据库时​​出现此错误。我试图修复错误的事情:

  • 清除所有缓存、路由和配置
  • 确保我的 env 文件已配置(我已经尝试将 127.0.0.1 替换为 localhost)
  • 验证登录信息是否正确(我什至创建了一个新用户,但我得到了同样的错误,如果我输入了错误的密码或用户,它确实会抛出正确的错误)
  • 在我提到的两个方面,迁移确实有效!
  • 我将 xampp phpmyadmin 上的数据插入到表中,它也可以工作
  • 我的 laravel 版本是 6.4.1,我从 repo 下载了 homestead 并使用命令 laravel new laravel "name of application" 创建项目

我遇到了这个问题(我不知道还能做什么),每次我尝试使用 homestead 使用 laravel 插入数据时,我都会在日志中收到此错误:

[previous exception] [object] (PDOException(code: 1045): SQLSTATE[HY000] [1045] Access denied for 

user 'root'@'localhost' (using password: NO) at /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70)
[stacktrace]
#0 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(70): PDO->__construct('mysql:host=loca...', 'root', '', Array)
#1 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(46): Illuminate\\Database\\Connectors\\Connector->createPdoConnection('mysql:host=loca...', 'root', '', Array)
#2 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Database/Connectors/MySqlConnector.php(24): Illuminate\\Database\\Connectors\\Connector->createConnection('mysql:host=loca...', Array, Array)
#3 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php(182): Illuminate\\Database\\Connectors\\MySqlConnector->connect(Array)
#4 [internal function]: Illuminate\\Database\\Connectors\\ConnectionFactory->Illuminate\\Database\\Connectors\\{closure}()
#5 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(920): call_user_func(Object(Closure))
#6 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(453): Illuminate\\Database\\Connection->getPdo()
#7 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(658): Illuminate\\Database\\Connection->Illuminate\\Database\\{closure}('insert into pos...', Array)
#8 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(625): Illuminate\\Database\\Connection->runQueryCallback('insert into pos...', Array, Object(Closure))
#9 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(460): Illuminate\\Database\\Connection->run('insert into pos...', Array, Object(Closure))
#10 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(412): Illuminate\\Database\\Connection->statement('insert into pos...', Array)
#11 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php(349): Illuminate\\Database\\Connection->insert('insert into pos...', Array)
#12 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(239): Illuminate\\Database\\DatabaseManager->__call('insert', Array)
#13 /home/vagrant/code/first-app/routes/web.php(42): Illuminate\\Support\\Facades\\Facade::__callStatic('insert', Array)
#14 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Routing/Route.php(205): Illuminate\\Routing\\RouteFileRegistrar->{closure}()
#15 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Routing/Route.php(179): Illuminate\\Routing\\Route->runCallable()
#16 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(680): Illuminate\\Routing\\Route->run()
#17 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(130): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#18 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#19 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#20 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(76): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#21 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#22 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#23 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\View\\Middleware\\ShareErrorsFromSession->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#24 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(56): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#25 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Session\\Middleware\\StartSession->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#26 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#27 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#28 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(66): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#29 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Cookie\\Middleware\\EncryptCookies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#30 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(105): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#31 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(682): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#32 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(657): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request))
#33 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(623): Illuminate\\Routing\\Router->runRoute(Object(Illuminate\\Http\\Request), Object(Illuminate\\Routing\\Route))
#34 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(612): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request))
#35 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(176): Illuminate\\Routing\\Router->dispatch(Object(Illuminate\\Http\\Request))
#36 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(130): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}(Object(Illuminate\\Http\\Request))
#37 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#38 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#39 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#40 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#41 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#42 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#43 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(62): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#44 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#45 /home/vagrant/code/first-app/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#46 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Fideloper\\Proxy\\TrustProxies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#47 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(105): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#48 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#49 /home/vagrant/code/first-app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))
#50 /home/vagrant/code/first-app/public/index.php(55): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
#51 {main}
"} 

我的 .env 文件具有以下配置:

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:zkV082cDVD5ScGfF26O2RpY8RFYZvmuyd8Qo7jBJCuY=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=localhost (or 127.0.0.1)
DB_PORT=3306
DB_DATABASE=laravel_test
DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

我在 web.php 上的代码如下:

Route::get('/insert', function (){
   DB::insert('insert into posts(title, content) values(?,?)', ['Php','Laravel']);
});

我用代码做了一个迁移:

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('title');
        $table->text('content');
        $table->timestamps();
        $table->tinyInteger('is_admin')->default('0');
    });
}

【问题讨论】:

  • 这是一个 SQL 错误而不是 laravel 错误
  • 检查 SQL 表的权限(SQL 用户)也正确配置你的 laravel .env 文件
  • @BobbyAxe 是的,它是正确的,我使用了下面提到的 pr1nce 命令,现在我收到此错误“C:\laravel-apps\first-app\storage\logs”并且它不可构建:协议错误
  • 您的站点是否使用虚拟机?
  • 好吧,您的 vagrant 安装无法访问您的 xamp 数据库。

标签: php mysql laravel


【解决方案1】:

问题不在于代码,而在于您的 env 文件。

使用正确的凭据设置您的 env 文件,然后运行 ​​php artisan config:cache 命令,以便 laravel 加载您的凭据。

在您提到的问题中,您清除了缓存。这还不够你需要使用config:cache

更新:

根据存储文件夹的新问题运行php artisan optimize:clear

【讨论】:

  • 一直在寻找这样的东西,但它可以工作,但是......现在我收到这个错误“C:\laravel-apps\first-app\storage\logs 没有现有目录”和它不可构建:协议错误
  • 更新了我的答案试试吧
  • 我在本地做这一切。我试图用谷歌搜索,但大多数答案都提到了 FTP 服务器或更改 linux 上的权限。
  • 您是否运行了我在答案中发布的新命令?我根据您的新错误在几分钟前更新了我的答案
  • @pr1nce 我这次尝试了它,它又回到了我遇到的最后一个错误:(。
【解决方案2】:

好吧,您的 vagrant 安装无法访问您的 xamp 数据库。

当您通过 Windows 中的 CMD 启动迁移时,它会使用您机器上的数据库 (xamp)

但是当您通过 vagrant 虚拟机上的 Web 服务器访问它时,它将使用该机器的数据库。默认情况下,用户为“root”,密码为“root”或“123”。

无论哪种情况,您都应该使用正确的凭据更新您的 .env,并从虚拟机的 CLI (vagrant ssh) 进行迁移。

【讨论】:

    猜你喜欢
    • 2020-12-05
    • 2019-12-01
    • 2018-07-22
    • 2021-09-01
    • 1970-01-01
    • 2017-11-05
    • 2017-08-27
    • 2015-06-15
    相关资源
    最近更新 更多