multiple mysql containers only one is working - mysql

What I am doing wrong? Only one is working, I am using phpmyadmin for accesing it, and in the log files I got this in all the others containers:
2021-02-19 16:13:24+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.33-1debian10 started.
2021-02-19 16:13:24+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2021-02-19 16:13:24+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.33-1debian10 started.
And phpmyadmin says mysqli::real_connect(): (HY000/2002): Connection refused (only mysql1 is connection)
This is my docker-compose
version: '3'
services:
mysql1:
image: mysql:5.7.33
container_name: mysql1
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: db1
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: db1
MYSQL_PASSWORD: db1
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- mysql1:/var/lib/mysql/
- ./etc/mysql/my1.cnf:/etc/mysql/my.cnf
networks:
- app-network
mysql2:
image: mysql:5.7.33
container_name: mysql2
restart: unless-stopped
tty: true
ports:
- "3307:3306"
environment:
MYSQL_DATABASE: db2
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: db2
MYSQL_PASSWORD: db2
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- mysql2:/var/lib/mysql/
- ./var/log/mysql:/var/log/mysql
- ./etc/mysql/my2.cnf:/etc/mysql/my.cnf
- ./var/transit/:/var/transit
networks:
- app-network
mysql3:
image: mysql:5.7.33
container_name: mysql3
restart: unless-stopped
tty: true
ports:
- "3308:3306"
environment:
MYSQL_DATABASE: db3
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: db3
MYSQL_PASSWORD: db3
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- mysql3:/var/lib/mysql/
- ./var/log/mysql:/var/log/mysql
- ./etc/mysql/my3.cnf:/etc/mysql/my.cnf
- ./var/transit/:/var/transit
networks:
- app-network
mysql4:
image: mysql:5.7.33
container_name: mysql4
restart: unless-stopped
tty: true
ports:
- "3309:3306"
environment:
MYSQL_DATABASE: db4
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: db4
MYSQL_PASSWORD: db4
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- mysql4:/var/lib/mysql/
- ./etc/mysql/my-4.cnf:/etc/mysql/my.cnf
networks:
- app-network
# PHPMYADMIN
phpmyadmin:
image: phpmyadmin
container_name: phpmyadmin
environment:
PMA_USER: root
PMA_PASSWORD: root
PMA_HOSTS: mysql1,mysql2,mysql3,mysql4
PMA_PORTS: 3306,3307,3308,3309
links:
- mysql1
- mysql2
- mysql3
- mysql4
restart: always
ports:
- 8080:80
volumes:
- phpmyadmin:/sessions
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
phpmyadmin:
driver: local
studioweb:
driver: local
sessions:
driver: local
mysql1:
driver: local
mysql2:
driver: local
mysql3:
driver: local
mysql4:
driver: local

They all use same network and port is used. It does not corrupt or show an error but not working. I added MYSQL_TCP_PORT to each service to work on different port(3307 not included) and it is working fine.
version: '3'
services:
mysql1:
image: mysql:5.7.33
container_name: mysql1
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: db1
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: db1
MYSQL_PASSWORD: db1
SERVICE_TAGS: dev
SERVICE_NAME: mysql
MYSQL_TCP_PORT: 3306
volumes:
- mysql1:/var/lib/mysql/
- ./etc/mysql/my1.cnf:/etc/mysql/my.cnf
networks:
- app-network
mysql2:
image: mysql:5.7.33
container_name: mysql2
restart: unless-stopped
tty: true
ports:
- "3310:3310"
environment:
MYSQL_DATABASE: db2
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: db2
MYSQL_PASSWORD: db2
SERVICE_TAGS: dev
SERVICE_NAME: mysql
MYSQL_TCP_PORT: 3310
volumes:
- mysql2:/var/lib/mysql/
- ./var/log/mysql:/var/log/mysql
- ./etc/mysql/my2.cnf:/etc/mysql/my.cnf
- ./var/transit/:/var/transit
networks:
- app-network
mysql3:
image: mysql:5.7.33
container_name: mysql3
restart: unless-stopped
tty: true
ports:
- "3308:3308"
environment:
MYSQL_DATABASE: db3
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: db3
MYSQL_PASSWORD: db3
SERVICE_TAGS: dev
SERVICE_NAME: mysql
MYSQL_TCP_PORT: 3308
volumes:
- mysql3:/var/lib/mysql/
- ./var/log/mysql:/var/log/mysql
- ./etc/mysql/my3.cnf:/etc/mysql/my.cnf
- ./var/transit/:/var/transit
networks:
- app-network
mysql4:
image: mysql:5.7.33
container_name: mysql4
restart: unless-stopped
tty: true
ports:
- "3309:3309"
environment:
MYSQL_DATABASE: db4
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: db4
MYSQL_PASSWORD: db4
SERVICE_TAGS: dev
SERVICE_NAME: mysql
MYSQL_TCP_PORT: 3309
volumes:
- mysql4:/var/lib/mysql/
- ./etc/mysql/my-4.cnf:/etc/mysql/my.cnf
networks:
- app-network
# PHPMYADMIN
phpmyadmin:
image: phpmyadmin
container_name: phpmyadmin
environment:
PMA_USER: root
PMA_PASSWORD: root
PMA_HOSTS: mysql1,mysql2,mysql3,mysql4
PMA_PORTS: 3306,3310,3308,3309
links:
- mysql1
- mysql2
- mysql3
- mysql4
restart: always
ports:
- 8080:80
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
phpmyadmin:
driver: local
studioweb:
driver: local
sessions:
driver: local
mysql1:
driver: local
mysql2:
driver: local
mysql3:
driver: local
mysql4:
driver: local

Related

Is it possible to have multiple default mysql servers in phpMyAdmin using docker

I'm trying to build enviroment using Docker with two MySQL servers, and one phpMyAdmin server to easily analyze data during development.
My docker-compose.yml looks like this.
version: "3.9"
services:
database1:
container_name: Database1
image: mysql:8.0.28
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_DATABASE: database1
MYSQL_ROOT_PASSWORD: somepassword
MYSQL_USER: databaseuser1
MYSQL_PASSWORD: databaseuser1
ports:
- "3306:3306"
volumes:
- database-volume:/var/lib/mysql
networks:
- my_network
database2:
container_name: Database2
image: mysql:8.0.28
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_DATABASE: database2
MYSQL_ROOT_PASSWORD: somepassword
MYSQL_USER: databaseuser2
MYSQL_PASSWORD: databaseuser2
ports:
- "3307:3306"
volumes:
- second-database-volume:/var/lib/mysql
networks:
- my_network
phpmyadmin:
container_name: PhpMyAdmin
image: phpmyadmin/phpmyadmin:5.1.3
restart: always
depends_on:
- database1
- database2
environment:
PMA_HOST: database1, database2
PMA_PORT: 3306, 3307
PMA_USER: databaseuser1, databaseuser2
PMA_PASSWORD: databaseuser1, databaseuser2
ports:
- "8080:80"
networks:
- my_network
volumes:
database-volume:
second-database-volume:
networks:
my_network:
driver: bridge
I was wondering is there any build in way to archive this.
Did you been tried to change the environment variable PMA_HOST to PMA_HOSTS?
Official phpMyAdmin Docker image

Docker-compose database table reset

I am new to docker technology. I use docker-compose in my project and every time I start docker-compose my database tables are reset.
docker-compose.yaml
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: ./docker/php/Dockerfile
image: php-7.4.1-extended
container_name: php
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: nginx
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./docker/nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#MySQL Service
db:
image: mysql:5.7.22
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: root
MYSQL_USER: root
MYSQL_PASSWORD: root
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./docker/mysql/my.cnf:/etc/mysql/my.cnf
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
I found an example of docker-compose.yaml file from some sources and made changes on it. What should I do to fix the problem.
Your docker image is storing its data within the image, which isn't persisted. You need to tell Docker to store the data on a volume, instead. This is a bit version-dependent, but something like
#MySQL Service
db:
image: mysql:5.7.22
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: root
MYSQL_USER: root
MYSQL_PASSWORD: root
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./docker/mysql/my.cnf:/etc/mysql/my.cnf
- "./.mysql-data/db:/var/lib/mysql"
And then delete
dbdata:
driver: local

phpmyadmin in docker-compose

I dockerized a Laravel project and I want to run PhpMyAdmin while running beside MySQL.
My problem is I can't login PhpMyAdmin in PhpMyAdmin page.
Here is the error: mysqli::real_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known.
This is the content of my docker-compose.yml file:
version: '3'
networks:
laravel:
services:
mysql:
image: mysql:5.7.22
container_name: mysql
tty: true
restart: unless-stopped
ports:
- "4506:3306"
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: homestead
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
MYSQL_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
phpmyadmin:
image: phpmyadmin/phpmyadmin
depends_on:
- mysql
ports:
- 8181:80
environment:
MYSQL_USERNAME: homestead
MYSQL_ROOT_PASSWORD: secret
networks:
- laravel
What am I missing?
You need to specify the PMA_HOST environment variable to tell phpmyadmin where to find the mysql server (in this case, the hostname is simply the service name, mysql):
version: '3'
networks:
laravel:
services:
mysql:
image: mysql:5.7.22
container_name: mysql
tty: true
restart: unless-stopped
ports:
- "4506:3306"
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: homestead
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
MYSQL_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
phpmyadmin:
image: phpmyadmin/phpmyadmin
depends_on:
- mysql
ports:
- 8181:80
environment:
PMA_HOST: mysql
MYSQL_USERNAME: homestead
MYSQL_ROOT_PASSWORD: secret
networks:
- laravel

Docker-compose file with multiple database containers in Laravel

I have a Laravel project with two mysql database containers. During seeding we're fetching data from database one and mapping/importing this into database two.
docker-compose.yml
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: digitalocean.com/php
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
ports:
- "9000:9000"
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#MySQL Service
db:
image: mysql:5.7.29
container_name: db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: aas
MYSQL_USER: aas_admin
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- dbdata:/var/lib/mysql/
- ./mysql/my.cnf:/etc/mysql/my.cnf
networks:
- app-network
access_db:
image: mysql:5.7.29
container_name: access_db
restart: unless-stopped
tty: true
ports:
- "3307:3306"
environment:
MYSQL_DATABASE: access_aas
MYSQL_USER: aas_admin
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- accessdbdata:/var/lib/mysql/
- ./mysql/my.cnf:/etc/mysql/accessmy.cnf
networks:
- app-network
#Redis
cache:
image: redis:rc-alpine3.11
container_name: cache
ports:
- "6382:6379"
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
accessdbdata:
driver: local
.env
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=aas
DB_USERNAME=aas_admin
DB_PASSWORD=secret
ACCESS_DB_CONNECTION=access
ACCESS_DB_HOST=access_db
ACCESS_DB_PORT=3307
ACCESS_DB_DATABASE=access_aas
I can access the database through SequelPro using the above credentials for both databases. I can also create migrations but seeding seems to be a problem when I query data from the access_db
When I try seed the database I get the following error:
Turns out in the .env file I needed to change:
.env
ACCESS_DB_PORT=3307 => ACCESS_DB_PORT=3306
I believe the reason for this is that Laravel is looking for a container inside the host in which case port 3307 doesn't exist.

Connecting separate WordPress Docker-Compose Stacks to an external MySQL and phpMyAdmin Containers

How to achieve the following with docker-compose:
Have a separate container for each WordPress environment.
A single container dedicated for MySQL and phpMyAdmin.
Setting up MySQL and phpMyAdmin:
version: '3.7'
services:
#Database
db:
image: mysql:latest
volumes:
- db_data:/var/lib/mysql
ports:
- "1207:3306"
restart: always
container_name: my_wordpress_db
environment:
MYSQL_ROOT_PASSWORD: password
#phpMyAdmin
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
container_name: my_wordpress_phpmyadmin
ports:
- "8080:80"
environment:
PMA_HOST: db:3306
MYSQL_ROOT_PASSWORD: password
volumes:
db_data:
networks:
default:
external:
name: my_wordpress_network
Setting up the WordPress docker environment:
version: '3.7'
services:
#Wordpress
wordpress:
image: wordpress:latest
external_links:
- my_wordpress_db:db
volumes: ['./:/var/www/html']
ports:
- "33003:80"
restart: always
container_name: my_project_container
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: password
WORDPRESS_DB_NAME: my_project_db
networks:
default:
external:
name: my_wordpress_network