TransWikia.com

Laravel 6.4.1 SQLSTATE[HY000] [2002] Connection refused

Stack Overflow Asked by Manish Mahajan on January 5, 2022

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=

12 Answers

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.

Answered by Loris Roncali on January 5, 2022

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

Answered by shahab ud din gohar on January 5, 2022

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.

Answered by Melvin Thiribi on January 5, 2022

Just simple step i follow and solved

open .env file

change DB_HOST = 127.0.0.1 to localhost

done

Answered by Ramesh K R on January 5, 2022

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

Answered by Nuha on January 5, 2022

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!

Answered by thesamoanppprogrammer on January 5, 2022

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

Answered by Shahzeb Ahmed on January 5, 2022

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

Answered by aDhaouadi on January 5, 2022

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

Answered by Christian Waltjen on January 5, 2022

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 

Answered by ufuk on January 5, 2022

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

Answered by Tauseef Tariq on January 5, 2022

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

Answered by Salim Djerbouh on January 5, 2022

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP