SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known - A Comprehensive Guide to Solving Laravel Database Connection Issues on OpenShift Body:

Connecting to a database is crucial for many modern web applications. Unfortunately, you may encounter errors when your application attempts to establish the connection with the database server. In this blog post, we'll discuss the SQLSTATE[HY000] [2002] error and how to address it in Laravel projects hosted on OpenShift. Additionally, we will provide valuable best practices for managing your database environment variables.

Common Errors and Their Causes

In Laravel, configuring the database connection is usually done through the .env file. The code above shows a typical sample configuration used when connecting to a MySQL database via OpenShift. However, this may lead to errors due to misconfiguration or network issues. Two common examples are:

1. SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known - This indicates a problem with the database hostname, which is often caused by an incorrect server address or missing DNS resolution. 2. mysqli::real_connect() failed because of (HY000/1045): Access denied for user 'username'@'localhost' (using password: YES) - This error appears due to insufficient database permissions, the wrong hostname, or an incorrect username and password combination.

Diagnosing the Problem

When you encounter these errors, you need to troubleshoot your application's connection settings and environment variables. Follow these steps to analyze the issue:

1. Inspect the database host configuration in both your .env file and OpenShift console. Ensure they are consistent and correct. 2. Check if the database user has sufficient permissions to connect from remote locations (OpenShift IP addresses). Modify the user's privileges as needed using the MySQL command line interface or a suitable database management tool. 3. Verify that your application is running on a trusted OpenShift network with proper DNS resolution configured, which can sometimes be an issue when hosting applications on shared servers or using VPN connections. 4. If none of the above resolve the problem, try disabling the SSL connection by changing 'ssl' to false within the database configuration array in your .env file. This might help if there are issues with certificate trust between the server and the client. 5. In case all else fails, it may be necessary to obtain OpenShift support for further troubleshooting or investigate whether there is an issue specific to your software stack or environment settings.

Best Practices for Managing Database Environment Variables

To ensure a smooth connection, follow these practices when managing database environment variables:

1. Use the .env file for all environment-related configuration and hostnames, including database information. By keeping sensitive data separate from the codebase, it's easier to maintain and secure the application. 2. Consider using environment variables or a configuration management system like Laravel Forge to store and manage database credentials and other private information. This keeps secrets safe from unauthorized access and makes deploying applications simpler. 3. Regularly review your .env file for consistency, correctness, and security. Make sure the right combination of hostname, port, username, password, and other details are specified correctly. 4. If using OpenShift, refer to their recommended practices for working with environment variables and storing sensitive data securely within the platform.

Conclusion

To summarize, solving database connection issues in Laravel applications on OpenShift requires a thorough analysis of the problem, including correct configuration of hostnames, user permissions, and network settings. Following best practices for managing environment variables will help you maintain and secure your application's database connections for smooth operation.

Note: The above example code is for illustration purposes only - make sure to use the correct values for your specific project setup.