Answer the question
In order to leave comments, you need to log in
Why can't Yii2 connect to the database on other ports than 3306?
There is some docker-compose. After building the project, the Yii2 site cannot connect to the database if the port is not specified exactly 3306 (external 3306 is also specified in docker-compose), which causes the error SQLSTATE[HY000] [2002] Connection refused
version: "3.9"
services:
nginx:
networks:
- demo_site
build:
context: ./docker/nginx
ports:
- "27015:80"
volumes:
- ./:/var/www/demo_site.store:ro
- ./docker/nginx/config:/etc/nginx/conf.d
php-fpm:
networks:
- demo_site
working_dir: "/var/www/demo_site.store"
build:
context: ./docker/php-fpm
volumes:
- ./:/var/www/demo_site.store
mariadb:
networks:
- demo_site
image: library/mysql:5.7
restart: "always"
volumes:
- ./docker/db/sql.sql:/docker-entrypoint-initdb.d/sql.sql
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- "3361:3306"
networks:
demo_site:
driver: bridge
<?php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=mariadb;port=3361;dbname=demo_site;',
'username' => 'demo_site',
'password' => '',
'charset' => 'utf8',
// Schema cache options (for production environment)
'enableSchemaCache' => true,
'schemaCacheDuration' => 3600 * 24 * 7,
'schemaCache' => 'cache',
];
Answer the question
In order to leave comments, you need to log in
you connect FROM the container, external:internal port - 3361:3306. and you most likely need the image https://hub.docker.com/_/mysql see description
3361 is a port forwarded to the host system so that you can connect to MySQL without entering the container, but inside the container MySQL listens on port 3306.
If you want to connect to a different port from the container with PHP (why?), change the port in the MySQL config .
'dsn' => 'mysql:host=mariadb;port=3361;dbname=demo_site;',
This will not work, because this port is not available from the container, due to the fact that it is located in the host system, that is, outside the container and the virtual network that unites the containers.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question