简体   繁体   English

无法连接mysql和php

[英]cannot connect mysql and php

Having this lamp docker setup (Im a sort of docker newbie): 有这个灯底座设置(我是一种码头新手):

docker-compose.yml 泊坞窗,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 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: 在localhost:8088获取此错误消息:

SQLSTATE[HY000] [2002] No such file or directory SQLSTATE [HY000] [2002]没有这样的文件或目录

How can I configure my docker setup to get past this connection problem? 如何配置我的docker设置以解决此连接问题?

Got some hints here: Starting with Zend Tutorial - Zend_DB_Adapter throws Exception: "SQLSTATE[HY000] [2002] No such file or directory" 这里有一些提示: 从Zend教程开始 - Zend_DB_Adapter抛出异常:“SQLSTATE [HY000] [2002]没有这样的文件或目录”

Do I need to install vim and do what they suggest in above or can I solve it in my docker files? 我是否需要安装vim并按照上面的建议执行操作,还是可以在docker文件中解决它?

On webserver image you should define the connection values such as database's hostname,database name, username and password. webserver映像上,您应该定义连接值,例如数据库的主机名,数据库名,用户名和密码。

What can you can is to specify a .env file as seen in https://docs.docker.com/compose/env-file/ 您可以在https://docs.docker.com/compose/env-file/中指定.env文件

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: 然后到您的Dockerfile指定:

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: 然后修改你docker-compose.yml

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. 然后你可以使用php的getenv从你指定的环境变量中检索值。 eg In your case the getenv('MYSQL_DATABASE_NAME') will retreive the value "se_racken_dev" as a string. 例如,在你的情况下, getenv('MYSQL_DATABASE_NAME')会将值“se_racken_dev”作为字符串进行检索。 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 . 一个简单的记忆方式是您在dockerfile中通过ENV指定的任何内容,您可以通过getenv进行检索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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