Docker 将 PHP 容器连接到 MySQL

时间:2023-05-07
本文介绍了Docker 将 PHP 容器连接到 MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有两个容器,一个 apache-php 容器和一个 mysql db 容器.我试图让我的 php 脚本查询我的 sql 数据库.但是,我收到以下错误;

I have two containers, a apache-php container, and a mysql db container. I am trying to get my php script to query my sql database. I am however receiving the following errors;

Fatal error: Uncaught PDOException: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known

Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] No such file or directory

在切换主机地址时,我想我错过了 docker 的一些基本知识,这让我发疯了.

when toggling the host address, i think i am missing something fundamental to docker and this is driving my crazy.

我的 docker compose 看起来像;

My docker compose looks like;

version: '2'

services:

    applications:
      image: tianon/true
      volumes:
        - /var/www/test:/var/www/html

    mysql_store:
      image: tianon/true
      volumes:
        - /var/www/test/mysql:/var/lib/mysql

    apache2:
      build:
        context: ./apache2
      volumes_from:
        - applications
      ports:
        - "80:80"
      depends_on:
       - mysql
      links:
        - mysql
    mysql:
      build:
        context: ./mysql
        volumes_from:
          - mysql_store
      environment:
        - MYSQL_DATABASE=testapp
        - MYSQL_USER=johnm
        - MYSQL_PASSWORD=johnm
        - MYSQL_ROOT_PASSWORD=secret
      volumes:
        - ./mysql/save:/var/lib/mysql
      ports:
        - "3306:3306"

我的 mysql dockerfile 看起来像

my mysql dockerfile looks like

FROM mysql:8

MAINTAINER Mahmoud Zalt <mahmoud@zalt.me>

#####################################
# Set Timezone
#####################################

ARG TZ=UTC
ENV TZ ${TZ}
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN chown -R mysql:root /var/lib/mysql/

ADD my.cnf /etc/mysql/conf.d/my.cnf

CMD ["mysqld"]

EXPOSE 3306

我的 apache dockerfile 看起来像;

my apache dockerfile looks like;

FROM php:7.0-apache
#COPY ./src/ /var/www/html/i
#RUN apt-get update && add-apt-repository ppa:ondrej/php && apt-get update && apt-get install php7.0-mysql
RUN apt-get update && apt-get install -y 
    && docker-php-ext-install pdo pdo_mysql 
    && docker-php-ext-enable pdo pdo_mysql

最后我的 index.php 看起来像

and finally my index.php looks like

<?php
    $db = new PDO('mysql:host=localhost;port=3306;dbname=testapp', 'root', 'secret');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

    var_dump($db->query('SELECT * from users'));

对于我的数据库主机,我尝试了db"、localhost"和127.0.0.1".

For my database host i have tried "db", "localhost" and "127.0.0.1".

对此的任何帮助将不胜感激!

Any help on this will be greatly appreciated!

推荐答案

你需要使用的 DNS 名称是服务名称:mysql:

What you need to use as DNS name is the service name: mysql:

 $db = new PDO('mysql:host=mysql;port=3306;dbname=testapp', 'root', 'secret');

因为你这样命名 MySQL compose 服务:

Because you named as so the MySQL compose service:

    mysql:
      build:
        context: ./mysql
...

这篇关于Docker 将 PHP 容器连接到 MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:从容器内获取 docker 网络的私有 ip 以配置 xdebug remote_host 下一篇:您首选的 php 部署策略是什么?

相关文章