Docker和Redis:SQLSTATE [HY000] [2002]连接被拒绝

时间:2019-09-22 18:17:37

标签: docker redis

在对包括Redis在内的PHP项目进行泊坞处理之后,我面临着连接数据库的小问题。我收到错误消息:SQLSTATE[HY000] [2002] Connection refused

似乎我的端口不正确(但也许我错了),我应该设置8889。怎么做?

这是我的功能:

public function initCache()
    {
        if (_CACHE_ != 'ON')
            return false;

        if (!defined('_SYSTEM_IN_DEV_'))
            define('_SYSTEM_IN_DEV_', 'OFF');
        if (isset($this->smarty) && !empty($this->smarty))
            $this->smarty->assign('inDev', _SYSTEM_IN_DEV_);

        require_once _ROOT_ . 'lib/classRedis.php';
        $this->cache = new IgRedis();
        $this->cache->connect('172.19.0.1');
        $this->cache->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
        $this->cache->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);

        if ($this->smarty) {
            try {
                $svn_rev = $this->cache->get('svn_revision');
                if (!$svn_rev) {
                    $svn_rev = time();
                    $svn_rev = $this->cache->set('svn_revision', $svn_rev);
                }
                $this->smarty->assign('SVN_REV', $svn_rev);
            } catch (Exception $e) {
                error_log("Couldn't connected to Redis");
                error_log($e->getMessage());
            }
        }
    }

这是我的docker-compose文件:

version: "2"
services:
    www:
        build: .
        ports: 
            - "80:80"
        volumes:
            - ./www/.htaccess:/var/www/html/.htaccess
            - ./www/lib:/var/www/html/lib
            - ./www/modules:/var/www/html/modules
            - ./www/themes:/var/www/html/themes
            - ./www/index.php:/var/www/html/index.php
        links:
            - db
            - redis
        environment:
            DATABASE_PASSWORD: ******
    db:
        image: mariadb:10.2
        environment:
            MYSQL_DATABASE: art
            MYSQL_USER: ******
            MYSQL_PASSWORD: ******
            MYSQL_ROOT_PASSWORD: ******
        volumes:
            - ./dump:/docker-entrypoint-initdb.d
            - mariadb-data:/var/lib/mysql
    phpmyadmin:
        image: phpmyadmin/phpmyadmin
        links: 
            - db:db
        ports:
            - 8001:80
        environment:
            MYSQL_USER: ******
            MYSQL_PASSWORD: ******
            MYSQL_ROOT_PASSWORD: ******
    redis:
        image: redis:5.0.5
        ports:
            - "6379:6379"
volumes:
    mariadb-data:

最后我的Dockerfile:

FROM composer AS vendor

COPY composer.json .
RUN composer install
RUN composer update

FROM php:7.3-apache

RUN docker-php-ext-install mysqli pdo_mysql
RUN a2enmod rewrite
RUN service apache2 restart

RUN pecl install redis && docker-php-ext-enable redis

COPY --from=vendor /app/vendor/ vendor/
COPY www ./

RUN mkdir templates_c
RUN mkdir cache
RUN chown www-data:www-data templates_c cache

VOLUME /var/www/html/templates_c/
VOLUME /var/www/html/cache/

欢迎任何帮助

0 个答案:

没有答案