Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 6.4.1 SQLSTATE[HY000] [2002] Connection refused

I am new in Laravel development. I have updated Xampp to 7.3.11 on my Mac Mojave 10.14.6. In Laravel project when I hit php artisan migrate command I got following error.

SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations and table_type = 'BASE TABLE')

When I start Xampp service, my admin panel run on http://127.0.0.1:8080/phpmyadmin. My working project in Laravel is also not connecting with database saying connection refused. I tried by changing DB_Port and DB_Host in .env file. I tried by clearing cache.

Any Help will be appreciated.

My .env file

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
like image 788
Manish Mahajan Avatar asked Nov 01 '19 11:11

Manish Mahajan


4 Answers

I've come across this Error too by building up a new project with Laravel running in docker-compose for development.

My solution was to compare the prebuild .env-File with the actual credentials I used for building the database container. Especially I was using DB_HOST=127.0.0.1 instead of the correct service name of my docker-compose setup: DB_HOST=mysql

like image 56
Christian Waltjen Avatar answered Nov 17 '22 23:11

Christian Waltjen


I had the same issue while running laravel and mysql within a docker container (MacOs).
I figured out that the problem was within the .env file.

the default configuration in .env was :

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_db_name
DB_USERNAME=sail
DB_PASSWORD=password

Some answers suggested to change DB_HOST to:

DB_HOST = localhost

But it didn't work for me...

After some research I found out that, while running laravel in docker, the DB_HOST expects the database service that is running in docker too, in our case the service is mysql.


So I had to change the .env to this:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=my_db_name
DB_USERNAME=sail
DB_PASSWORD=password

I also changed the DB_HOST in config/database.php

'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', 'mysql'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'my_db_name'),
            'username' => env('DB_USERNAME', 'sail'),
            'password' => env('DB_PASSWORD', 'password'),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

It's only then that everything started working fine.

like image 33
Matrix11 Avatar answered Sep 28 '22 02:09

Matrix11


Open localhost/phpmyadmin and find a tab called User accounts.

Find the root user and set its password in your .env and also don't forget to create the database named laravel if it doesn't exist

Then you can clear config cache

php artisan config:clear

And migrate

php artisan migrate
like image 16
Salim Djerbouh Avatar answered Nov 17 '22 22:11

Salim Djerbouh


Just simple step i follow and solved

open .env file

change DB_HOST = 127.0.0.1 to localhost

done

like image 12
Ramesh K R Avatar answered Nov 17 '22 23:11

Ramesh K R