dongshan7060 2017-04-19 12:18
浏览 325

无法连接mysql和php

Having this lamp docker setup (Im a sort of docker newbie):

docker-compose.yml

version: '2'

services:
  webserver:
    build: .
    ports:
      - "8080:80"
      - "443:443"
    volumes:
      - ./:/var/www/html
    links:
      - db
  db:
    image: mysql:5.6
    ports:
      - "3306:3306"
    volumes:
       - /var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=adminpasswd
      - MYSQL_DATABASE=se_racken_dev



phpmyadmin:
    image: phpmyadmin/phpmyadmin:latest
    ports:
      - "88:80"
    links:
      - db:db

Dockerfile

FROM php:5.6-apache

RUN apt-get update -y && apt-get install -y libpng-dev curl libcurl4-openssl-dev

RUN docker-php-ext-install pdo pdo_mysql gd curl

RUN a2enmod rewrite
RUN service apache2 restart

Just cant get my local environment to work.

Get this error message at localhost:8088:

SQLSTATE[HY000] [2002] No such file or directory

How can I configure my docker setup to get past this connection problem?

Got some hints here: Starting with Zend Tutorial - Zend_DB_Adapter throws Exception: "SQLSTATE[HY000] [2002] No such file or directory"

Do I need to install vim and do what they suggest in above or can I solve it in my docker files?

  • 写回答

1条回答 默认 最新

  • dongyue3795 2017-04-19 12:58
    关注

    On webserver image you should define the connection values such as database's hostname,database name, username and password.

    What can you can is to specify a .env file as seen in https://docs.docker.com/compose/env-file/

    In your case that should be:

    DB_HOSTNAME=db:/var/run/mysqld/mysqld.sock
    DB_USER=root
    DB_PASSWORD=somepassword
    DB_NAME=se_racken_dev
    

    Then to your Dockerfile specify:

    FROM php:5.6-apache
    
    ENV MYSQL_HOSTNAME="localhost"
    ENV MYSQL_PASSWORD=""
    ENV MYSQL_USERNAME="root"
    ENV MYSQL_DATABASE_NAME=""  
    
    RUN apt-get update -y && apt-get install -y libpng-dev curl libcurl4-openssl-dev
    
    RUN docker-php-ext-install pdo pdo_mysql gd curl
    
    RUN a2enmod rewrite
    RUN service apache2 restart
    

    Then modify your docker-compose.yml like that:

    version: '2'
    
    services:
      webserver:
        build: .
        ports:
          - "8080:80"
          - "443:443"
        volumes:
          - ./:/var/www/html
        links:
          - db
        environment:
         - MYSQL_HOSTNAME=$DB_HOSTNAME
         - MYSQL_PASSWORD=$DB_USER
         - MYSQL_USERNAME=$DB_PASSWORD
         - MYSQL_DATABASE_NAME=$DB_NAME
    
      db:
        image: mysql:5.6
        ports:
          - "3306:3306"
        volumes:
           - /var/lib/mysql
        environment:
          - MYSQL_ROOT_PASSWORD=$DB_PASSWORD
          - MYSQL_DATABASE=$DB_NAME
    
    phpmyadmin:
        image: phpmyadmin/phpmyadmin:latest
        ports:
          - "88:80"
        links:
          - db:db
    

    Then you can use php's getenv to retreive the values from enviromental variables you specified. eg In your case the getenv('MYSQL_DATABASE_NAME') will retreive the value "se_racken_dev" as a string. So you need to modify the database configuration file in order to retreive correctly the database connection credentials using the function mewntioned above.

    A simple way to remember is whatever you specify via ENV in your dockerfile you can retreive via getenv.

    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示