• <small id='bGg1o'></small><noframes id='bGg1o'>

      <bdo id='bGg1o'></bdo><ul id='bGg1o'></ul>

    <tfoot id='bGg1o'></tfoot>

        <i id='bGg1o'><tr id='bGg1o'><dt id='bGg1o'><q id='bGg1o'><span id='bGg1o'><b id='bGg1o'><form id='bGg1o'><ins id='bGg1o'></ins><ul id='bGg1o'></ul><sub id='bGg1o'></sub></form><legend id='bGg1o'></legend><bdo id='bGg1o'><pre id='bGg1o'><center id='bGg1o'></center></pre></bdo></b><th id='bGg1o'></th></span></q></dt></tr></i><div id='bGg1o'><tfoot id='bGg1o'></tfoot><dl id='bGg1o'><fieldset id='bGg1o'></fieldset></dl></div>
      1. <legend id='bGg1o'><style id='bGg1o'><dir id='bGg1o'><q id='bGg1o'></q></dir></style></legend>

        Docker 将 PHP 容器连接到 MySQL

        时间:2023-10-04
        <legend id='CrKyi'><style id='CrKyi'><dir id='CrKyi'><q id='CrKyi'></q></dir></style></legend>

          <small id='CrKyi'></small><noframes id='CrKyi'>

            <bdo id='CrKyi'></bdo><ul id='CrKyi'></ul>
            1. <tfoot id='CrKyi'></tfoot>

                <tbody id='CrKyi'></tbody>

                  <i id='CrKyi'><tr id='CrKyi'><dt id='CrKyi'><q id='CrKyi'><span id='CrKyi'><b id='CrKyi'><form id='CrKyi'><ins id='CrKyi'></ins><ul id='CrKyi'></ul><sub id='CrKyi'></sub></form><legend id='CrKyi'></legend><bdo id='CrKyi'><pre id='CrKyi'><center id='CrKyi'></center></pre></bdo></b><th id='CrKyi'></th></span></q></dt></tr></i><div id='CrKyi'><tfoot id='CrKyi'></tfoot><dl id='CrKyi'><fieldset id='CrKyi'></fieldset></dl></div>
                  本文介绍了Docker 将 PHP 容器连接到 MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有两个容器,一个 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

                  相关文章

                  最新文章

                    <legend id='w47OT'><style id='w47OT'><dir id='w47OT'><q id='w47OT'></q></dir></style></legend>

                    • <bdo id='w47OT'></bdo><ul id='w47OT'></ul>

                    1. <tfoot id='w47OT'></tfoot>

                      <i id='w47OT'><tr id='w47OT'><dt id='w47OT'><q id='w47OT'><span id='w47OT'><b id='w47OT'><form id='w47OT'><ins id='w47OT'></ins><ul id='w47OT'></ul><sub id='w47OT'></sub></form><legend id='w47OT'></legend><bdo id='w47OT'><pre id='w47OT'><center id='w47OT'></center></pre></bdo></b><th id='w47OT'></th></span></q></dt></tr></i><div id='w47OT'><tfoot id='w47OT'></tfoot><dl id='w47OT'><fieldset id='w47OT'></fieldset></dl></div>

                      <small id='w47OT'></small><noframes id='w47OT'>