1

When I run (Laravel 6) php artisan migrate it appears that I am failing to connect to the db with the error:

 Illuminate\Database\QueryException  : SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known (SQL: select * from information_schema.tables where table_schema = finance_app and table_name = migrations and table_type = 'BASE TABLE')

  at /Users/robertrocha/Documents/finance_app/system/finance_app/vendor/laravel/framework/src/Illuminate/Database/Connection.php:665

.... Exception trace:

  1   PDOException::("PDO::__construct(): php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known")
      /Users/robertrocha/Documents/finance_app/system/finance_app/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  2   PDO::__construct("mysql:host=db;port=3306;dbname=finance_app", "root", "password", [])
      /Users/robertrocha/Documents/finance_app/system/finance_app/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  Please use the argument -v to see more details.

My Setup: Yaml:

version: '3.3'

services:
    backend:
        build:
          context: laravel
        depends_on:
          - db
        image: php-apache-laravel
        ports:
            - "10000:80"
        restart: unless-stopped
        volumes:
          - ./system:/var/www/html
        environment:
          DB_HOST: db
          DB_PORT: 3306
          DB_USERNAME: root
          DB_PASSWORD: password
          DB_DATABASE: finance_app
    db:
        image: mysql:5.7
        environment:
            MYSQL_ROOT_PASSWORD : password
            MYSQL_DATABASE: finance_app
            MYSQL_USER: root
            MYSQL_PASSWORD: password
        volumes:
          - db_data:/var/lib/mysql
        restart: unless-stopped
volumes:
    db_data: {}

At first I read other answers about changing the host to what your yaml file has and did that. But it seems that did not solve the issue. Laravel .env

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=finance_app
DB_USERNAME=root
DB_PASSWORD=password

Via command line I have no problem connecting:

Roberts-Air:finance_app robertrocha$ docker ps
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS              PORTS                   NAMES
1bb6f2d30d8a        php-apache-laravel   "docker-php-entrypoi…"   4 minutes ago       Up 2 minutes        0.0.0.0:10000->80/tcp   finance_app_backend_1
8b6c33d4a74f        mysql:5.7            "docker-entrypoint.s…"   4 minutes ago       Up 2 minutes        3306/tcp, 33060/tcp     finance_app_db_1


Roberts-Air:finance_app robertrocha$ docker exec -it finance_app_db_1 bash
root@8b6c33d4a74f:/# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.28 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| LaravelPOS         |
| finance_app        |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.01 sec)

mysql> 

Any help understanding and solving the problem would be greatly appreciated.

2
  • Try remove MYSQL_USER and MYSQL_PASSWORD. These vars are optional so just use root user details for now. You could also try docker-compose exec backend php artisan migrate for a better error message. Commented Nov 25, 2019 at 17:07
  • If not that try adding ``` ports: - '3306:3306' ``` Commented Nov 25, 2019 at 17:19

2 Answers 2

5

After some research and thinking about it, the problem was that I was not running the migrate command from within the container but from the outside.

Here is what I did:

  1. (Within VS Code) docker ps
  2. docker exec -it finance_app_backend_1 bash, result: root@1bb6f2d30d8a:/var/www/html#
  3. cd finance_app/
  4. root@1bb6f2d30d8a:/var/www/html/finance_app# php artisan migrate

And it worked

Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (0.07 seconds)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (0.06 seconds)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (0.03 seconds)
Migrating: 2019_11_25_012607_create_cateagories_table
Migrated:  2019_11_25_012607_create_cateagories_table (0.04 seconds)
root@1bb6f2d30d8a:/var/www/html/finance_app# 
Sign up to request clarification or add additional context in comments.

1 Comment

I did the same mistake :P
1

What worked for me is to change DB_HOST=host.docker.internal in my .env file

1 Comment

Perfect! Likewise!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.