简体   繁体   中英

cannot connect mysql and 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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM