Why does mariadb connection fail via docker? - mysql

My docker-compose.yml looks like this:
version: '3'
services:
database:
build:
context: ./database
environment:
- MYSQL_DATABASE=${DATABASE_NAME}
- MYSQL_USER=${DATABASE_USER}
- MYSQL_PASSWORD=${DATABASE_PASSWORD}
- MYSQL_ROOT_PASSWORD=${DATABASE_ROOT_PASSWORD}
ports:
- "3306:3306"
volumes:
- ./database/init.sql:/docker-entrypoint-initdb.d/init.sql
- ./database/data:/var/lib/mysql
php-fpm:
build:
context: ./php-fpm
depends_on:
- database
environment:
- APP_ENV=${APP_ENV}
- APP_SECRET=${APP_SECRET}
- DATABASE_URL=mysql://${DATABASE_USER}:${DATABASE_PASSWORD}#database:3306/${DATABASE_NAME}?serverVersion=5.7
volumes:
- ../src:/var/www
nginx:
build:
context: ./nginx
volumes:
- ../src:/var/www
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/sites/:/etc/nginx/sites-available
- ./nginx/conf.d/:/etc/nginx/conf.d
- ./logs:/var/log
depends_on:
- php-fpm
ports:
- "80:80"
- "443:443"
With this .env file:
DATABASE_NAME=testDB
DATABASE_USER=appuser
DATABASE_PASSWORD=apppassword
DATABASE_ROOT_PASSWORD=password
APP_ENV=dev
APP_SECRET=24e17c47430bd2044a61c131c1cf6990
Result after i ran docker-compose build and docker-compose-up:
Version: '10.5.4-MariaDB-1:10.5.4+maria~focal' socket: '/run/mysqld/mysqld.sock' port: 3306 mariadb.org binary distribution
database_1 | 2020-06-25 17:13:10 3 [Warning] Aborted connection 3 to db: 'unconnected' user: 'unauthenticated' host: 'symfonytestproject_php-fpm_1.symfonytestproject_default' (This connection closed normally without authentication)
And now the console stuck and i have to force a quit.
I tried always everything: removing volumes, rebuild volumes...
Iam very thankfull when anyone can help me with this problem.

Related

MySQL dial tcp 172.30.0.3:3306: connect: connection refused

go web app get mysql dial connect err:
docker-compose.yml
version: '3'
services:
db:
build:
context: ./docs/mysql
environment:
MYSQL_ROOT_PASSWORD: root
container_name: puzzle-mysql
ports:
- 3306:3306
tty: true
restart: always
networks:
- puzzle_network
redis:
image: redis:6.2.4
ports:
- 6379:6379
container_name: puzzle-redis
restart: always
networks:
- puzzle_network
server:
build:
context: .
volumes:
- "./:/workspace"
container_name: puzzle-server
environment:
env: production
ports:
- 8081:8081
tty: true
restart: on-failure
depends_on:
- db
- redis
networks:
- puzzle_network
networks:
puzzle_network:
driver: bridge
docker-compose up -d then all containers is running:
containers running
get server error log:
[2022-05-23 07:38:36.544] production.info bootstrap/db.go:37 mysql connect info: {"info": "root:root#tcp(db:3306)/puzzle?charset=utf8mb4&parseTime=True&loc=Local"}
[2022-05-23 07:38:36.546] production.error bootstrap/db.go:52 Mysql connect failed, err: {"err": "dial tcp 172.30.0.3:3306: connect: connection refused"}
mysql contaienr info
help, how to solution?
solved!
Before running the server, you need to wait for the mysql service to be started.

ERROR 2002 (HY000): Can't connect to MySQL server on 'db' (115)

I am trying to move existing project to Docker. I followed this tutorial at M.Academy and followed the setup instructions from Docker on Existing Project
While executing trying to connect with MySQL I am getting this error ERROR 2002 (HY000): Can't connect to MySQL server on 'db' (115)
I also tried to execute: telnet db 3306
Response:
Trying 172.17.0.1...
telnet: Unable to connect to remote host: Connection timed out
I tried everything but couldn't figure out the problem. I am new to Docker. I am using Ubuntu 18.04 LTE. Haven't changed db.env and got no error on any other step before importing the database.
I further checked and found out that the container is not able to establish connection with MySQL.
P.S. I am successfully able to connect with same MySQL service outside Docker container by using (external MySQL port) mysql -h 127.0.01 -u root -p -P 3306
Steps To Reproduce
Install Docker & Docker Compose on Ubuntu 18.04
Download the Docker Compose template: curl -s https://raw.githubusercontent.com/markshust/docker-magento/master/lib/template | bash
Replace with existing source code of your existing Magento instance: cp -R ~/Sites/existing src
Execute: docker-compose -f docker-compose.yml up -d
Copy files to container: bin/copytocontainer --all
Import existing database: bin/mysql < /var/www/magento243.sql
Expected Result
Database should have been successfully imported
Actual Result
ERROR 2002 (HY000): Can't connect to MySQL server on 'db' (115)
P.S. Issue has already been raised here: https://github.com/markshust/docker-magento/issues/589
docker-compose.yml
version: "3"
services:
app:
image: markoshust/magento-nginx:1.18-5
ports:
- "80:8000"
- "443:8443"
depends_on:
- "db"
volumes: &appvolumes
- ~/.composer:/var/www/.composer:cached
- ~/.ssh/id_rsa:/var/www/.ssh/id_rsa:cached
- ~/.ssh/known_hosts:/var/www/.ssh/known_hosts:cached
- appdata:/var/www/html
- sockdata:/sock
- ssldata:/etc/nginx/certs
extra_hosts: &appextrahosts
## M1 Mac support to fix Docker delay, see #566
- "app:172.17.0.1"
- "phpfpm:172.17.0.1"
- "db:172.17.0.1"
- "redis:172.17.0.1"
- "elasticsearch:172.17.0.1"
- "rabbitmq:172.17.0.1"
## Selenium support, replace "magento.test" with URL of your site
- "magento.test:172.17.0.1"
phpfpm:
image: markoshust/magento-php:7.4-fpm-11
volumes: *appvolumes
extra_hosts: *appextrahosts
env_file: env/phpfpm.env
db:
image: mariadb:10.4
command: --max_allowed_packet=256M
ports:
- "3306:3306"
env_file: env/db.env
volumes:
- dbdata:/var/lib/mysql
extra_hosts: *appextrahosts
redis:
image: redis:5.0-alpine
ports:
- "6379:6379"
extra_hosts: *appextrahosts
elasticsearch:
image: markoshust/magento-elasticsearch:7.9.3-1
ports:
- "9200:9200"
- "9300:9300"
environment:
- "discovery.type=single-node"
## Set custom heap size to avoid memory errors
- "ES_JAVA_OPTS=-Xms1g -Xmx1g"
## Avoid test failures due to small disks
## More info at https://github.com/markshust/docker-magento/issues/488
- "cluster.routing.allocation.disk.threshold_enabled=false"
- "index.blocks.read_only_allow_delete"
extra_hosts: *appextrahosts
rabbitmq:
image: rabbitmq:3.8.22-management-alpine
ports:
- "15672:15672"
- "5672:5672"
volumes:
- rabbitmqdata:/var/lib/rabbitmq
environment:
- RABBITMQ_VM_MEMORY_HIGH_WATERMARK=1GB
extra_hosts: *appextrahosts
mailcatcher:
image: sj26/mailcatcher
ports:
- "1080:1080"
extra_hosts: *appextrahosts
## Selenium support, uncomment to enable
#selenium:
# image: selenium/standalone-chrome-debug:3.8.1
# ports:
# - "5900:5900"
# extra_hosts: *appextrahosts
volumes:
appdata:
dbdata:
rabbitmqdata:
sockdata:
ssldata:
This issue was finally resolved by
Removing extra_hosts entries from the YML file
Adding networks in YML
Final docker-compose.yml
version: "3"
services:
app:
image: markoshust/magento-nginx:1.18-5
ports:
- "81:8000"
- "444:8443"
depends_on:
- "db"
volumes: &appvolumes
- ~/.composer:/var/www/.composer:cached
- ~/.ssh/id_rsa:/var/www/.ssh/id_rsa:cached
- ~/.ssh/known_hosts:/var/www/.ssh/known_hosts:cached
- appdata:/var/www/html
- sockdata:/sock
- ssldata:/etc/nginx/certs
networks:
- customNetwork
phpfpm:
image: markoshust/magento-php:7.4-fpm-11
volumes: *appvolumes
env_file: env/phpfpm.env
networks:
- customNetwork
db:
image: mariadb:10.4
command: --max_allowed_packet=256M
ports:
- "3307:3306"
env_file: env/db.env
volumes:
- dbdata:/var/lib/mysql
networks:
- customNetwork
redis:
image: redis:5.0-alpine
ports:
- "6379:6379"
networks:
- customNetwork
elasticsearch:
image: markoshust/magento-elasticsearch:7.9.3-1
ports:
- "9201:9200"
- "9301:9300"
environment:
- "discovery.type=single-node"
## Set custom heap size to avoid memory errors
- "ES_JAVA_OPTS=-Xms1g -Xmx1g"
## Avoid test failures due to small disks
## More info at https://github.com/markshust/docker-magento/issues/488
- "cluster.routing.allocation.disk.threshold_enabled=false"
- "index.blocks.read_only_allow_delete"
networks:
- customNetwork
rabbitmq:
image: rabbitmq:3.8.22-management-alpine
ports:
- "15672:15672"
- "5672:5672"
volumes:
- rabbitmqdata:/var/lib/rabbitmq
environment:
- RABBITMQ_VM_MEMORY_HIGH_WATERMARK=1GB
networks:
- customNetwork
mailcatcher:
image: sj26/mailcatcher
ports:
- "1080:1080"
networks:
- customNetwork
volumes:
appdata:
dbdata:
rabbitmqdata:
sockdata:
ssldata:
networks:
customNetwork:

use client to connect to mysql inside a docker network

I'm using docker swarm to create a small infrastructure with different wordpress instances, I have an ingress (nginx) and different stacks (wp,mysql,wp). The docker-compose for each stack is like this:
version: "3.7"
networks:
foo-frontend:
foo-backend:
volumes:
db_data:
wordpress_data:
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
env_file:
- db.env
networks:
- foo-backend
ports:
- "3306:3306"
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- wordpress_data:/var/www/html/wp-content
env_file:
- wp.env
networks:
- foo-frontend
- foo-backend
nginx:
depends_on:
- wordpress
image: nginx:latest
volumes:
- ./nginx:/etc/nginx/conf.d
- /etc/letsencrypt:/etc/letsencrypt
networks:
- foo-frontend
while the ingress is:
version: "3.7"
services:
nginx:
image: nginx:latest
ports:
- 80:80
- 443:443
volumes:
- ~/docker-vps/ingress/conf.d:/etc/nginx/conf.d
- /etc/letsencrypt:/etc/letsencrypt
networks:
- foo-frontend
networks:
frontend:
external: true
my problem is that I'm not able to connect to mysql with my client, I tried to use the container ip with the right port but is not working....is there a way to connect to mysql with a client?
many thanks
Consider as hostname the name of service. This is how services communicate. So, the hostname of database is db. Let me know the error message that occurs, because I don't have rating to comment your original question to know this.

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 not work with docker compose

This is my docker-compose.yml
version: '2'
services:
wordpress:
image: wordpress:4.6.1-php5.6-apache
container_name: wordpress
volumes:
- ./projects/:/home/docker/
working_dir: /home/docker/
ports:
- "8000:80"
environment:
WORDPRESS_DB_PASSWORD: secret
links:
- database-mysql
database-mysql:
image: mysql:5.7
container_name: mysqldb
ports:
- "3306:3306"
volumes:
- ./backups/mysqldb/:/var/lib/mysql/
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: wordpress
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
container_name: phpmyadmin
ports:
- "8080:80"
environment:
PMA_USER: root
PMA_PASSWORD: secret
PMA_HOST: database-mysql
links:
- database-mysql
When I run: docker-compose up, the log error shows:
MySQL Connection Error: (2002) php_network_getaddresses: getaddrinfo failed: Name or service not know
Warning: mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed: Name or service not known in - on line 19
Warning: mysqli::mysqli(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name or service not known in - on line 19
2016-11-11 04:14:33,648 INFO success: php-fpm entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2016-11-11 04:14:33,648 INFO success: nginx entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
What am I doing wrong?
is's too long to comment, therefore i create another answer
try this and wait for 2 minutes, after that access localhost:8000
version: '2'
services:
db:
image: mysql:5.7
volumes:
- "./.data/db:/var/lib/mysql"
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
links:
- db
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_PASSWORD: wordpress
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
container_name: phpmyadmin
ports:
- "8080:80"
environment:
PMA_USER: root
PMA_PASSWORD: secret
PMA_HOST: database-mysql
links:
- db
On Windows 10 Home edition (so using docker-toolbox) I also ran into this problem.
It seems that the wordpress image now required the environment setting:
WORDPRESS_DB_HOST: db:3306.
After adding this, the wordpress container can connect to the database.
Also with Windows 10 Home edition I can't get the port mapping to work for localhost. I need to lookup the ip-adress via docker-machine ip and connect to the port there.
On Linux Mint (based on Ubuntu) this does not seem required.
So I expect this to be an installation problem with docker-toolbox or Windows 10 Home Edition.