skip to Main Content

I have a Laravel project set up on my MacBook and I’m encountering an issue with connecting to the database when using PHP artisan serve. Here’s the problem:

When I access the project via localhost/project_name/public, everything works fine, and database connection is also working fine.

However, when I try to use php artisan serve and access the project via http://127.0.0.1:8000 or http://localhost:8000, I encounter the following error:

enter image description here

Here are the relevant sections of my configuration:

.env File

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=my_db
DB_USERNAME=root
DB_PASSWORD=root

database.php file

'mysql' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    ...
]

Here are the troubleshooting steps I’ve already taken. But nothing works for me

1.Cleared configuration cache using commands like

php artisan config:clear 
php artisan cache:clear 
php artisan config:cache 
php artisan optimize
  1. Deleted the cache files located at /bootstrap/cache/.
  2. Tried changing localhost to 127.0.0.1 in the .env file.

Note: I am not using MAMP’s default ports. Instead using 80 & 3306

Despite these attempts, the issue persists. I suspect there might be a configuration issue with MAMP or my macOS settings. Can anyone suggest a solution or further troubleshooting steps? Any help would be greatly appreciated. Thank you in advance!

2

Answers


  1. The default MAMP MySQL port might be 8889, not the standard 3306.

    Can you try changing it to the 8889?

    Login or Signup to reply.
  2. You are using DB_PASSWORD=root in .env file but in database.php

      'password' => env('DB_PASSWORD', ''),
    

    it is empty.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search