Docker can't connect to MySQL running docker-compose up - mysql

I am using this Docker config: https://github.com/romaricp/kit-starter-symfony-4-docker
I start the environment by using:
docker-compose build
followed by:
docker-compose up -d
Everything is running fine but there is a problem with MySQL service. I get an Symfony error:
"An exception occurred in driver: could not find driver"
as well as PMA error when I try to login to DB:
mysqli_real_connect(): php_network_getaddresses: getaddrinfo failed:
Name does not resolve
Any idea how could I make it work?
docker-compose.yml:
version: '3'
services:
apache:
build: .docker/apache
container_name: sf4_apache
ports:
- 80:80
volumes:
- .docker/config/vhosts:/etc/apache2/sites-enabled
- .:/home/wwwroot/sf4
depends_on:
- php
mysql:
image: mysql
container_name: sf4_mysql
volumes:
- .docker/data/db:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: sf4
MYSQL_USER: sf4
MYSQL_PASSWORD: sf4
php:
build: .docker/php
container_name: sf4_php
volumes:
- .:/home/wwwroot/sf4
environment:
- maildev_host=sf4_maildev
depends_on:
- maildev
- mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: sf4_phpmyadmin
ports:
- 8080:80
links:
- mysql
maildev:
image: djfarrelly/maildev
container_name: sf4_maildev
ports:
- 8001:80
Also I opened the mysql logs and I see this:
2019-04-07T12:00:30.943414Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.15' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
Maybe the port is wrong and that's why I can't connect?

For phpmyadmin service, i think you should set the PMA_HOST and PMA_PORT environment variables like this:
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: sf4_phpmyadmin
environment:
PMA_HOST: mysql
PMA_PORT: 3306
ports:
- 8080:80
links:
- mysql
Your mysql container should have the command instruction at the start to set the authentication plugin (there is an issue with the connectors with mysql 8), more details here
mysql:
image: mysql
container_name: sf4_mysql
command: "--default-authentication-plugin=mysql_native_password"
volumes:
- .docker/data/db:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: sf4
MYSQL_USER: sf4
MYSQL_PASSWORD: sf4
in your Symfony app, the connection string is located in .env file, and it should have the following format :
DATABASE_URL=mysql://mysql_user:mysql_user_password#mysql_host:mysql_port/db_name
mysql_user: your mysql user (ex: root)
mysql_user_password: the user's password (ex: root)
mysql_host: is should contain your mysql container service name
located in your docker-compose.yml file (mysql in this case)
mysql_port: mysql container internal port (3306, in this case)
db_name: the database you want to connect to.
the DATABASE_URL can look like this :
DATABASE_URL=mysql://root:root#mysql:3306/sf4
stop your containers after these changes and start them up again.
Hope this will help.

You have to expose the mysql port 3306:
mysql:
image: mysql
container_name: sf4_mysql
volumes:
- .docker/data/db:/var/lib/mysql
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: sf4
MYSQL_USER: sf4
MYSQL_PASSWORD: sf4
networks:
- default

I think you're having trouble linking the containers. I suggest that you use a custom network instead of linking. That all containers can see each other

Related

Connection refused: MariaDB in Docker

Good morning,
I have 2 docker container configured, with the same Dockerfile, but different ports, see configuration
mariadb:
container_name: mariadb
image: project/mariadb
build:
context: .
dockerfile: ./docker/mariadb/Dockerfile
environment:
MYSQL_ROOT_PASSWORD: "$MYSQL_ROOT_PASSWORD"
MYSQL_DATABASE: "$MYSQL_DATABASE"
MYSQL_USER: "$MYSQL_USER"
MYSQL_PASSWORD: "$MYSQL_PASSWORD"
command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci']
ports:
- "3306:3306"
volumes:
- mysql_data:/var/lib/mysql
- ./services/mysql/utf8mb4.cnf:/etc/mysql/conf.d/utf8mb4.cnf:ro
mariadb_test:
container_name: mariadb_test
image: project/mariadb
build:
context: .
dockerfile: ./docker/mariadb/Dockerfile
environment:
MYSQL_ROOT_PASSWORD: "admin"
MYSQL_DATABASE: project_test
MYSQL_USER: foo
MYSQL_PASSWORD: bar
command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci']
ports:
- "3317:3306"
volumes:
- ./services/mysql/utf8mb4.cnf:/etc/mysql/conf.d/utf8mb4.cnf:ro
I can connect to mariadb (Port:3306) Container without any problems, Parameters are saved in .env:
mysql://$MYSQL_USER:$MYSQL_PASSWORD#$MYSQL_HOST:$MYSQL_PORT/$MYSQL_DATABASE
But if I try to connect to mariadb_test I get a
SQLSTATE[HY000] [2002] Connection refused
even if I hardcode the connection params to:
mysql://foo:bar#mariadb_test:3317/project_test
I'm very frustrated...What am I doing wrong?
Thanks to #David Maze who answered my question in a comment:
Connections between containers always use the "normal" ports; they ignore ports: remappings. Use the standard MySQL port 3306 in the database connection string.

DOCKER and WordPress: Error establishing a database connection

I am new to docker and i have this docker .yml file (below) and i have a local set up of my WordPress site, i have imported a sql dump into a database in the 'container_web'. The problem i am having is that when i try to connect WordPress to the database inside the container i get this error "Error establishing a database connection." For the host i've used localhost, 127.0.0.1, mysql and none of them seems to work.
Do you know or could direct me in the right direction on what i should be checking in order to connect my WordPress install to the database?
services:
web:
build: .
container_name: 'container_web'
dns: '8.8.8.8'
volumes:
- ./:/var/www/html
- ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
depends_on:
- db
ports:
- 80:80
restart: always
environment:
WORDPRESS_ENV: 'development'
db:
container_name: 'container_db'
image: mysql:5.7
ports:
- 3306:3306
volumes:
- ./:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: mydatabase
MYSQL_USER: user
MYSQL_PASSWORD: password
volumes:
mysql-data: {}

can not connect to mysql in docker's bridge network

I created a MySQL container but can only connect to the database from the host.
I start the MySQL container in this file:
docker-composer.yml:
version: '3.3'
services:
db:
image: mysql:5.7
ports:
- "3307:3306"
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
when run docker-compser up , I got this error:
MySQL Connection Error: (2002) No route to host
But I can succeed to connect to the container from the host, like this:
mysql -u root -p -h 127.0.0.1 -P 3307
You can try to connect without a port specifying:
WORDPRESS_DB_HOST: db
It turns out to be a firewall problem. I am using fedora 32 system. And the docker interface using the default zone which will block 80 port and 3306 port.
So, after I bind my docker interface to trusted zone and added some port everything worked.

Docker Compose phpmyadmin not connecting to MySQL

I have a docker compose file which is basically trying to build a WAMP/LAMP style environment.
Basically, PHPMyAdmin can't seem to connect to MySQL. Looking through answers here, it appeared that it was an issue with legacy auth using MySQL Image 8 so I added:
command: --default-authentication-plugin=mysql_native_password
but that didn't work, so I dropped down to mysql image5.7 and the issue is still present. For some reason, I can't connect to MySQL and I get the error:
mysqli_real_connect(): php_network_getaddresses: getaddrinfo failed: Try again
and
mysqli_real_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Try again
Here is a copy of my Docker Compose which I don't think is doing anything weird.
Hoping someone can clarify that for me.
version: "3.1"
services:
site:
build: .
ports :
- "80:80"
volumes:
- ./www:/var/www/html/
links:
- database
networks:
- php-network
#######################################
# PHP MY ADMIN
#######################################
phpmyadmin:
build:
context: .
dockerfile: PHPMYADMIN.Dockerfile
restart: always
links:
- database:mysql
environment:
PMA_HOST: mysql
PMA_PORT: 3306
ports:
- "8080:80"
environment:
- MYSQL_USERNAME=admin
- MYSQL_PASSWORD=root
networks:
- php-network
#######################################
# MySQL server
#######################################
database:
image: mysql:5.7.25
ports:
- "3306:3306"
container_name: db-mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=test_db
- MYSQL_USER=admin
- MYSQL_PASSWORD=root
networks:
- php-network
networks:
php-network:
driver: bridge
I also saw mention of a "depends on" flag which I tried too but that also failed to allow me to connect. I have had the same error throughout.
It's about the container name.
database:
image: mysql:5.7.25
ports:
- "3306:3306"
- **container_name: db-mysql**
and in your phpcontainer:
phpmyadmin:
build:
context: .
dockerfile: PHPMYADMIN.Dockerfile
restart: always
links:
- database:mysql
environment:
**PMA_HOST: mysql**
PMA_PORT: 3306
you define host as mysql, which in docker network will be unrecognizable.
try switching PMA_HOST to db-mysql.
Sorry for bad formatting.
Also use docker ps to see docker container names and to figure out which hosts do you need to connect.
I've got this working now with the following:
version: "3.1"
services:
www:
build: .
ports:
- "8081:80"
volumes:
- ./www:/var/www/html/
links:
- db
networks:
- default
#######################################
# MySQL server
#######################################
db:
image: mysql:5.7.25
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: test_db
MYSQL_ROOT_PASSWORD: test
volumes:
- ./dump:/docker-entrypoint-initdb.d
- persistent:/var/lib/mysql
networks:
- default
#######################################
# PHP MY ADMIN
#######################################
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- db:db
ports:
- 8080:80
environment:
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
persistent:
According to your phpmyadmin service configuration, you are trying to reach mysql container by mysql address:
environment:
PMA_HOST: mysql
PMA_PORT: 3306
But container with mysql server is accessible by database or db-mysql addresses.
So, you need to change phpmyadmin service configuration to:
environment:
PMA_HOST: database
PMA_PORT: 3306

wordpress docker cannot connect to mysql docker

Not sure what I am doing wrong but my wordpress docker just does not connect to the mysql docker. Can someone help me here please?
version: '2'
services:
db:
image: mysql:8.0
container_name: eve_db
volumes:
- ./database/data:/var/lib/mysql
- ./database/initdb.d:/docker-entrypoint-initdb.d
restart: always
environment:
MYSQL_ROOT_PASSWORD: skdjd # any random string will do
MYSQL_DATABASE: djdjd # the name of your mysql database
MYSQL_USER: djdjd # the name of the database user
MYSQL_PASSWORD: djdjd # the password of the mysql user
wordpress:
depends_on:
- db
image: wordpress:php7.2 # we're using the image with php7.1
container_name: eve_de
ports:
- "8080:80"
restart: always
links:
- db:mysql
volumes:
- ./src/wp:/var/www/html
I have also defined
define('DB_HOST', 'db:3306');
in the wp-config.php file. But it did not work.
The main site was having mysql v5.6.38. Changing the same in docker-compose.yml solved the issue.