当我运行(Laravel 6)php artisan migrate
时,似乎无法连接到数据库,并显示以下错误:
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
... 异常跟踪:
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:
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
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=finance_app
DB_USERNAME=root
DB_PASSWORD=password
通过命令行,我没有问题:
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>
对于理解和解决问题的任何帮助将不胜感激。
答案 0 :(得分:0)
经过研究和思考,问题是我不是从容器内部而是从外部运行迁移命令。
这是我所做的:
root@1bb6f2d30d8a:/var/www/html#
它奏效了
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#
答案 1 :(得分:0)
对我有用的是更改我的 DB_HOST=host.docker.internal
文件中的 .env