[英]Laravel won't connect to database server that set in .env file
I started a new Laravel 9.x + JsonApi project on docker-compose with 2 containters, 'web' and 'mysql'.我在 docker-compose 上启动了一个新的 Laravel 9.x + JsonApi 项目,其中包含 2 个容器,“web”和“mysql”。 Artisan migrations, seeding and db:show works fine.
工匠迁移、播种和 db:show 工作正常。 But when I'm trying to load a page from browser or Postman, it returns DB error
但是当我试图从浏览器或 Postman 加载页面时,它返回数据库错误
`SQLSTATE[HY000] [2002] php.network_getaddresses: getaddrinfo for mariadb failed: Temporary failure in name resolution `SQLSTATE[HY000] [2002] php.network_getaddresses: mariadb的 getaddrinfo 失败:名称解析暂时失败
select * from redirects
where redirects
. select * from
redirects
where redirects
。 id
= 1 limit 1 ` id
= 1 限制 1 `
The weird part is that my DB host is mysql and 'mariadb' string is never mentioned in my code (except some vendor files), nor in docker files, nor in.env file.奇怪的是,我的数据库主机是mysql ,我的代码(除了一些供应商文件)、docker 文件和 in.env 文件中都没有提到“mariadb”字符串。 I also checked container environment variables - nothing there.
我还检查了容器环境变量——什么也没有。
Artisan scripts work fine.工匠脚本工作正常。
Here are my settings, if it helps.这是我的设置,如果有帮助的话。 Mariadb was never mentioned, it was mysql from the very beginning.
从未提及 Mariadb,从一开始就是 mysql。
docker-compose.yml docker-compose.yml
version: "3.4"
services:
mysql:
image: mysql
volumes:
- mysql_db:/var/lib/mysql
ports:
- 3306:3306
environment:
- MYSQL_ROOT_PASSWORD=***
- MYSQL_DATABASE=url_shortener
web:
build:
dockerfile: Dockerfile.web.dev
context: .
#image: php:alpine
volumes:
- "./:/app"
- "/app/src/vendor"
- web_root_home:/root
working_dir: /app/src/
command: "php artisan serve --host=0.0.0.0 --port=7999"
ports:
- 8000:7999
depends_on:
- mysql
volumes:
mysql_db:
web_root_home:
Dockerfile.web.dev Dockerfile.web.dev
#FROM php
FROM bitnami/laravel
WORKDIR /app/src/
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
ENV COMPOSER_ALLOW_SUPERUSER=1
RUN apt update
RUN apt install -y git build-essential vim mc mlocate
RUN pecl install xdebug
COPY ./src /app/src
COPY ./src/composer.json ./
COPY ./src/.env ./
RUN composer install
RUN php artisan key:generate
CMD ["php", "artisan", "serve", "--host=0.0.0.0", "--port=8000"]
.env (DB part) .env(数据库部分)
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=url_shortener
DB_USERNAME=root
DB_PASSWORD=***
app/config/database.php wasn't changed app/config/database.php 没有改变
artisan db:show工匠数据库:显示
root@a3c873713ee1:/app/src# php artisan db:show
MySQL 8 ...................................................................
Database .................................................... url_shortener
Host ................................................................ mysql
Port ................................................................. 3306
Username ............................................................. root
URL .......................................................................
Open Connections ........................................................ 3
Tables .................................................................. 6
Total Size ........................................................ 0.09MiB
Table .......................................................... Size (MiB)
redirects ............................................................ 0.02
failed_jobs .......................................................... 0.02
migrations ........................................................... 0.02
personal_access_tokens ............................................... 0.02
password_resets ...................................................... 0.02
users ................................................................ 0.02
migrations and seeding works fine, I can connect to mysql from the host laptop.迁移和播种工作正常,我可以从主机笔记本电脑连接到 mysql。 All seeds data are there
所有种子数据都在那里
I tried to clear config cache, didn't help我试图清除配置缓存,没有帮助
root@a3c873713ee1:/app/src# php artisan config:clear
INFO Configuration cache cleared successfully.
I also reloaded containers, no luck as well我也重新加载了容器,也没有运气
Stack trace isn't useful error screenshot堆栈跟踪没有用错误屏幕截图
I'm out of ideas what to try and where it takes this mariadb host at all.我完全不知道要尝试什么,也不知道把这个 mariadb 主机带到哪里。
I could solve the problem by defining the DATABASE_URL=mysql://root:PASSWORD@mysql/url_shortener in my.env file.我可以通过在 my.env 文件中定义 DATABASE_URL=mysql://root:PASSWORD@mysql/url_shortener 来解决这个问题。 Still no idea where it got mariadb host from.
仍然不知道它从哪里得到 mariadb 主机。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.