尝试从Docker容器连接到本地mysql数据库时拒绝连接

时间:2019-01-02 09:42:19

标签: mysql docker docker-compose lumen host

我在从Docker容器连接本地mysql数据库时遇到问题。我将docker-compose与容器中的两个服务一起使用,数据库不在容器中

我有这个docker-compose文件:

version: '2'
services:
    web:
        build:
            context: ./
            dockerfile: web-dev.dockerfile
        volumes:
            - ./:/var/www
        ports:
            - "8080:80"
        links:
            - app
        network_mode: "bridge"
        dns:
            - 10.0.50.6
    app:
        build:
            context: ./
            dockerfile: app-dev.dockerfile
        volumes:
            - ./:/var/www
        network_mode: "bridge"
        dns:
            - 10.0.50.6

web容器是带有此Dockerfile的Nginx服务:

FROM nginx:1.10

ADD ./vhost.dev.conf /etc/nginx/conf.d/default.conf

WORKDIR /var/www

和此配置文件:

server {
    listen 80;

    index index.php index.html;
    root /var/www/formapp/public;

    location / {
        try_files $uri /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

app容器是具有以下Dockerfile的应用程序服务:

FROM php:7-fpm

ENV USER=pasquale

RUN apt-get update && apt-get install -y libmcrypt-dev mysql-client \
    openssl zip unzip git nano wget libaio-dev iputils-ping

RUN mkdir -p /opt/oracle/instantclient_10_2
# Download files from in oracle folder:
#   https://agora.zanichelli.it/downloads/basic-10.2.0.5.0-linux-x64.zip
#   https://agora.zanichelli.it/downloads/sdk-10.2.0.5.0-linux-x64.zip

ADD oracle/basic-10.2.0.5.0-linux-x64.zip /opt/oracle/basic-10.2.0.5.0-linux-x64.zip
ADD oracle/sdk-10.2.0.5.0-linux-x64.zip /opt/oracle/sdk-10.2.0.5.0-linux-x64.zip
RUN unzip /opt/oracle/basic-10.2.0.5.0-linux-x64.zip -d /opt/oracle \
    && unzip /opt/oracle/sdk-10.2.0.5.0-linux-x64.zip -d /opt/oracle \
    && ln -s /opt/oracle/instantclient_10_2/libclntsh.so.10.1 /opt/oracle/instantclient_10_2/libclntsh.so \
    && ln -s /opt/oracle/instantclient_10_2/libclntshcore.so.10.1 /opt/oracle/instantclient_10_2/libclntshcore.so \
    && ln -s /opt/oracle/instantclient_10_2/libocci.so.10.1 /opt/oracle/instantclient_10_2/libocci.so

ADD oracle/tns-admin/tnsnames.ora /opt/oracle/instantclient_10_2/network/admin/tnsnames.ora
ENV LD_LIBRARY_PATH /opt/oracle/instantclient_10_2/

RUN docker-php-ext-install pdo_mysql \
    && pecl install mcrypt-1.0.1 \
    && docker-php-ext-enable mcrypt \
    && pecl install mongodb \
    && docker-php-ext-enable mongodb \
    && docker-php-ext-configure pdo_oci --with-pdo-oci=instantclient,/opt/oracle/instantclient_10_2,10.2 \
    && echo 'instantclient,/opt/oracle/instantclient_10_2' | pecl install oci8 \
    && docker-php-ext-install pdo_oci \
    && docker-php-ext-enable oci8

RUN yes | pecl install xdebug \
    && echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.remote_enable=1' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.default_enable=1' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.remote_connect_back=1' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.remote_autostart=1' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.remote_handler="dbgp"' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.remote_port=9000' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.remote_host=0.0.0.0' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.remote_log=/var/www/xdebug.log' >> /usr/local/etc/php/conf.d/xdebug.ini

RUN mkdir -p /home/$USER
RUN groupadd -g 1000 $USER
RUN useradd -u 1000 -g $USER $USER -d /home/$USER
RUN chown $USER:$USER /home/$USER
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

WORKDIR /var/www
USER $USER

使用docker-compose -f docker-compose.dev.yml up --build -d启动容器可以正常工作,并且docker ps命令给出以下输出:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                           NAMES
7202e2862ef5        190todb_web         "nginx -g 'daemon of…"   9 hours ago         Up About an hour    443/tcp, 0.0.0.0:8080->80/tcp   190todb_web_1_3c628ae1c69b
d45c04d353d5        190todb_app         "docker-php-entrypoi…"   9 hours ago         Up About an hour    9000/tcp                        190todb_app_1_dd2ac7028b87

我在项目文件夹formapp中安装了一个lumen应用程序,然后创建了一个播种机,以便在我的数据库中插入伪数据,并且如果运行/projectfolder/formapp$ php artisan db:seed,我可以从bash中运行该播种机,并且我拥有输出:

Seeding: UsersTableSeeder
Database seeding completed successfully.

然后,我创建了一条通过lumen应用访问我的用户表的路线:

$router->get('users', function () use ($router) {
    return User::all();
});

我的流明环境文件是这样的:

APP_NAME=Lumen
APP_ENV=local
APP_KEY=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9
APP_DEBUG=true
APP_URL=http://localhost
APP_TIMEZONE=UTC

LOG_CHANNEL=stack
LOG_SLACK_WEBHOOK_URL=

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=form_db
DB_USERNAME=root
DB_PASSWORD=radiohead

CACHE_DRIVER=file
QUEUE_CONNECTION=sync

JWT_SECRET=JhbGciOiJIUzI1N0eXAiOiJKV1QiLC

但是如果我尝试从http://localhost:8080/users连接,则会出现以下流明错误:

SQLSTATE[HY000] [2002] Connection refused (SQL: select * from `users`)

我尝试更改DB_HOST,但无法解决问题:

0.0.0.0 (SQLSTATE[HY000] [2002] Connection refused (SQL: select * from `users`));
172.17.0.1 (SQLSTATE[HY000] [1130] Host '172.17.0.2' is not allowed to connect to this MySQL server (SQL: select * from `users`));

172.17.0.1是我的docker0 inet地址。

如何配置我的项目才能正常工作?

PS:流明应用程序已启动并正在运行,仅是数据库连接无法正常工作

1 个答案:

答案 0 :(得分:1)

当数据库和应用程序不在同一服务器上时,您不能使用localhost

您需要允许通过以下方式进行访问:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'172.17.0.2' IDENTIFIED BY '<password>';

您可以将172.17.0.2替换为通配符%