0

This question has been asked before but none of the solutions have worked. I am using:

  • OS X 11.0.1
  • PHP 7.4.9
  • mySQL 8.0.20
  • Laravel Framework 8.18.1

I am following a tutorial and I am trying to run some code to connect to the DB.

I am able to connect to the database via Adminer.php and my SQL client, but when trying to access the database, I get:

Illuminate\Database\QueryException SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known (SQL: select name from products where id = 4 limit 1)

My env file:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=storify
DB_USERNAME=root
DB_PASSWORD=rootroot

My guess is that it cannot connect to the local DB instance. I have tried setting the host to 127.0.0.1:3306. I have also tried to edit the database.php to troubleshoot further but the message persists:

        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => '127.0.0.1',
            'database' => env('DB_DATABASE', 'forge'),
            'username' => 'root',
            'password' => 'rootroot',
            'unix_socket' => env('DB_SOCKET', ''),

I also run this command every time I make a change:

php artisan cache:clear

If I change the host to something obviously incorrect, I still get the same error, so Laravel is definitely not finding the host for some weird reason.

My hosts file only contains:

127.0.0.1       localhost       localhost.local

phpAdmin screen:

phpMyAdmin Connection

Any ideas on how to troubleshoot this?

csaborio
  • 111
  • 1
  • 7
  • The entry for `'port' => env('DB_PORT', '3306') ` is missing from the config snippet posted in your question - do you have this entry in the config? – Donkarnash Dec 18 '20 at 20:00
  • You likely don't have a database running on server `127.0.0.1`. MySQL does not use networking in its default configuration. Change the host to `localhost` and ensure your socket is set to point to the correct location. – miken32 Dec 18 '20 at 20:03
  • Sensitive info like `username`, `password`, `host` etc should not be hard coded in the config file as they are not excluded from the vcs. Define such info in `.env` file which is excluded from version control and use `env()` helper in config file – Donkarnash Dec 18 '20 at 20:05
  • Yes, don't touch the database config file at all. Use the env file to change your configuration. – miken32 Dec 18 '20 at 20:06
  • 2
    Thanks for the help...everything in my configuration was fine. After doing a million things, this command made everything work fine: php artisan config:clear – csaborio Dec 20 '20 at 00:19

0 Answers0