I am using vagrant with virtualbox. I get this error when inserting data to database. Things I have tried to fix the error:
- Clear all caches, routes and config
- Made sure my env file is configured (I already tried to replace the 127.0.0.1 to localhost)
- Verify if the login info is correct (I even made a new user and I get the same error, If i enter wrong pass or user it does throw the correct error)
- On the side of the two I mentioned Migrations do work!
- I inserted data on the xampp phpmyadmin to the table and it also works
- My laravel version is 6.4.1 and I downloaded homestead from the repo and used the command laravel new laravel “name of application” to create the proyect
I am having this problem (I dont know what else to do) that everytime I attempt to insert data with laravel using homestead i get this error on the logs:
[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}
"}
My .env file has the following configuration:
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}"
My code is the following on web.php:
Route::get('/insert', function (){
DB::insert('insert into posts(title, content) values(?,?)', ['Php','Laravel']);
});
I made a migration with the code:
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');
});
}
2
Answers
The problem is not in the code but in your env file.
Setup your env file with the right credentials and then run
php artisan config:cache
command so laravel will load your credentials.In your question you mentioned you cleared cache. That’s not enough you need to use
config:cache
Update:
Run
php artisan optimize:clear
based on your new issue with the storage folder.well, your vagrant installation can’t access your xamp database.
When you launch migration via the CMD in your windows, it uses the database on your machine (xamp)
But when you access it through the web server on your vagrant virtual machine, it will uses that machine’s database. wich by default has user “root” and password “root” or “123”.
Either case, you should update your
.env
with the right credentials and migrate from the CLI of your virtual machine (vagrant ssh
).