SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. (Laravel 8 migration error)

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. (Laravel 8 migration error)

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The error SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known during a Laravel migration is often misunderstood as a database configuration issue, but it is almost exclusively a DNS resolution problem occurring within your local environment. When you attempt to run php artisan migrate on a fresh Laravel 8 installation with Livewire, the framework attempts to connect to a database defined in your .env file. The stack trace provided clearly shows the failure happening at the PDO initialization level (PDO::__construct), specifically when trying to resolve the host string mysql:host=mysql;port=3306.

The root cause lies in the mismatch between the hostname specified in your configuration and the actual machine name available to your operating system's resolver. In the provided error log, the connection string attempts to connect to host=mysql. On a standard local development setup using XAMPP or similar toolkits, there is typically no machine named mysql running on the network or locally. The MySQL server usually resides on the same machine as the PHP application, often listening on localhost (127.0.0.1) or the machine's own hostname.

When PHP's underlying C library calls getaddrinfo, it attempts to translate the host string into an IP address. If the system's /etc/hosts file (on Unix-like systems) or the Windows equivalent does not contain an entry for mysql, and the DNS server cannot resolve the name, the call fails immediately. This results in the PDOException wrapping the socket error, which Laravel then re-throws as a QueryException.

To resolve this without touching complex networking configurations, you must align your environment variable with your local infrastructure. Open your .env file and locate the DB_HOST directive. Change the value from mysql to 127.0.0.1 or localhost.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=lweb
DB_USERNAME=root
DB_PASSWORD=your_password_here

Using 127.0.0.1 is generally preferred in local development environments because it bypasses DNS lookup entirely, forcing the connection to the loopback interface directly. This eliminates any ambiguity regarding hostname resolution and ensures that the PHP process connects to the MySQL server running on the same machine.

If you are using a custom domain name for your project, such as liveweb.local, ensure that this entry exists in your /etc/hosts file (Linux/Mac) or C:\Windows\System32\drivers\etc\hosts (Windows). The line should look like:

127.0.0.1    liveweb.local

However, for the specific error regarding the mysql host, switching to the IP address is the most direct fix. Once the .env file is updated, clear your application cache and re-run the migration command:

php artisan config:clear
php artisan migrate

It is important to note that this error is distinct from authentication failures (which would show SQLSTATE[HY000] [1045]) or permission errors (Access denied). The getaddrinfo failure confirms the TCP connection was never established because the client could not find the target host. By correcting the hostname to a resolvable IP address, you ensure the socket connection can be established successfully before any SQL queries are executed against the lweb database schema.

For developers managing multiple local environments or using Docker, ensuring consistency between the container's internal DNS and the host machine's configuration is equally critical. In Docker setups, the default MySQL service name is often mysql, but if you are running PHP and MySQL on the same host without a container network bridging them, hardcoding the loopback IP remains the safest approach to avoid hostname resolution issues.

Our network