Sqlstate[Hy000] [2002] Connection Refused Within Laravel Homestead

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

laravel 5.6 SQLSTATE[HY000] [2002] connection refused on freash install

I ended up sshing into the homestead vagrant box and running 'artisan migrate:install' which solved the bulk of my problems. I then had to create a database schema partially generated by laravel and the rest edited by myself.

SQLSTATE[HY000] [2002] Connection refused only in browser

It turns out I had the wrong port number. In the .env file it should be 3306 while in Sequel Pro it should be 33060.

According to the Connecting to databases of the Laravel manual:

You should only use these non-standard ports when connecting to the
databases from your host machine. You will use the default 3306 and
5432 ports in your Laravel database configuration file since Laravel
is running within the virtual machine.

SQLSTATE[HY000] [2002] Connection refused on laravel and docker setup

I can see that you have not configured the driver for your laravel network.
You can try put the driver bridge in your Laravel network

networks:
laravel:
driver: bridge

Then in your .env file you can use the name of container instead IP address

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=root
DB_PASSWORD=secret

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



Related Topics



Leave a reply



Submit