SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known
Solution 1
Check your DB_HOST
on your .env
file
DB_HOST=http://localhost/
--> DB_HOST=localhost
Result:
I can migrate peacefully now.
php artisan migrate
Migration table created successfully.
Migrated: 2014_10_12_000000_create_users_table
Solution 2
Set DB_HOST=localhost
and run the following artisan command
php artisan config:clear
it will help you, clear cached config
Solution 3
I finally figured out what was causing this error for me. I'm using docker but I wasn't running php artisan migrate
from the shell in the container. I was just running it in Terminal after navigating to the laravel project folder.
Go to your docker dashboard and launch the CLI for the laravel container in the image for your project. Then run the migrate command.
Solution 4
You might also get this error if you are using sail, and you run
php artisan migrate
instead of:
sail artisan migrate
Solution 5
if you are using docker first find your docker container with docker ps command and then execute php artisan migrate command form your container with this command
docker exec -it {containerId}
bash
Related videos on Youtube
code-8
I'm B, I'm a cyb3r-full-stack-web-developer. I love anything that is related to web design/development/security, and I've been in the field for about ~9+ years. I do freelance on the side, if you need a web project done, message me. ;)
Updated on December 31, 2021Comments
-
code-8 over 2 years
I keep this while running
php artisan migrate
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known
My Environment
- Laravel Framework version 5.1.20 (LTS)
- Laravel Installer version 1.2.0
- PHP 5.6.14 (cli) (built: Oct 2 2015 08:55:56)
- mysql Ver 14.14 Distrib 5.6.24, for osx10.10 (x86_64)
.env file
APP_ENV=local APP_URL=http://localhost/ APP_DEBUG=true APP_KEY=**** DB_HOST=http://localhost/ DB_DATABASE=name-local DB_USERNAME=root DB_PASSWORD= UNIX_SOCKET = /tmp/mysql.sock
That data-base
name-local
is exist in my local database.Any hints / suggestions will be a huge help for me.
-
giovannipds over 6 yearsJust commenting if anyone else falls here too, my problem was that my MySQL host server was down. Pay attention to that. It may be the cause as well.
-
Rikard almost 3 yearsThis answer helpt me! Thanks!
-
Faramarz Qoshchi almost 3 yearsor vessel package which you should run migration as
./vessel artisan migrate
-
Bennett over 2 yearsThis helped! However, could you please explain why it will work from the Container CLI but not the WSL Terminal? (I'm pretty new to Sail / Docker. I usually use Laragon)
-
miken32 over 2 yearsHow does this help with a misconfigured database connection while running a migration from the command line?
-
Jakub Ujvvary about 2 yearsThis solution works for me : ) @Bennett - Im also wondering how it is possible that migrate command works fine in macOS terminal and seed command must be ran via docker container cli - someone can explain that?
-
maiakd almost 2 yearsThis guided me to the right direction. I'm using Docker and was running "php" instead of "./vendor/bin/sail"