Warholm's avatar

Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] Connection refused

Hey - I'm completely new to Laravel. I'm trying to do Laravel+Vue and make a user login/register with searching included. However, I cannot seem to even get the login to work on a Mac.

Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = homestead and table_name = migrations)

That is the full error.

I've installed composer and installed Laravel from composer. I downloaded PHP as well.

1 like
18 replies
Seilem's avatar

Try to set this in your .env file

DB_HOST=localhost
2 likes
foxhassam's avatar

@Seilem I'm using Fedora. and by setting DB_HOST=127.0.0.1 to DB_HOST=localhost. and now it worked. Thanks.

1 like
Warholm's avatar

PDOException::("SQLSTATE[HY000] [2002] No such file or directory")

Is what I get when I run php artisan migrate with that.

Here is my .env file with the change you stated:

APP_NAME=Laravel
APP_ENV=local
APP_KEY=myApiKey
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

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

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
SESSION_LIFETIME=120
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
1 like
Seilem's avatar

Try again this this:

DB_HOST=127.0.0.1
DB_PORT=33060
1 like
Warholm's avatar

PDOException::("SQLSTATE[HY000] [2002] Connection refused")

Is what I get. I don't know why though.

1 like
biishmar's avatar

@Warholm

R u using homestead for your project? if not,

DB_DATABASE=dbname
DB_USERNAME=root
DB_PASSWORD=
1 like
Vijay's avatar

Try clearing your cache and config cache:

php artisan cache:clear 
php artisan config:clear

and then run migration

php artisan migrate
4 likes
zainudinnoori's avatar

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 you 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

5 likes
vincent15000's avatar

It's not a good idea to cache the configuration in development mode.

Warholm's avatar
Warholm
OP
Best Answer
Level 1

I did not install mySQL. Whoopsies. I didn't know mySQL workbench and mySQL community server needed to be installed from mySQL Community Downloads after my PHP installation.

Thanks for the help guys!

2 likes
imsyuan's avatar

php artisan config:cache

It's worked for me.

2 likes
bruceCorazon's avatar

i have the same problem but i'm using docker and i wanted to run php artisan migrate in my Dockerfile

2 likes
abdulalimrony's avatar

Well, I got the same problem. But when I started my local server (XAMPP), It started running successfully. Maybe we also can use laravel homestead

1 like
n0cturn0's avatar

When using Docker compose, each service gets a local Docker DNS entry with its service name (unless configured differently), so the service mysql will be reachable at mysql, no need to mess with IPs that might change.

Example.:

DB_CONNECTION=mysql

DB_HOST=mysql

DB_PORT=3306

DB_DATABASE=database

DB_USERNAME=sail

DB_PASSWORD=password

1 like

Please or to participate in this conversation.