SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. (Laravel 8 migration error)
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Title: Understanding and Solving SQLSTATE[HY000] [2002] Errors in Laravel 8 Migration Process
Introduction
After installing a new Laravel 8 project with Jetstream and Livewire, you may encounter an error when trying to run the command 'php artisan migrate'. One of the common errors is "SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known." This issue can be frustrating, especially for developers who are new to Laravel. To understand it better and find a solution, read through this comprehensive guide.
Why Does This Error Occur?
The error message points to connection issues between your Laravel project and the database server. Specifically, the MySQL driver could not connect due to address-related problems such as incorrect hostname, port, or database name settings.
Problem Analysis
To troubleshoot this issue, first, examine your database configuration settings in your ".env" file, which is usually found at app/config/. Make sure that the connection settings are correct:
1. Database Host: Set "DB_HOST" to the proper hostname or IP address (e.g., 'mysql' for localhost).
2. Port Number: Ensure that your port number is correctly configured. By default, MySQL uses port 3306. If you use a different port, replace it with the correct one (e.g., '3306' for non-standard ports).
3. Database Name: Check if your database name is typed properly and matches your actual database name in the server.
4. Username and Password: Ensure that the credentials provided are accurate.
If everything seems fine, it could still be possible that the Laravel installation or some other configuration file might be causing trouble.
Solution 1: Check Your Laravel Installation
You can try to install a new Laravel project with Jetstream and Livewire on another computer or different environment and compare the project files. If there's no error for the same command in the new installation, you may assume that there are compatibility issues between the PHP extensions and database drivers used by your Laravel installation or conflicts with another installed component (e.g., XAMPP, MAMP, etc.). To solve this issue, you can try:
1. Updating PHP and database driver extensions to their latest versions, if any updates exist.
2. Reinstalling a fresh Laravel installation, ensuring that all dependencies are up-to-date and compatible with your server configuration.
3. Checking for any conflicting or incompatible components affecting the installation process.
Solution 2: Try a Different Database Connection Method
If none of the previous solutions work, you can try setting up your database connection differently using PDO (PHP Data Objects). Laravel provides multiple ways to connect to a database through PDO. For example, you could add the following code snippet to your ".env" file:
DB_CONNECTION=pdo
PDO_DSN=mysql:host=localhost;dbname=lweb
PDO_USERNAME=root
PDO_PASSWORD='' (or set it if MySQL is configured with a password)
This new connection configuration will connect to your database using PDO instead of the default Laravel driver. If that works, you can assume that your previous connection settings were incorrect or had conflicts with PHP extensions.
Conclusion
When faced with SQLSTATE[HY000] [2002] errors during Laravel 8 migration execution, it is essential to first check and correct your database configuration settings in the ".env" file. If the issue persists, try installing a fresh project or using alternative connection methods such as PDO, which may provide additional insights into the cause of the error. Remember that a well-configured Laravel environment ensures a smooth migration process for your projects.