gpt4 book ai didi

php - SQLSTATE[HY000] [1045] 拒绝用户 'root' @'localhost' 访问 ../laravel/framework/src/Illuminate/Database/Connectors/Connector.php :70

转载 作者:行者123 更新时间:2023-11-29 07:17:05 27 4
gpt4 key购买 nike

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

  • 清除所有缓存、路由和配置
  • 确保我的 env 文件已配置(我已经尝试将 127.0.0.1 替换为本地主机)
  • 验证登录信息是否正确(我什至创建了一个新用户,但我得到了同样的错误,如果我输入了错误的密码或用户,它确实会抛出正确的错误)
  • 在我提到的两个方面,迁移确实有效!
  • 我将 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');
});
}

最佳答案

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

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

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

更新:

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

关于php - SQLSTATE[HY000] [1045] 拒绝用户 'root' @'localhost' 访问 ../laravel/framework/src/Illuminate/Database/Connectors/Connector.php :70,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58661818/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com