SQLSTATE[HY000] [2002] Connection refused within Laravel homestead
Problem
In Laravel you have config/database.php
where all the setup for the connection is located. You also have a .env
file in the root directory in your project (which everyone uses for timesaving). This contains variables that you can use for the entire project.
On a standard L5 project the MySql section of config/database.php
looks like this:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
Notice there is no port set!
Although in my .env
file I had set DB_PORT=33060
. But that value (3306)
was never read into the config/database.php
.
So don't be a dumbass like myself and forget to check the database.php
file.
FIX
Simply add
'port' => env('DB_PORT', 3306),
to your config/database.php and set that value in .env like this DB_PORT=3306
SQLSTATE[HY000] [2002] Connection refused Laravel 8
Basicly problem was i've added read
and write
ip's in database.php
. If you guys are getting this issue, here is some helpful links;
https://laradock.io/help/
https://www.xspdf.com/resolution/50456690.html
https://laracasts.com/discuss/channels/servers/how-to-fix-sqlstatehy000-2002-connection-refused-error
Hope those help. If there is any question, feel free to comment.
Laravel 7 SQLSTATE[HY000] [2002] Connection refused
The default port for MySQL is 3306 not 3302 you must use like this unless you have changed the port in php.ini
DB_PORT = 3306
Run this command
php artisan config:clear
And instead of using
php artisan serve
Use
php -S 127.0.0.1:8000 -t public/
This might help because if you use artisan serve and make changes to env you must kill the serve and run again.
SQLSTATE[HY000] [2002] Connection refused in Laravel 8
dont forget... WSL :)
use mysql
not localhost.
Related Topics
How to Get the File Path in HTML <Input Type="File"> in PHP
Calling Perl Script from PHP and Passing in Variables, While Also Using Variablized Perl Script Name
How to Convert Excel File into MySQL Database
Admin Product Pages Custom Field Displayed in Cart and Checkout
How to Remove the Fatal Error When Fetching an Assoc Array
What Is the Name for the "<<<" Operator
Official End of Support for PHP4
Why Does PHP 7.4 on Windows Aborts All Command Line Actions That Require User Input
How to Get Attribute of Node with Namespace Using Simplexml
Parse Select Clause of SQL Queries into a PHP Array
Getting the Difference Between Two Time/Dates Using PHP
MySQL & PHP Parameter 1 as Resource
Check If Value Exists Before Inserting into MySQL Db in a PHP Script