Laravel在Docker容器中时无法连接到数据库 [英] Laravel fails to connect to database while in Docker container

查看:561
本文介绍了Laravel在Docker容器中时无法连接到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行(Laravel 6)php artisan migrate时,似乎无法连接到数据库,并显示以下错误:

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.

我的设置: Yaml:

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: {}

起初,我阅读了有关将主机更改为yaml文件所具有的内容的其他答案,并做到了这一点.但是似乎并没有解决问题. Laravel .env

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.

推荐答案

经过研究和思考,问题在于我不是从容器内部而是从外部运行迁移命令.

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.

这是我所做的:

  1. (在VS代码内)docker ps
  2. docker exec -it finance_app_backend_1 bash,结果:root@1bb6f2d30d8a:/var/www/html#
  3. cd finance_app/
  4. root @ 1bb6f2d30d8a:/var/www/html/finance_app#php artisan migration
  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

它奏效了

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# 

这篇关于Laravel在Docker容器中时无法连接到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆