Database Laravel 8 Laravel 9 Laravel 10 Laravel 11 Laravel 12

Laravel Database Connection Refused Error - Fix

This error occurs when Laravel cannot establish a connection to the database server.

The Error

Error Message
SQLSTATE[HY000] [2002] Connection refused

Common Causes

  1. 1 Database server not running
  2. 2 Incorrect database host in .env
  3. 3 Wrong port number
  4. 4 Firewall blocking connection
  5. 5 Docker networking issues

Solutions

1

Check if database service is running

Bash
# For MySQL
sudo systemctl status mysql

# For PostgreSQL
sudo systemctl status postgresql
2

Verify .env database configuration

ENV
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database
DB_USERNAME=your_username
DB_PASSWORD=your_password
3

Clear configuration cache

Bash
php artisan config:clear
php artisan cache:clear
4

For Docker, use container name as host

ENV
DB_HOST=mysql
# or
DB_HOST=host.docker.internal

Need Help With Your Laravel Project?

I specialize in building custom Laravel applications, process automation, and SaaS development. Whether you need to eliminate repetitive tasks or build something from scratch, let's discuss your project.

Currently available for 2-3 new projects

Hafiz Riaz

About Hafiz

Full Stack Developer from Italy. I build web applications with Laravel and Vue.js, and automate business processes. Creator of ReplyGenius, StudyLab, and other SaaS products.

View Portfolio

Related Errors