Symfony5 connection refused to mysql using docker - php

I have the following docker-compose.yml
version: '3.9'
services:
web:
build: .
container_name: web
volumes:
- .:/var/www/html
ports:
- 8000:80
networks:
- db_bridge
db:
image: mysql
container_name: db
restart: always
environment:
MYSQL_ROOT_PASSWORD: ${DATABASE_ROOT_PASSWORD}
MYSQL_DATABASE: ${DATABASE_NAME}
MYSQL_USER: ${DATABASE_USER}
MYSQL_PASSWORD: ${DATABASE_PASSWORD}
ports:
- 3306:3306
networks:
- db_bridge
networks:
db_bridge:
driver: bridge
.env file looks as following:
###> Mysql ###
DATABASE_ROOT_PASSWORD=root
DATABASE_NAME=everphone
DATABASE_USER=demo
DATABASE_PASSWORD=password
###< Mysql ###
DATABASE_URL="mysql://demo:password#db:3306/everphone?serverVersion=8.0"
###< doctrine/doctrine-bundle ###
However when i try to run:
bin/console make:migration
I get the following error:
An exception occurred in driver: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client
Can you please tell me what is the problem here?

Related

Docker compose: getaddrinfo for mariadb failed

I'm quite bad at creating docker-compose and I currently have a problem when I try to build a new project.
After the build I can't access my mariadb server within my php app container, I've got this error:
PDO::__construct(): php_network_getaddresses: getaddrinfo for mariadb
failed: Name does not resolve
So I know something is misconfigured but I don't know what, yet. I tried many changes but nothing that worked.
Here is my current docker-compose.yml:
version: "3.8"
networks:
# used by some services (php) to communicate with other docker-compose.yaml
censored.com:
external:
name: censored.com
services:
app:
build:
context: .
target: symfony_php
args:
- secret=id=composerauth,src=${HOME}/.composer/auth.json
restart: unless-stopped
healthcheck:
interval: 10s
timeout: 3s
retries: 3
start_period: 30s
environment:
APP_ENV: dev
HOST: www.censored.lan
networks:
- default
- censored.com
volumes:
- ./:/srv/app:rw,cached
- ./docker/php/conf.d/symfony.dev.ini:/usr/local/etc/php/conf.d/symfony.ini
- ${HOME}/.composer/auth.json:/root/.composer/auth.json
# If you develop on Linux, comment out the following volumes to just use bind-mounted project directory from host
- ./var/cache:/srv/app/cache:rw
- ./var/log:/srv/app/logs:rw
depends_on:
- mariadb
extra_hosts:
- www.censored.lan:127.0.0.1
nginx:
build:
context: .
target: symfony_nginx
args:
- secret=id=composerauth,src=${HOME}/.composer/auth.json
restart: unless-stopped
depends_on:
- app
environment:
NGINX_DOMAIN: www.censored.lan
ports:
- 8001:80
volumes:
- ./docker/nginx/templates/dev.conf.template:/etc/nginx/templates/default.conf.template:ro
- ./docker/nginx/rules/rules.dev.conf:/etc/nginx/rules.conf:ro
- ./public:/srv/app/public:ro
- ./src:/srv/app/src:ro
mariadb:
image: mariadb:10.7
environment:
MYSQL_ROOT_PASSWORD: changeme
MYSQL_DATABASE: database
MYSQL_USER: user
MYSQL_PASSWORD: changeme
networks:
- default
- censored.com
ports:
- '3307:3306'
restart: on-failure
volumes:
- db_data:/var/lib/mysql
volumes:
db_data: {}
Can someone help me to fix the issue please?
Thanks !
The problem wasn't related to my docker-compose configuration file.
My app is a PHP Symfony app and it was doing a "cache:clear" after the initial "composer install" during the build. The "cache:clear" was triggering calls to the database which wasn't ready yet.
To solve this I just had to set my mariadb version to my "DATABASE_URL" parameter in my Symfony app, to avoid useless database queries.

Unable to login to MySQL server

I get the following errors when I try to access phpmyadmin.
1
mysqli::real_connect(): php_network_getaddresses:
getaddrinfo failed: Temporary failure in name resolution
2
mysqli::real_connect(): (HY000/2002): php_network_getaddresses:
getaddrinfo failed: Temporary failure in name resolution
version: '3.7'
services:
mysqldb:
container_name: mysqldb
image: mysql
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: Root193782
MYSQL_DATABASE: admin
ports:
- 3306:3306
volumes:
- /data/mysql:/var/lib/mysql
networks:
- mysql-net
phpmyadmin:
container_name: phpmyadmin
image: phpmyadmin
environment:
MYSQL_ROOT_PASSWORD: Root193782
ports:
- 8080:80
volumes:
- /data/php/admin/uploads.ini:/usr/local/etc/php/conf.d/php-phpmyadmin.ini
networks:
- mysql-net
networks:
mysql-net:
driver: bridge
==================================================
I managed to solve it, passing the environment variables below. below
version: '3.7'
networks:
mysql-net:
driver: bridge
services:
mysqldb:
container_name: mysqldb
image: mysql
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: Root193782
MYSQL_DATABASE: admin
ports:
- 3306:3306
volumes:
- /data/mysql:/var/lib/mysql
networks:
- mysql-net
phpmyadmin:
container_name: phpmyadmin
image: phpmyadmin/phpmyadmin:latest
environment:
- PMA_HOSTS=mysqldb
- PMA_ARBITRARY=1
- PMA_USER=root
- PMA_PASSWORD=Root193782
ports:
- 8080:80
volumes:
- /data/php/admin/uploads.ini:/usr/local/etc/php/conf.d/php-phpmyadmin.ini
networks:
- mysql-net

docker-compose, WordPress and MariaDB, PHP Fatal error: Uncaught mysqli_sql_exception: Connection refused

I have a WP project with following docker-compose configuration. When I try to connect my http://localhost to access the installation, I get 500 and I read this in the log:
PHP Fatal error: Uncaught mysqli_sql_exception: Connection refused in /var/www/html/web/wp/wp-includes/wp-db.php
I assume there is something wrong with the ip or the host but the weird thing is that phpmyadmin is working fine, and it connects with mysql without issues.
version: '3.9'
services:
nginx:
image: nginx:latest
container_name: ${APP_NAME}-nginx
ports:
- '80:80'
volumes:
- "./nginx/:/etc/nginx/templates/"
- ./src:/var/www/html:rw,cached
- ./certs:/etc/certs
environment:
- "NGINX_ENVSUBST_TEMPLATE_SUFFIX=.conf"
- "DOMAIN=${DOMAIN}"
depends_on:
- wordpress
restart: always
mysql:
image: mariadb:latest
container_name: ${APP_NAME}-mysql
command: --lower_case_table_names=2
volumes:
- './data/db:/var/lib/mysql:delegated'
environment:
- MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
- MYSQL_DATABASE=${DB_NAME}
restart: always
ports:
- '3307:3306'
wordpress:
build:
context: .
dockerfile: Dockerfile
container_name: ${APP_NAME}-wordpress
volumes:
- ./src:/var/www/html:rw,cached
- ./config/php.ini:/usr/local/etc/php/conf.d/php.ini
environment:
XDEBUG_ENABLED: 1
XDEBUG_CONFIG: remote_host=host.docker.internal
PHP_IDE_CONFIG: serverName=localhost
env_file:
- src/.env
depends_on:
- mysql
restart: always
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: ${APP_NAME}-phpmyadmin
volumes:
- ./config/phpmyadmin.ini:/usr/local/etc/php/conf.d/phpmyadmin.ini
environment:
PMA_HOST: "${DB_HOST}"
PMA_PORT: 3306
MYSQL_ROOT_PASSWORD: "${DB_ROOT_PASSWORD}"
ports:
- '8083:80'
links:
- mysql:mysql
mailhog:
container_name: ${APP_NAME}-mailhog
image: mailhog/mailhog
ports:
- "8025:8025"
- "1025:1025"
composer:
image: composer
container_name: ${APP_NAME}-composer
working_dir: /var/www/html
restart: 'no'
volumes:
- ./src:/var/www/html:rw,cached
My wordpress .env, among the other settings, set the host like the following:
DB_HOST="mysql:3307"
But I also tried
DB_HOST="mysql:3306"
or simply
DB_HOST="mysql"
Do you have any suggestions?
Thanks.
your Wordpress Instance and MySQL Instance isn't connected. Try adding networks on the docker-compose.yml
nginx:
...
networks:
- your-network-name
mysql:
...
networks:
- your-network-name
wordpress:
...
networks:
- your-network-name
and on the bottom of the file add:
networks:
your-network-name:
driver: bridge
this is the way to configure docker to be connected each other

Can't create database with postgres and docker

I was trying to create a database in my app using command:
php bin/console doctrine:database:create
After that I got an error:
An exception occurred in driver: SQLSTATE[08006] [7] could not connect to server: Connection refused Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?
My docker-compose file looks like that:
version: '3'
services:
php:
container_name: symfony_php
build:
context: ./php
volumes:
- ./symfony/:/var/www/symfony/
depends_on:
- database
networks:
- symfony
database:
container_name: symfony_postgres
image: postgres
restart: always
hostname: symfony_postgres
environment:
POSTGRES_DB: symfony_db
POSTGRES_USER: root
POSTGRES_PASSWORD: root
ports:
- "5432:5432"
volumes:
- ./postgres:/var/lib/postgres
networks:
- symfony
pgadmin:
container_name: symfony_pgadmin
image: dpage/pgadmin4
restart: always
ports:
- "5555:80"
depends_on:
- database
links:
- database
environment:
PGADMIN_DEFAULT_EMAIL: pgadmin4#pgadmin.org
PGADMIN_DEFAULT_PASSWORD: root
networks:
- symfony
nginx:
container_name: symfony_nginx
image: nginx:stable-alpine
build:
context: ./nginx
dockerfile: Dockerfile-nginx
volumes:
- ./symfony:/var/www/symfony
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- database
networks:
- symfony
networks:
symfony:
I'm not using any postgres config file, according to Symfony documentation, there's a config line in .env:
DATABASE_URL="postgresql://root:root#127.0.0.1:5432/symfony_db?serverVersion=11&charset=utf8"
I run netstat command and I got:
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN
I think there's a problem with binding my localhost address where my app's server is running to postgres. Does anyone know, how I can fix it?
In docker-compose all services can be reached by their names.
You need to change DB host address to database - as a service name in docker-compose
DATABASE_URL="postgresql://root:root#database:5432/symfony_db?serverVersion=11&charset=utf8"

How to migrate to db on docker (Symfony)?

version: '3'
services:
db:
image: mysql:8.0.20
command: --default-authentication-plugin=mysql_native_password
volumes:
- "db_app:/var/lib/mysql"
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
ports:
- 3306:3306
networks:
- symfony
php:
build:
context: .
dockerfile: docker/php/Dockerfile
args:
TIMEZONE: ${TIMEZONE}
volumes:
- ./symfony/:/Users/admin/Downloads/symfony-docker-master/symfony
networks:
- symfony
nginx:
build:
context: .
dockerfile: docker/nginx/Dockerfile
volumes:
- ./symfony/:/Users/admin/Downloads/symfony-docker-master/symfony
ports:
- 8050:8050
networks:
- symfony
volumes:
db_app:
networks:
symfony:
The application starts, connects to the database via
DATABASE_URL=mysql://user:123#db:3306/db
But when performing migrations through the console, I get the error
An exception occurred in driver: SQLSTATE[HY000] [2002] php_network_getaddr
esses: getaddrinfo failed: nodename nor servname provided, or not known
As I understand it, due to the fact that the address is specified in the connection db.
How to make migrations?
You need to run console commands from inside of your php container
docker-compose exec php bin/console doctrine:migrations:migrate
You should connect to the container name.
These can be found by running docker container ls on your console.
It's easier when you specify the name in your compose file like so:
services:
db:
container_name: db # This can be anything
image: mysql:8.0.20

Categories