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:
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
- Deleted the cache files located at
/bootstrap/cache/
. - Tried changing
localhost
to127.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
The default MAMP MySQL port might be 8889, not the standard 3306.
Can you try changing it to the 8889?
You are using
DB_PASSWORD=root
in .env file but in database.phpit is empty.