Docker Database Connection Issue

User
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for db failed: No such host is known.
when using docker , apache ,adminer
php artisan migrate says no db host
Assistant
The error you're encountering (`SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for db failed: No such host is known.`) typically means that Laravel is unable to connect to the database because it can't resolve the hostname `db`. In a Docker environment, this usually happens if the service or container name for the database is incorrect or if there’s a networking issue between containers.

Here are steps to troubleshoot and resolve this issue:

### 1. **Verify the Service Name in `docker-compose.yml`**
If you’re using Docker Compose, the database hostname in your Laravel configuration should match the service name of the database defined in your `docker-compose.yml` file. Look for something like this:

services:
     db:
       image: mysql:latest
       environment:
         MYSQL_DATABASE: your_database
         MYSQL_USER: your_user
         MYSQL_PASSWORD: your_password
         MYSQL_ROOT_PASSWORD: your_root_password

– Ensure the service name is `db` (or whatever you've named it).
– If it’s not `db`, update the `DB_HOST` in the `.env` file to match the service name. For example, if the service name is `mysql-db`, update `.env` like so:

DB_HOST=mysql-db

### 2. **Check the `.env` Configuration**
Ensure your `.env` file has the correct database configuration:

DB_CONNECTION=mysql
   DB_HOST=db
   DB_PORT=3306
   DB_DATABASE=your_database
   DB_USERNAME=your_user
   DB_PASSWORD=your_password

– `DB_HOST` should be the same as the service name of the database container (usually `db`).
– Double-check the username, password, and database name to ensure they match the environment variables defined in `docker-compose.yml`.

### 3. **Check Docker Networking**
Ensure all services (Apache, Adminer, Laravel, DB) are on the same network. You can verify this by checking the `docker-compose.yml` to ensure the `network` directive is used properly.

services:
     web:
       ...
       networks:
         - your_network
     db:
       ...
       networks:
         - your_network
   networks:
     your_network:
       driver: bridge

If you’re not using Docker Compose, make sure you manually connect the containers to the same network using:

docker network connect your_network your_container_name

### 4. **Restart Docker Containers**
After making changes to the `docker-compose.yml` or `.env` file, restart your containers:

docker-compose down
   docker-compose up -d

### 5. **Verify the Database Container**
Ensure that the database container is up and running by executing:

docker ps

You should see the `db` service running. If it’s not, inspect the logs for errors:

docker-compose logs db

### 6. **Test the Database Connection**
You can also use `Adminer` to check if you can manually connect to the database from the Adminer UI. Ensure you are using the same hostname (`db`), username, password, and database as in your `.env` file.

By following these steps, you should be able to resolve the `php_network_getaddresses` error and successfully connect to your database.

Leave a Comment

Your email address will not be published. Required fields are marked *