use client to connect to mysql inside a docker network - mysql

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.

Related

Connect MySQL container volume to existing data

I'm trying to dockerize our website and I need to be able to connect the MySQL container to an existing database. I don't want to link my app container to a local DB but would rather use a volume to bring the data into the MySQL container. If what I'm trying to do makes sense could someone please help me. Below is my docker-compose file.
version: "3.9"
services:
app:
build:
context: .
dockerfile: dockerfiles/app/Dockerfile
depends_on:
- mysql
container_name: pedestalpro_app
volumes:
- ./:/var/www
- ./dockerfiles/php/local.ini:/usr/local/etc/php/conf.d/local.ini
env_file:
- .env
tty: true
nginx:
image: nginx:alpine
depends_on:
- app
container_name: pedestalpro_nginx
volumes:
- ./public/:/var/www/public
- ./dockerfiles/nginx/conf.d/:/etc/nginx/conf.d/
ports:
- "80:80"
- "443:443"
tty: true
mysql:
image: mysql:5.7.22
container_name: pedestalpro_mysql
volumes:
- pedestalpro_db:/var/lib/mysql
- ./dockerfiles/mysql/my.cnf:/etc/mysql/my.cnf
ports:
- "3307:3306"
env_file:
- .env.mysql
volumes:
pedestalpro_db:

mysql docker cannot connect from outside container

I've created a docker-compose file for PHP dev but I cannot connect to the DB with sequel pro neither from the APP.
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
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
ports:
- "80:80"
- "443:443"
networks:
- app-network
#MySQL Service
db:
image: mysql:latest
container_name: db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
expose:
# Opens port 3306 on the container
- '3306'
environment:
MYSQL_DATABASE: laravel
MYSQL_USER: laraveluser
MYSQL_PASSWORD: root
MYSQL_ROOT_PASSWORD: root
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./mysql/data:/var/lib/mysql
- ./mysql/my.cnf:/etc/my.cnf
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
everything works OK and no errors are displayed but when I'm trying to connect to the DB in any way it doesn't work docker-compose exec app PHP artisan migrate nor with sequel Pro, what's wrong with my config?
The only way to access the DB is with docker-compose exec db bash
here the full repo
straight from the GitHub repo I have the answer, though I haven't tried yet as I think I'm going to stick with 5.7 for now.
You might need to add --default-auth=mysql_native_password to your command
Mysql:8 uses caching_sha2_password by default https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password
#454 (comment)
After digging into your code, I found that you need to update .env.example file
Replace DB_HOST=db on line 10.
You must have missed it, but it is already mentioned in the post that you had referred, under the heading "Step 8 — Modifying Environment Settings and Running the Containers".

flask can not connect mysql in docker compose

flask can not connect mysql in docker compose
in connect string, I already use db service name as host, still can't connect db
My docker-compose.yml:
services:
api:
build:
context: ./api
dockerfile: Dockerfile
volumes:
- './api:/usr/src/app'
ports:
- 5002:5000
environment:
- FLASK_CONFIG=development
- FLASK_ENV=development
- APP_SETTINGS=project.config.DevelopmentConfig
- DATABASE_URL=mysql+pymysql://root:xxxxx#mysql-db:3307/gaojiesi
- SECRET_KEY=ZQbn05PDeA7v11
depends_on:
- mysql-db
links:
- mysql-db
restart: unless-stopped
mysql-db:
image: mysql:8.0
container_name: mysql-db
command: --default-authentication-plugin=mysql_native_password
environment:
- MYSQL_ROOT_PASSWORD=frikyalong
ports:
- "3307:3306"
volumes:
- ./db/mysql/conf/my.cnf:/etc/my.cnf.d/
- ./db/mysql/logs:/logs
- ./db/mysql/data:/var/lib/mysql
you need to change :
DATABASE_URL=mysql+pymysql://root:xxxxx#mysql-db:3307/gaojiesi
to :
DATABASE_URL=mysql+pymysql://root:xxxxx#mysql-db:3306/gaojiesi
port 3307 in not reachable from inside the container

Docker-Compose Unable to connect to any of the specified MySQL hosts

i have an existing Project ( API - portal - Mysql )
i have used Docker-compose without dockerfile
i publish the API - Portal and put them all in folder and then Docker-compose up
i can reach the api by getting the local values in it
but if i tried to reach mysql trough the API using postman its not working even when i open the frontend website
ConnectionString:
"ConnectionString": "server=xmysql;port=4406;Database=sbs_hani;User ID=hani;Password=123456; persistsecurityinfo=True;Charset=utf8; TreatTinyAsBoolean=false;"
This is my docker-compose file :
version: '3'
services:
xmysql:
container_name: xmysql
hostname: xmysql
image: mysql:latest
restart: always
environment:
MYSQL_ROOT_PASSWORD: "123456"
MYSQL_DATABASE: "sbs_hani"
MYSQL_USER: "hani"
MYSQL_PASSWORD: "123456"
ports:
- "3306:4406"
networks:
- xnetwork
volumes:
- data-volume:/var/lib/mysql
- ./hanimysql/sbs_hani.sql:/docker-entrypoint-initdb.d/sbs_hani.sql
xapi:
container_name: xapi
hostname: xapi
image: microsoft/dotnet:latest
# restart: always
tty: true
command: ["dotnet", "/var/lib/volhaniapi/hani.APIs.dll"]
ports:
- "8081:80"
- "8444:443"
networks:
- xnetwork
links:
- xmysql:xmysql
depends_on:
- xmysql
volumes:
- ./haniapi/:/var/lib/volhaniapi/
xportal:
container_name: xportal
hostname: xportal
image: microsoft/dotnet:latest
# restart: always
tty: true
command: ["dotnet", "/var/lib/volhaniportal/hani.Portal.dll"]
ports:
- "8083:80"
- "8446:443"
networks:
- xnetwork
links:
- xmysql:xmysql
depends_on:
- xmysql
volumes:
- ./haniportal/:/var/lib/volhaniportal/
xfront:
container_name: xfront
hostname: xfront
image: nginx:stable-alpine
# restart: always
ports:
- "8082:80"
- "4445:443"
networks:
- xnetwork
links:
- xapi:xapi
depends_on:
- xapi
volumes:
- ./hanifront/:/usr/share/nginx/html
volumes:
data-volume: {}
# xvolmysql:
# driver: "local"
# xvolmongo:
# driver: "local"
# xvolrabbitmq:
# driver: "local"
# xvolstarapi:
# driver: "local"
networks:
xnetwork:
driver: bridge
When you make a connection from one Docker container to another, you always connect to the port the service inside the container is actually listening on. Any ports: mappings are ignored (and in fact you don't need ports: if you don't want the service to be accessible from outside Docker container space).
In your example, you need to change the port number in the connection string to the default MySQL port 3306.
(Consider removing all of the container_name:, hostname:, networks:, and links: blocks in the file. You should have an equivalent container stack with the same functionality; the two most observable differences are that if you directly use docker commands then the container names will be prefixed with the directory names, and the Docker-internal network will be named default. You can still use the service block names like xmysql as host names.)

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