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
Related
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
i install wordpress with docker ,and mysql connected successfully,but i can't find my wordpress mysql data . i use mysql client to connet mysql server docker ,i can't find data.
version: "3.9"
services:
db:
container_name: mysql-bbpress-compose
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: 1234567
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
container_name: wrodpress-bbpress-compose
depends_on:
- db
image: wordpress:latest
volumes:
- wordpress_data:/var/www/html
ports:
- "1080:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
wordpress_data: {}
enter image description here
I can't fidn DB wordpress
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
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?
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: {}