SQLSTATE[HY000] [2005] Unknown MySQL server host 'localhost:3306' (2)

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Solving the SQLSTATE[HY000] Unknown MySQL Server Host Error in Laravel: A Guide for Environment Migrations

As a senior developer, I frequently encounter environment-related headaches when migrating projects between different operating systems, especially when dealing with database connections. The error you are facing—SQLSTATE[HY000] [2005] Unknown MySQL server host 'localhost:3306'—is one of the most common stumbling blocks in Laravel development, particularly during environment transitions like moving from macOS to Linux.

This error doesn't necessarily mean your database is broken; it means the PHP application (Laravel) cannot resolve or connect to the specified hostname (localhost) on the specified port (3306). This usually points to a mismatch between how the application expects the database to be located and how the operating system resolves those names in the new environment.

Here is a comprehensive breakdown of why this happens and the exact steps you need to take to fix it.


Understanding the Root Cause: Hostname Resolution Discrepancies

The core issue lies in networking and hostname resolution, not necessarily the database server itself. When you run Laravel on Linux, the concept of localhost can behave differently depending on whether you are running the application directly or within a containerized environment (like Docker).

When your .env file specifies DB_HOST=localhost, the PHP PDO driver attempts to connect to the machine where the PHP process is running. If that machine cannot find a valid external network path for localhost:3306 to reach the MySQL server, the connection fails with the "Unknown host" error.

Common Scenarios Leading to This Error:

  1. Environment Mismatch: The configuration was set up assuming a specific networking setup on macOS that doesn't translate directly to your new Linux environment.
  2. MySQL Service Status: The MySQL server might not be running or accessible on the expected IP address within the Linux VM/host.
  3. Docker Misconfiguration: If you are using Docker, localhost inside the container refers only to the container itself, not the host machine's network.

Step-by-Step Fixes for Your Laravel Database Connection

Follow these steps sequentially to diagnose and resolve the connection issue:

1. Verify MySQL Server Status on Linux

First and foremost, ensure that your MySQL server is actually running and accessible on your Linux system.

Action: Open your Linux terminal and check the status of the MySQL service.

sudo systemctl status mysql
# Or, depending on your distribution:
sudo service mysql status

If the status shows it is inactive or failed, start the service immediately:

sudo systemctl start mysql

2. Re-evaluate the DB_HOST Configuration

Since you are moving environments, rely on a more explicit configuration than just localhost.

Action: Open your Laravel .env file and review the database settings.

If you are running MySQL directly on the same Linux machine as PHP, try explicitly using the loopback IP address:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1  # Change from 'localhost' to the explicit IP address
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_user
DB_PASSWORD=your_password

Using 127.0.0.1 instead of localhost often bypasses subtle networking resolution issues between the PHP process and the database server, resolving the "Unknown host" error. Remember that configuration is key when setting up robust systems, as emphasized by best practices in frameworks like those found on laravelcompany.com.

3. Check MySQL Binding (Advanced)

In some highly secured Linux setups (especially within Docker or VMs), the MySQL server might be configured to only listen on a specific interface (e.g., 127.0.0.1) instead of all interfaces (0.0.0.0). If it's bound strictly to 127.0.0.1, attempting to connect via an external route might still fail if the network layer is restrictive.

Action: Check your MySQL configuration file (often /etc/mysql/my.cnf or similar) to ensure the bind-address setting allows external connections, or at least connections from the PHP execution environment.

4. Firewall Inspection

If steps 1 and 2 fail, a firewall might be blocking port 3306 entirely.

Action: Check your Linux firewall rules (using ufw or firewalld) to ensure that traffic on port 3306 is allowed between the application server and the database server.


Conclusion

The error SQLSTATE[HY000] [2005] is almost always a networking symptom rather than a database schema issue. By systematically checking the MySQL service status, explicitly setting the hostname to 127.0.0.1, and inspecting firewall rules, you will effectively bridge the gap created by your environment migration.

Always treat your environment configuration as a critical piece of code. Adopting these troubleshooting steps ensures that your Laravel application maintains robust and predictable connections, allowing you to focus on building features rather than debugging infrastructure concerns. For more deep dives into Laravel architecture and best practices, always refer to resources from laravelcompany.com.