20

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=
Fabian S.
  • 2,441
  • 2
  • 24
  • 34
Manish Mahajan
  • 2,062
  • 1
  • 13
  • 19

15 Answers15

22

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

18

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
Salim Djerbouh
  • 10,719
  • 6
  • 29
  • 61
13

Just simple step i follow and solved

open .env file

change DB_HOST = 127.0.0.1 to localhost

done

Ramesh K R
  • 173
  • 1
  • 5
  • In higher versions of laravel >7, the DB_HOST does not accept the use of the local IP address (127.0.0.1) but instead uses the URL : localhost. I don't know why but using localhost instead of 127.0.0.1 works – stanley mbote Jan 12 '21 at 06:29
4

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.

Matrix11
  • 192
  • 1
  • 12
3

I had the same problem and when I applied the following operations, the problem was solved.

    php artisan key:generate
    php artisan cache:clear
    php artisan route:clear
    php artisan config:clear 
    php artisan view:clear 
ufuk
  • 367
  • 3
  • 16
  • 1
    Code only answers can almost always be improved by the addition of some explanation. A litany like this is great, but it doesn't explain why each step should be taken, or what benefit you believe each step will yield. The goal of an answer should be to educate the reader as well as to provide a working solution. – Jason Aller Mar 07 '20 at 16:53
2

Try out doing

php artisan config:cache

then on your .env file specify your database and your port

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE= YOUR_DATABASE_NAME
DB_USERNAME=root
DB_PASSWORD=

then run your mysql server and run

php artisan migrate

Note: Make sure your Xammp server is perfectly fine & have already started mysql (using apache server or any server you are using) and also make sure you already created your database before running php artisan migrate Hope it works for you

1

I had the same issue. It was resolved by just restarting the local host server i.e Xampp

1

In most cases it's an issue with the port configuration. You need to check which port your server is running on. Then you have to edit the .env file of your project. For instance if you are using mamp the port should be changed to 8889 instead of the default 3606 for laravel. The same applies to laravel 8.

Thomas Urban
  • 4,649
  • 26
  • 32
0

Try this conf in .env (i'm using Laravel v7.0), it's working for me :

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=root
aDhaouadi
  • 17
  • 2
0

I had this issue, I use MAMP for MYSQL server. It was a configuration issue that I solved by changing the port to the correct one I found in MAMP instructions, which is 8889. So the correct configuration in my .env file is:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=dbname
DB_USERNAME=root
DB_PASSWORD=rootpassword
Nuha
  • 314
  • 1
  • 3
  • 15
0

I'm using laravel 8 and mysql with docker, i had the same problem i tried several solutions and none worked, i deleted my containers and volumes and nothing too, so i decided to restart my os and go into incognito mode and boomm it went smoothly so i went back for my tab that was giving error and I deleted the session cookies and boom the problem was solved, another tip is to use the database container IP using docker inspect [container--database-id] instead of DB_HOST=localhost. Sorry for the writing, I don't speak English, I hope I helped.

Verify port:

'${FORWARD_DB_PORT:-3306}:3306'
Danilo Santos
  • 392
  • 3
  • 11
0

In my case (Windowsx64 + WSL + php/laravel8.1) I was able to add data source via PhPStorm/PyCharm using valid credentials but php artisan migrate ended up with SQLSTATE[HY000] [2002] Connection refused error every time.

I had to stop MySql service from XAMPP control panel and setup DB and USER via mysql-cli

after stopping services from XAMPP, do sudo service mysql start and then:

sudo mysql -u root

SHOW DATABASES;
CREATE USER 'elite'@'localhost' IDENTIFIED BY 'elite';
CREATE DATABASE laravel
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, INDEX, DROP, ALTER, CREATE 
TEMPORARY TABLES, LOCK TABLES ON laravel.* TO 'elite'@'localhost';
exit
Abdullah
  • 93
  • 14
-1

Damn, I feel so stupid lol. This is what helped me after scratching my head for hours. Check what port your MySQL is running on and that's the one you use in your .env file.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=laravel
DB_USERNAME=yourusername
DB_PASSWORD=yourpassword

Make sure when you create your user that you grant all privileges for the user. If you're still confused then ask me and ill find time to help you out. Happy coding everyone!

Mr. Disability
  • 799
  • 1
  • 10
  • 19
-1

I was using docker compose file. All you need to do is to expose the port of the mysql

db:
  image: mysql
  command: --default-authentication-plugin=mysql_native_password
  restart: always
  environment:
    MYSQL_ROOT_PASSWORD: example
    MYSQL_DATABASE: example
    MYSQL_USER: example
    MYSQL_PASSWORD: example
  ports:
    - 3306:3306
adminer:
  image: adminer
  restart: always
  ports:
    - 8080:8080
-1

It was actually this in my case.

DB_HOST=127.0.0.1
DB_PORT=33060

Before it was

DB_HOST=127.0.0.1
DB_PORT=3306

Many solutions were suggesting to change DB_HOST to "localhost". In my case it was just a tiny little 0 in DB_PORT.