0

I received the following error, "Port must be configured within host parameter. like localhost:3306 in /var/www/html/dbm2/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php" when trying to go to go to my dev site.

Here is my env.php file:

<?php
return [
    'cache_types' => [
        'config' => 1,
        'layout' => 1,
        'block_html' => 1,
        'collections' => 1,
        'reflection' => 1,
        'db_ddl' => 1,
        'eav' => 1,
        'customer_notification' => 1,
        'target_rule' => 1,
        'full_page' => 1,
        'config_integration' => 1,
        'config_integration_api' => 1,
        'translate' => 1,
        'config_webservice' => 1,
        'compiled_config' => 1,
        'vertex' => 1,
        'google_product' => 1
    ],
    'db' => [
        'connection' => [
            'default' => [
                'dbname' => 'dbm2',
                'host' => '127.0.0.1:3307',
                'username' => 'root',
                'password' => '',
                'model' => 'mysql4',
                'engine' => 'innodb',
                'initStatements' => 'SET NAMES utf8;',
                'active' => '1',
                'profiler' => '1'
            ]
        ],
        'table_prefix' => ''
    ],
    'cache' => [
        'frontend' => [
            'default' => [
                'backend' => 'Cm_Cache_Backend_Redis',
                'backend_options' => [
                    'server' => 'redis',
                    'database' => '0',
                    'port' => '6379'
                ]
            ],
            'page_cache' => [
                'backend' => 'Cm_Cache_Backend_Redis',
                'backend_options' => [
                    'server' => 'redis',
                    'port' => '6379',
                    'database' => '1',
                    'compress_data' => '0'
                ]
            ]
        ]
    ],
    'MAGE_MODE' => 'developer',
    'backend' => [
        'frontName' => 'admin'
    ],
    'crypt' => [
        'key' => 'abfd843e41c08030189ad4ebaf1191f0'
    ],
    'resource' => [
        'default_setup' => [
            'connection' => 'default'
        ]
    ],
    'x-frame-options' => 'SAMEORIGIN',
    'session' => [
        'save' => 'redis',
        'redis' => [
            'host' => 'redis',
            'port' => '6379',
            'password' => '',
            'timeout' => '2.5',
            'persistent_identifier' => '',
            'database' => '2',
            'compression_threshold' => '2048',
            'compression_library' => 'gzip',
            'log_level' => '1',
            'max_concurrency' => '6',
            'break_after_frontend' => '5',
            'break_after_adminhtml' => '30',
            'first_lifetime' => '600',
            'bot_first_lifetime' => '60',
            'bot_lifetime' => '7200',
            'disable_locking' => '0',
            'min_lifetime' => '60',
            'max_lifetime' => '2592000'
        ]
    ],
    'install' => [
        'date' => 'Fri, 06 Apr 2018 22:04:26 +0000'
    ],
    'system' => [
        'default' => [
            'dev' => [
                'debug' => [
                    'debug_logging' => '0'
                ]
            ],
            'smile_elasticsuite_core_base_settings' => [
                'es_client' => [
                    'servers' => '172.17.0.1:9250',
                    'enable_https_mode' => '0',
                    'enable_http_auth' => '0',
                    'http_auth_user' => '',
                    'http_auth_pwd' => ''
                ]
            ]
        ]
    ]
];

I am using docker and I have multiple containers for different versions of databases since I am developing multiple sites that require different database versions to run correctly. Because of this, I need to access the database through port 3307 for this particular site. The above configuration works for running basic M2 commands (setup:upgrade, setup:static:deploy, cache:flush, etc). When I try and visit the site, however, the above error shows.

I can't connect to the database with localhost or with the docker container's ip address, only 127.0.0.1 works. I have 127.0.0.1 mapped to localhost in my hosts file, but it doesn't work.

Here is my hosts file:

  GNU nano 4.8                                                 /etc/hosts                                                            
127.0.0.1       localhost
127.0.1.1       eric-virtual-machine
172.19.0.200 mailhog
172.19.0.100 redis
172.19.0.110 nginx
172.19.0.177 php72
172.19.0.178 php73
172.19.0.17 php7
172.19.0.171 php71
172.19.0.170 php7
172.19.0.156 php56
172.19.0.105 db
172.19.0.106 db2
172.19.0.130 elasticsearch
172.19.0.132 elasticsearch2
172.19.0.135 elasticsearch5
172.19.0.136 elasticsearch6
172.19.0.115 rabbitmq
172.19.0.3 varnish3
172.19.0.4 varnish4
0

Could you provide yours docker-compose.yml? Assuming, that you have multiple service/containers for each database (like. db_1, db_2).

Yours /etc/hosts file does not play any role in networking between docker containers. It does not route through your host network. That said, if you have application container and database containers in same docker network, you can access their IP address via their (service) names.

So for example, if you want to point your mangento applicaiton to database #2, just use:

'db' => [
        'connection' => [
            'default' => [
                'dbname' => 'dbm2',
                'host' => 'db_2:3306',

3306 instead of yours 3307, assuming, that 3306 is internal database container port, and 3307 is binded host port. There, you need to use internal port.

Hope it helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.