Running MySql on different port from Docker - mysql

I have a similar issue to Run MySQL on Port 3307 Using Docker Compose but either I can't see the wood for the trees or the solution here is not working.
I have the following docker-compose.yml file:
version: '3'
services:
db:
image: mysql:5.7
container_name: squirrels_db
volumes:
- db_data:/var/lib/docker/volumes/squirrels_db_data/_data
restart: always
ports:
# <Port exposed> : <MySQL Port running inside container>
- 3310:3306
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: mydb_user
MYSQL_PASSWORD: password
volumes:
- ./var/lib/docker/volumes/squirrels_db_data/_data
networks:
internal-net:
ipv4_address: 172.29.0.11
wordpress:
image: wordpress:latest
container_name: squirrels_web
depends_on:
- db
ports:
- 8000:80
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: mydb_user
WORDPRESS_DB_NAME: mydb_name
WORDPRESS_DB_PASSWORD: password
volumes:
- ./data/wp_content:/var/www/html/wp-content
- ./config/wordpress/uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
networks:
nginx-proxy:
internal-net:
ipv4_address: 172.29.0.12
# Names our volume
volumes:
db:
networks:
nginx-proxy:
external:
name: nginx-proxy
internal-net:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.29.0.0/16
Note: I have changed usernames and passwords here and will eventually have them in a .env file
When I hit http://localhost:8000 I am seeing a WordPress delivered "Error establishing a database connection" message and the following in the log (from docker-compose logs):
PHP Warning: mysqli::__construct(): (HY000/2002): Connection refused
in Standard input code on line 22 MySQL Connection Error: (2002)
Connection refused
This now officially driving me nuts, not helped at all by just knowing I am missing something obvious! So, any observations or suggestions gratefully received
Thanks

I managed to get it working. Here's the working file – I'll explain my changes below:
version: "3"
services:
db:
image: mysql:5.7
container_name: squirrels_db
volumes:
# You prepended with db_data: but volume was called db – scroll down to volumes to see my fix
- db_data:/var/lib/docker/volumes/squirrels_db_data/_data
restart: always
ports:
# <Port exposed> : <MySQL Port running inside container>
- 3310:3306
environment:
MYSQL_ROOT_PASSWORD: password
# This is likely the isse - you called it wordpress but tried to connect to mydb_name in the wordpress container
MYSQL_DATABASE: mydb_name
MYSQL_USER: mydb_user
MYSQL_PASSWORD: password
# REMOVE BELOW 2 LINES - you declared above
# volumes:
# - ./var/lib/docker/volumes/squirrels_db_data/_data
networks:
internal-net:
ipv4_address: 172.29.0.11
wordpress:
image: wordpress:latest
container_name: squirrels_web
depends_on:
- db
ports:
- 8000:80
restart: always
environment:
WORDPRESS_DB_HOST: db # db:3306 is fine but that's default so removed
WORDPRESS_DB_USER: mydb_user
WORDPRESS_DB_NAME: mydb_name
WORDPRESS_DB_PASSWORD: password
volumes:
- ./data/wp_content:/var/www/html/wp-content
- ./config/wordpress/uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
networks:
nginx-proxy:
internal-net:
ipv4_address: 172.29.0.12
# Names our volume
volumes:
# FIX: renamed from db to db_data. Added {} to declare an empty volume
db_data: {}
networks:
nginx-proxy:
external:
name: nginx-proxy
internal-net:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.29.0.0/16
Changes
I think your indentation was fine - but I changed this as a first test
You had 2 volumes keys declared in your db container. the first was seemingly correct, the second was missing the db_data: volume prepending the path.
Your volume was named db but was being used (as in point 2) as db_data
Your database names didn't match. You called the db wordpress on setup at MYSQL_DATABASE: wordpress then tried to connect to mydb_name at WORDPRESS_DB_NAME: mydb_name
Simpler solution
Although the above works, do you definitely need the networking? The below will also work and is much simpler:
version: "3"
services:
db:
image: mysql:5.7
container_name: squirrels_db
volumes:
- db_data:/var/lib/docker/volumes/squirrels_db_data/_data
restart: always
ports:
- 3310:3306
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: mydb_name
MYSQL_USER: mydb_user
MYSQL_PASSWORD: password
wordpress:
image: wordpress:latest
container_name: squirrels_web
depends_on:
- db
ports:
- 8000:80
restart: always
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: mydb_user
WORDPRESS_DB_NAME: mydb_name
WORDPRESS_DB_PASSWORD: password
volumes:
- ./data/wp_content:/var/www/html/wp-content
- ./config/wordpress/uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
volumes:
db_data: {}

Related

Docker / phpmyadmin: Unable to connecto

I have the following docker-compose file.
version: "3.7"
services:
main:
container_name: buspack_main
build:
context: .
target: development
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- ${API_PORT}:${API_PORT}
command: bash -c "npm install --save && npm run start:dev"
env_file:
- .env
networks:
- webnet
depends_on:
- db
links:
- db
restart: always
db:
container_name: buspack_db
image: mysql:5.7.33
networks:
- webnet
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: "backendnest"
ports:
- 23306:3306
restart: always
volumes:
- ./db:/var/lib/mysql:rw
phpmyadmin:
container_name: buspack_phpmyadmin
image: phpmyadmin/phpmyadmin
depends_on:
- db
restart: always
ports:
- '8030:80'
environment:
PMA_HOST: buspack_db
MYSQL_ROOT_PASSWORD: 123456
networks:
webnet:
volumes:
db: {}
My problem Is that when I try to login with phpmyadmin I get the following error:
mysqli::real_connect(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution
And also
mysqli::real_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution
Is there something I'm missing?
networks:
- webnet
add webnet network to phpmyadmin service
There are more options to solve the connection.
Add phpmyadmin container to webnet network as Def Soudani suggests. The YML example provided overrides the default network. Since the phpMyAdmin is lacking the network config, it won't be on the webnet docker network by default.
Remove all networks related config from the YML. Since compose already creates a default network (docs) for the containers within one YML file.
Use the PMA_ARBITRARY=1 (docs )which allows you to enter a database server hostname on login form.
Example of my YML setup with custom bridge networking:
version: '3.8'
services:
mysql:
image: mysql:5.7
container_name: myapp-mysql-db
restart: unless-stopped
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
volumes:
- ./database/dbdata:/var/lib/mysql
networks:
- myapp-network
phpmyadmin:
image: phpmyadmin:latest
container_name: phpmyadmin
ports:
# 8080 is the host port and 80 is the docker port
- 8080:80
environment:
PMA_HOST: mysql
UPLOAD_LIMIT: 20M
MYSQL_USERNAME: ${DB_USERNAME}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
restart: unless-stopped
networks:
# define your network where all containers are connected to each other
- myapp-network
networks:
myapp-network:
driver: bridge

Cannot resolve hostname in docker desktop windows

I have setup docker-compose to run phpmyadmin and mysql. Here is my docker-compose.yml
version: "3.9"
networks:
local_web_network:
driver: bridge
volumes:
mysql_data:
driver: local
services:
mysql:
image: mysql:8.0
container_name: local_mysql
restart: always
tty: true
ports:
- "3306:3306"
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_DATABASE: local_master
MYSQL_ROOT_PASSWORD: secret
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
volumes:
- mysql_data:/var/lib/mysql
networks:
- local_web_network
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: local_phpmyadmin
restart: always
tty: true
ports:
- "8083:80"
environment:
PMA_HOST: local_mysql
PMA_ABSOLUTE_URI: http://db.test
MYSQL_ROOT_PASSWORD: secret
networks:
- local_web_network
extra_hosts:
- 'db.test:127.0.0.1'
I have configured the windows hosts file with the below settings
127.0.0.1 db.test
I have successfully run the docker containers without any errors. I have tried accessing the http://127.0.0.1:8083 or http://db.test:8083 it works and displays the phpmyadmin login page.
But the problem here is, i am unable to access the page without specifying the port i.e http://db.test. How do i get this working without specifying the port?

MySQL on Docker “Connection Refused” despite being able to connect via MySQL Workbench

I’m trying to set up a local dev environment for WordPress with MySQL.
After starting the container, the WordPress element works fine and is accessible.
But MySQL throws connection refused despite being able to connect using the same database details in MySQL Workbench.
I have tried changing the host from dB:3306 to 0.0.0.0:3306 but to no avail.
Using Docker for Mac OS.
Dockerfile:
---
version: '3.3'
services:
db:
image: mysql:5.7.30
volumes:
- ./database:/var/lib/mysql
- ./mysqldumps/production-backup.sql:/docker-entrypoint-initdb.d/production.sql
- ./init/migrate.sh:/docker-entrypoint-initdb.d/migrate.sh
restart: unless-stopped
networks:
- wp
ports:
- '3306:3306'
environment:
MYSQL_ROOT_PASSWORD: $db_root_password
MYSQL_DATABASE: $db_name
MYSQL_USER: $db_user
MYSQL_PASSWORD: $db_password
WORDPRESS_TABLE_PREFIX: $db_table_prefix
PRODUCTION_URL: $production_url
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- ./wordpress/wp-content:/var/www/html/wp-content
- ./init/prep.sh:/usr/local/bin/prep.sh
ports:
- '80:80'
restart: unless-stopped
networks:
- wp
environment:
WORDPRESS_DB_HOST: $db_host
WORDPRESS_DB_USER: "root"
WORDPRESS_DB_PASSWORD: "password"
WORDPRESS_TABLE_PREFIX: $db_table_prefix
WORDPRESS_DEBUG: $wp_debug_mode
DISABLED_PLUGINS: $wp_plugins_to_disable
networks:
wp:
volumes:
database:
Does anybody have any ideas what’s going on here?

Docker WordPress image can't connect to database

I have an issue with a docker image.
I have just followed this tutorial to get custom local domains: https://medium.com/#francoisromain/set-a-local-web-development-environment-with-custom-urls-and-https-3fbe91d2eaf0
This seem to work good. But now I can not seem to connect to the Mysql database.
I'm using the WordPress image and my docker-compose.yml file looks like this:
version: '3.3'
services:
db:
image: mysql:5.7
restart: unless-stopped
volumes:
- db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: p4ssw0rd!
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
expose:
- 80
environment:
VIRTUAL_HOST: phpmyadmin.local
VIRTUAL_PORT: 80
PMA_HOST: db
MYSQL_ROOT_PASSWORD: p4ssw0rd!
wordpress:
depends_on:
- db
image: wordpress:latest
restart: unless-stopped
working_dir: /var/www/html
volumes:
- ./wp-content:/var/www/html/wp-content
restart: always
expose:
- 80
environment:
VIRTUAL_HOST: domain.local
VIRTUAL_PORT: 80
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_NAME: wp_name
WORDPRESS_DB_USER: wp_user
WORDPRESS_DB_PASSWORD: p4ssw0rd!
WORDPRESS_TABLE_PREFIX: wp_
WORDPRESS_DEBUG: 1
volumes:
db_data: {}
networks:
default:
external:
name: nginx-proxy
When I start running the container, the error I'm getting is this:
So now I know my WordPress container and proxy are working (domain.local), but the only thing missing is the connection to the database.
I think this has to do with the 'network' and that he simply can't find the database. But I have no idea how to connect it.
Hope one of you guys know what I'm doing wrong.
Thanks in advance.
Okay, so I finally figured it out.
The WORDPRESS_DB_USER should be set to: root
Then everything will work.

Access mysql db inside docker container from outside

I'm learning to use docker to make my development easier but I'm still failing access mysql.
Here is my docker-compose.yaml:
version: '3.3'
services:
# Database
db:
image: mysql:latest
ports:
- '3306:3306'
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: dev1
MYSQL_USER: root
MYSQL_PASSWORD: password
networks:
- wpsite
# Wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- '8000:80'
restart: always
volumes: ['./:/var/www/html']
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: password
WORDPRESS_DB_NAME: dev1
networks:
- wpsite
networks:
wpsite:
volumes:
db_data:
Wordpress is running without difficulties which means that mysql must be alright too. I'm on Linux and trying to connect database via mysql workbench. It appears that connection is also ok expect for, there is no schema and so no wordpress tables.
I tried to add also phpmyadmin into docker-compose.yaml:
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- '8080:80'
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: password
networks:
- wpsite
but here I get following error after attempt to access db:
What I miss?
EDIT:
here is overview of running containers:
So i modified your docker-compose , with 2 small changes , and i dont have a issue .
I created a user for wordpress ( userdev1 ) in mysql .
The root is already here and can have some restrictions for remote access .
Via phpmyadmin i can login with userdev1 or root
You want a network access with the root account you must set this variable
MYSQL_ROOT_HOST .
You can find more information on this page (
https://dev.mysql.com/doc/mysql-installation-excerpt/5.7/en/docker-mysql-more-topics.html#docker_var_mysql-root-host )
version: '3.3'
services:
# Database
db:
image: mysql:5.7
ports:
- '3306:3306'
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: password4root
MYSQL_DATABASE: dev1
MYSQL_USER: userdev1
MYSQL_PASSWORD: password4dev1
networks:
- wpsite
# Wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- '8000:80'
restart: always
volumes: ['./:/var/www/html']
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: userdev1
WORDPRESS_DB_PASSWORD: password4dev1
WORDPRESS_DB_NAME: dev1
networks:
- wpsite
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- '8080:80'
environment:
PMA_HOST: db
networks:
- wpsite
networks:
wpsite:
volumes:
db_data:
** UPDATED **
With the very last version of mysql docker image ( aka mysql 8.0 ),
you must change the default-authentification to mysql_native_password to be comptatible with legacy mysql client
source :
https://dev.mysql.com/doc/mysql-installation-excerpt/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password