Docker连接phpmyadmin和mysql

时间:2018-08-19 14:42:32

标签: mysql docker phpmyadmin

我有docker的lamp和phpmyadmin。

我的设置如下 localhost:80 -> web localhost:8081 -> phpmyadmin

但是,如果我访问phpmyadmin

它显示这样的错误 mysqli_real_connect(): (HY000/1045): Access denied for user 'root'@'172.27.0.4' (using password: YES)

这些是我的设置。

docker-compose.yml

version: '2'
services:
  mysql:
    container_name: lampmysql
    build: ./mysql
    environment:
      MYSQL_ROOT_PASSWORD: passsql
    volumes:
      - db:/var/lib/mysql
  php:
    container_name: lampphp
    build: ./php
    ports:
      - '80:80'
    volumes:
      - ./html:/var/www/html
    depends_on:
      - mysql
  phpmyadmin:
    container_name: lampphpmyadmin
    image: phpmyadmin/phpmyadmin
    environment:
      - PMA_ARBITRARY=1
      - PMA_HOST=mysql
      - PMA_USER=root
      - PMA_PASSWORD=passsql
    links:
      - mysql:mysql
    ports:
      - 8081:80
    volumes:
      - /sessions

volumes:
  db:

phpmyadmin/Dockerfile

FROM php:7.2-fpm-alpine

RUN apk add --no-cache \
    nginx \
    supervisor

# Install dependencies
RUN set -ex; \
    \
    apk add --no-cache --virtual .build-deps \
        bzip2-dev \
        freetype-dev \
        libjpeg-turbo-dev \
        libpng-dev \
        libwebp-dev \
        libxpm-dev \
    ; \
    \
    docker-php-ext-configure gd --with-freetype-dir=/usr --with-jpeg-dir=/usr --with-webp-dir=/usr --with-png-dir=/usr --with-xpm-dir=/usr; \
    docker-php-ext-install bz2 gd mysqli opcache zip; \
    \
    runDeps="$( \
        scanelf --needed --nobanner --format '%n#p' --recursive /usr/local/lib/php/extensions \
            | tr ',' '\n' \
            | sort -u \
            | awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
    )"; \
    apk add --virtual .phpmyadmin-phpexts-rundeps $runDeps; \
    apk del .build-deps

# Copy configuration
COPY etc /etc/

# Copy main script
COPY run.sh /run.sh
RUN chmod u+rwx /run.sh

# Calculate download URL
ENV VERSION 4.8+snapshot
ENV URL https://files.phpmyadmin.net/snapshots/phpMyAdmin-${VERSION}-all-languages.tar.gz
LABEL version=$VERSION

# Download tarball, verify it using gpg and extract
RUN set -ex; \
    curl --output phpMyAdmin.tar.gz --location $URL; \
    tar xzf phpMyAdmin.tar.gz; \
    rm -f phpMyAdmin.tar.gz phpMyAdmin.tar.gz.asc; \
    mv phpMyAdmin-$VERSION-all-languages /www; \
    rm -rf /www/setup/ /www/examples/ /www/test/ /www/po/ /www/composer.json /www/RELEASE-DATE-$VERSION; \
    sed -i "s@define('CONFIG_DIR'.*@define('CONFIG_DIR', '/etc/phpmyadmin/');@" /www/libraries/vendor_config.php; \
    chown -R root:nobody /www; \
    find /www -type d -exec chmod 750 {} \; ; \
    find /www -type f -exec chmod 640 {} \;

# Add directory for sessions to allow session persistence
RUN mkdir /sessions \
    && mkdir -p /www/tmp \
    && chmod -R 777 /www/tmp

# We expose phpMyAdmin on port 80
EXPOSE 80

ENTRYPOINT [ "/run.sh" ]
CMD ["phpmyadmin"]

1 个答案:

答案 0 :(得分:1)

我的建议是将links中的phpmyadmin更改为:- mysql:mysql_db_server

此外,尝试将所有环境变量添加到mysql

MYSQL_ROOT_PASSWORD=root
MYSQL_DATABASE=db_name
MYSQL_HOST= mysql
MYSQL_USER=root
MYSQL_PASSWORD=root
MYSQL_PORT=3306