SQLSTATE[HY000] [2002] Connection refused LARAVEL [duplicate] - php

This question already has answers here:
Laravel SQLSTATE[HY000] [2002] Connection refused
(11 answers)
Closed 1 year ago.
I am trying to run php artisan migrate on a new database I just created. I was having many troubles with connecting to mariadb but I managed to fix it. Now whenever i try to connect to it, for some reason I can type no password or literally any password and i still connect to it. I don't know if that's normal. Anyway, i modified the .env file in my laravel project to
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=liberty_comerce
DB_USERNAME=root
DB_PASSWORD=
But i get the error SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = liberty_comerce and table_name = migrations and table_type = 'BASE TABLE'). If I change DB_HOST=127.0.0.1 to DB_HOST=localhost i get the error SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = liberty_comerce and table_name = migrations and table_type = 'BASE TABLE').
I tried running
php artisan config:clear
php artisan config:cache
php artisan cache:clear
every time i changed anything. I've been looking for answer online but nothing seems to work.

Connection refused comes when .env DB_PORT doesn't match with your local db port. so first check the port and
then clear cache commands:
php artisan config:clear
php artisan config:cache
php artisan cache:clear
and then do
php artisan migrate.

I had the same issue yet to get to the solution to it, I have tried couple of things like changing 127.0.0.1 to localhost, and:
php artisan config:clear
php artisan config:cache
php artisan cache:clear

Related

Queries works Fine but the migration

I'm working on a small project using Laravel 9 / Laravel Sail. I have a weird error. my application queries works fine I can select, update, insert with no problem using Eloquent also. Everything is good.
Now I try to created a new migration and I try to push the migration using the usual php arisan migrate I'm getting an error message :
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mysql failed: Temporary failure in name resolution (SQL: select * from information_schema.tables where table_schema = project and table_name = migrations and table_type = 'BASE TABLE')
But by application works fine its connected to the database as I'm inserting and selecting, updating
the problem is just when I try to migrate which is weird.
my env :
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=project
DB_USERNAME=username
DB_PASSWORD=password
If you're using Laravel Sail, you should run:
sail artisan migrate
instead of:
php artisan migrate
you can try set DB_HOST=localhost
or it can be DB_HOST=0.0.0.0
next execute the
php artisan config:clear
this command will help you, clear the cached config
then you can run your migration
php arisan migrate

SQLSTATE[HY000] [2002] Connection timed out (SQL: select * from `sessions` where `id` = Me2dEG11Zq4pJBhzCoWlhZwZuWPebKdGPLyhuleg limit 1)

I have a problem, my web site was working without problems, but when I enter any product, the web site does not work and gives this error
I made
php artisan cache:clear
php artisan key:generate
php artisan cache:clear
php artisan make:command FlushSessions
php artisan session:flush
And I Deleted the data into the sessions table inside the Database
But the problem has not been resolved
I have the same issue just in .env file change the value of DB_HOST to 127.0.0.1
"change DB_HOST=mysql to DB_HOST=127.0.0.1"

Laravel 8 - SQLSTATE[HY000] [2002] No such file or directory

I'm trying to make a project using Laravel and Sail but I'm getting errors when I'm trying to migrate or do anything related to the project's database (for example, seeding).
More specifically, when using artisan sail migrate or anything related, I'm getting the following errors:
When DB_HOST=localhost in my .env file:
SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = noway and table_name = migrations and table_type = 'BASE TABLE')
When DB_HOST=127.0.0.1 in my .env file:
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = noway and table_name = migrations and table_type = 'BASE TABLE')
What I've tried:
changing the DB_HOST to localhost and 127.0.0.1 (most common answer online, no succes)
making sure SQL is running
clearing cache for sail artisan migrate
./vendor/bin/sail down --rmi all -v to remove all images and volumes
changing username and password and putting them between '' or ""
None of the above have been of succes.
did you run ./vendor/bin/sail up after creating and setting .env ? if not please execute ./vendor/bin/sail down --rmi all -v to remove all images and volumes and then just ran ./vendor/bin/sail up to recreate the images and volumes.
Also as per laravel docs https://laravel.com/docs/8.x/sail you can connect to db by setting env DB_HOST to mysql
T had the same problem with pretty much the same conditions:
-I started with a laravel project then moved it to sail
the way I solved this was by adding an entry DB_PORT=3306 in the .env file and changed DB_HOST=localhost to DB_HOST=mysql as described in laravel docs : https://laravel.com/docs/8.x/sail#mysql
don't forget to check the new credentials that sail uses by default

"Temporary failure in name resolution" when running migration on Homestead VM [duplicate]

This question already has answers here:
"No such file or directory" or "No such host is known" when running migrations
(8 answers)
Closed 2 years ago.
I started a new Laravel 8 Project, I installed a fresh update of vagrant machine "laravel/homestead" in my project. I can "vagrant up" and I can see the mysql database created during the start.
I did "mysql -uhomestead -psecret" on ssh connection and it connect correctly, I can see my database "homestead" that is empty. I can also connect on the database with MySQL Workbench on my host machine with the adress "192.168.10.10:3306" and same credentials.
Now here my .env file :
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
But here the error message when I try to migrate :
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution (SQL: select * from information_schema.tables where table_schema = homestead and table_name = migrations and table_type = 'BASE TABLE')
I also tried with 33060 port and I changed 127.0.0.1 by localhost...
It should work, and it worked on my previous project...
I tried to destroy the machine with vagrant destroy and vagrant up again
I tried to clean cache of laravel with php artisan cache:clear and config:clear etc
But always the same error !
What did I forget or misunderstand ?
Thx everyone !!
Try deleting the DB_HOST=mysql
I think that is causing the issue

I'm having an error while typing php artisan migrate

when typing the command php artisan migrate, it is giving me an error:
Illuminate\Database\QueryException : SQLSTATE[HY000] [1049] Unknown
database 'proj' (SQL: select * from information_schema.tables where
table_schema = proj and table_name = migrations and table_type = 'BASE
TABLE')
Although, i edited the .env file and i had created the database proj and restart the cmd and also i tried
php artisan config:cache
and similar commands but still not working
view error image
phpmyadmin view
.env file view
This means that you don't have this database created. First create the database and after run: php artisan migrate:fresh to create the tables.
By the screenshots you have uploaded, you have many servers. Check the port of the server mysql and change the port on the .env file. This will work.
Open the .env file and edit:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE= // db Name
DB_USERNAME= // db Username
DB_PASSWORD= // db Password
Run below commands:
php artisan config:cache
php artisan migrate
I hope this will help you

Categories