Skip to content
Databases⏱️ 11 min read·

How to Connect MySQL to Laravel and Fix Database Connection Errors (SQLSTATE[HY000] [2002])

Connect MySQL to Laravel 10/11 properly. Step-by-step setup with XAMPP, Laragon, and Docker, plus how to resolve Connection Refused [2002] and Access Denied errors.

MN
Manoj NeupaneSoftware Developer & Programming Instructor
#Laravel#MySQL#PHP#Database#Troubleshooting
🎯 Who This Tutorial Is For

Laravel beginners running into database connection errors when executing 'php artisan migrate'.

📋 Prerequisites
  • ✓PHP 8.2+ installed with pdo_mysql extension
  • ✓MySQL server running locally via XAMPP, Laragon, or standalone MySQL

🚀 What You Will Learn & Build

  • ▸Configuring config/database.php and .env for MySQL
  • ▸Understanding DB_HOST 127.0.0.1 vs localhost in PHP sockets
  • ▸Resolving SQLSTATE[HY000] [2002] Connection refused
  • ▸Resolving SQLSTATE[HY000] [1045] Access denied for user
1

1. The Correct .env Configuration for MySQL

Open your Laravel project's root `.env` file and configure these parameters:
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_laravel_app
DB_USERNAME=root
DB_PASSWORD=

# Tip: If using XAMPP on Windows, DB_PASSWORD is empty by default.
# If using Laragon or Docker, check your custom root password.
2

2. Fixing SQLSTATE[HY000] [2002] Connection Refused

### Symptom: ``` SQLSTATE[HY000] [2002] Connection refused (Connection: mysql, SQL: select * from ...) ``` ### Root Causes & Solutions: 1. **MySQL Service is Stopped:** If using XAMPP, open the XAMPP Control Panel and check whether the MySQL module has a green "Running" status. If not, click **Start**. 2. **Socket vs TCP/IP (localhost vs 127.0.0.1):** In PHP on Unix/macOS, setting `DB_HOST=localhost` instructs PHP to look for a UNIX domain socket (`/tmp/mysql.sock`). Setting `DB_HOST=127.0.0.1` forces standard TCP networking. **Always use 127.0.0.1 in Laravel.** 3. **Port Conflict:** If another service is occupying port 3306, MySQL may fail to start or bind to a different port (like 3307).
3

3. Fixing SQLSTATE[HY000] [1045] Access Denied for User

### Symptom: ``` SQLSTATE[HY000] [1045] Access denied for user 'root'@'127.0.0.1' (using password: NO/YES) ``` ### Solution: Verify user credentials in MySQL:
MySQL Console
-- Open MySQL prompt and grant privileges
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourNewPassword';
FLUSH PRIVILEGES;

✨ Expected Output & Verification

Running 'php artisan migrate' creates default tables (users, password_reset_tokens, failed_jobs, personal_access_tokens) successfully with: INFO Preparing database. INFO Running migrations. 2014_10_12_000000_create_users_table ......... DONE

🧪 How to Test the Implementation

  1. 1Run `php artisan db:show` in terminal to view table list and connection health
  2. 2Run `php artisan migrate:status` to ensure database connection is active

⚠️ Common Errors & How to Fix Them

❌Unknown database 'my_laravel_app'

Root Cause: Database does not exist in MySQL yet.

Fix: Create database in phpMyAdmin: CREATE DATABASE my_laravel_app CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

🛡️ Security Considerations & Best Practices

  • !Never use the 'root' user in production. Create a dedicated database user with restricted permissions for the specific database only.

❓ Frequently Asked Questions (FAQ)

Laravel caches configuration files. Run 'php artisan config:clear' whenever you change database credentials in .env.

📌 Summary of Key Takeaways

  • ✓Use 127.0.0.1 instead of localhost for DB_HOST.
  • ✓Clear config cache with 'php artisan config:clear' after editing .env.

Official References & Further Reading