How to establish database connection from wordpress docker - mysql

I try running a docker compose wordpress by using this guide: https://docs.docker.com/compose/wordpress/
This is the yaml file as described in the guide:
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/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
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DEBUG: "true"
volumes:
db_data: {}
After I run my
"docker-compose up -d"
command, I go to "http://localhost:8000/" in my browser and get the white page with "Error establishing a database connection". According to the guide, wordpress should show me the 5 minute Installation already at this point. When I run the container with wordpress debug true, this error message is shown then:
Warning: mysqli_real_connect(): (HY000/2002): Connection refused in /var/www/html/wp-includes/wp-db.php on line 1612
Connection refused
I now use
docker exec it container_id /bin/bash
and type "mysql -p". Now I use the MYSQL_ROOT_PASSWORD from the docker compose file but I get access denied ("Access denied for user 'root'#'localhost' (using password: YES)")
I am not sure what I did earlier, but at some point it worked and I listed the databases and the mysql.users and the db and user were there.
So I dont even know, what the problem here is...
And why can I not access as root anymore? Does anyone know what to do?
EDIT: changed port back to 3306, I tried 3308 just to see if that may be a port issue

I found another post and they used this yaml. Still not sure why this works, but it does.
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/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
ports:
- 8000:80
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
volumes:
db_data: {}

Check the logs of a service. To check the services, do:
$ docker-compose logs
...
db_1 | 2022-10-21 2:08:08 0 [Note] InnoDB: Buffer pool(s) load completed at 221021 2:08:08
db_1 | 2022-10-21 2:08:08 0 [Note] mysqld: ready for connections.
db_1 | Version: '10.6.4-MariaDB-1:10.6.4+maria~focal' socket: '/run/mysqld/mysqld.sock' port: 3306 mariadb.org binary distribution
For example, If you have a mariadb service named db and it doesn't print that way, you might have to wait for it to print.

Related

can not connect to mysql in docker's bridge network

I created a MySQL container but can only connect to the database from the host.
I start the MySQL container in this file:
docker-composer.yml:
version: '3.3'
services:
db:
image: mysql:5.7
ports:
- "3307:3306"
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
when run docker-compser up , I got this error:
MySQL Connection Error: (2002) No route to host
But I can succeed to connect to the container from the host, like this:
mysql -u root -p -h 127.0.0.1 -P 3307
You can try to connect without a port specifying:
WORDPRESS_DB_HOST: db
It turns out to be a firewall problem. I am using fedora 32 system. And the docker interface using the default zone which will block 80 port and 3306 port.
So, after I bind my docker interface to trusted zone and added some port everything worked.

docker mysql wordpress port doesn't connect

I downloaded the mysql and wordpress images. Mysql ports are
3306 localhost:32781
33060 localhost:32780
Wordpress configuration is
WORDPRESS_DB_HOST 192.168.99.100:32774
MYSQL_ROOT_PASSWORD and WORDPRESS_DB_PASSWORD are the same
I try to connect to wordpress with
http://192.168.99.100:32774/
I get the message
This site can’t be reached
How do I have to configure the ports of mysql and wordpress?
CONFIGURATION MYSQL
WORDPRESS
Error trace
From what you can find on the docker configuration page, you should take this example and modify it to your needs.
There is the following docker-compose file that will launch a wordpress in a minute:
https://docs.docker.com/compose/wordpress/
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
From that file you have various options like, using docker compose tool (https://docs.docker.com/compose), or if you have a swarm running you could use docker stack command(https://vsupalov.com/difference-docker-compose-and-docker-stack/) or you can divide the configuration of both elements and create separate Dockerfile's(the configuration of a Docker file differs from what you can see on docker-compose so take the information an create your own) and launch them separated, you should launch mysql first as wordpress depends on a bbdd running first.
The easiest way to implement this is using docker-compose. Here is an example:
version: '3.2'
services:
database:
image: mysql:5.7
volumes:
- my_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: root_password
MYSQL_DATABASE: wordpress
MYSQL_USER: wp_user
MYSQL_PASSWORD: password
wordpress:
depends_on:
- database
image: wordpress:php7.3-apache
ports:
- '8000:80'
restart: always
environment:
WORDPRESS_DB_HOST: database:3306
WORDPRESS_DB_USER: wp_user
WORDPRESS_DB_PASSWORD: password
working_dir: /var/www/html
volumes:
my_data: {}
A few notes: the database doesn't mount any port on host because it doesn't need to. If you don't want to use docker-compose you can run docker run commands for this but then you have to create your own network for the containers and attach them to it.
Wordpress will be available on http://localhost:8000.
WORDPRESS_DB_HOST is the connection to the database and you won't be able to access that through http anyway.
Hope this helps you.

Docker can't connect to MySQL running docker-compose up

I am using this Docker config: https://github.com/romaricp/kit-starter-symfony-4-docker
I start the environment by using:
docker-compose build
followed by:
docker-compose up -d
Everything is running fine but there is a problem with MySQL service. I get an Symfony error:
"An exception occurred in driver: could not find driver"
as well as PMA error when I try to login to DB:
mysqli_real_connect(): php_network_getaddresses: getaddrinfo failed:
Name does not resolve
Any idea how could I make it work?
docker-compose.yml:
version: '3'
services:
apache:
build: .docker/apache
container_name: sf4_apache
ports:
- 80:80
volumes:
- .docker/config/vhosts:/etc/apache2/sites-enabled
- .:/home/wwwroot/sf4
depends_on:
- php
mysql:
image: mysql
container_name: sf4_mysql
volumes:
- .docker/data/db:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: sf4
MYSQL_USER: sf4
MYSQL_PASSWORD: sf4
php:
build: .docker/php
container_name: sf4_php
volumes:
- .:/home/wwwroot/sf4
environment:
- maildev_host=sf4_maildev
depends_on:
- maildev
- mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: sf4_phpmyadmin
ports:
- 8080:80
links:
- mysql
maildev:
image: djfarrelly/maildev
container_name: sf4_maildev
ports:
- 8001:80
Also I opened the mysql logs and I see this:
2019-04-07T12:00:30.943414Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.15' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
Maybe the port is wrong and that's why I can't connect?
For phpmyadmin service, i think you should set the PMA_HOST and PMA_PORT environment variables like this:
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: sf4_phpmyadmin
environment:
PMA_HOST: mysql
PMA_PORT: 3306
ports:
- 8080:80
links:
- mysql
Your mysql container should have the command instruction at the start to set the authentication plugin (there is an issue with the connectors with mysql 8), more details here
mysql:
image: mysql
container_name: sf4_mysql
command: "--default-authentication-plugin=mysql_native_password"
volumes:
- .docker/data/db:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: sf4
MYSQL_USER: sf4
MYSQL_PASSWORD: sf4
in your Symfony app, the connection string is located in .env file, and it should have the following format :
DATABASE_URL=mysql://mysql_user:mysql_user_password#mysql_host:mysql_port/db_name
mysql_user: your mysql user (ex: root)
mysql_user_password: the user's password (ex: root)
mysql_host: is should contain your mysql container service name
located in your docker-compose.yml file (mysql in this case)
mysql_port: mysql container internal port (3306, in this case)
db_name: the database you want to connect to.
the DATABASE_URL can look like this :
DATABASE_URL=mysql://root:root#mysql:3306/sf4
stop your containers after these changes and start them up again.
Hope this will help.
You have to expose the mysql port 3306:
mysql:
image: mysql
container_name: sf4_mysql
volumes:
- .docker/data/db:/var/lib/mysql
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: sf4
MYSQL_USER: sf4
MYSQL_PASSWORD: sf4
networks:
- default
I think you're having trouble linking the containers. I suggest that you use a custom network instead of linking. That all containers can see each other

Unknown authentication method error while using wordpress and mysql docker images

I am trying to install wordpress with mysql via docker but I have communication errors between the container of the DB and the wordpress container.
here is my docker-compose.yml file
version: '3'
services:
db:
image: mysql:latest
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
links:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
volumes:
db_data:
If I execute the "docker-compose up -d" command and that I open the logs of the wordpress container I have this error:
...
wordpress_1 | Warning: mysqli::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] in Standard input code on line 22
wordpress_1 |
wordpress_1 | Warning: mysqli::__construct(): (HY000/2054): The server requested authentication method unknown to the client in Standard input code on line 22
wordpress_1 |
wordpress_1 | MySQL Connection Error: (2054) The server requested authentication method unknown to the client
...
I added command: '--default-authentication plugin=mysql_native_password' in the db section and i changed the mysql version to 5.7 but it did not help me solve the problem.
I also visited these discussions :
Wordpress on docker-compose no run
https://serverfault.com/questions/880773/unable-to-access-wordpress-site-created-as-a-docker-stack/880777#880777
https://github.com/docker-library/wordpress/issues/313
Thank you in advance for your proposals.
It seems that WordPress/PHP doesn't support MySQL v8 yet. Even wordpress docker image readme suggests to use MySQL v5.7. After changing mysql version to v5.7, you might notice that MySQL container crashes with an error similar to below:
...
db_1 | 2019-01-28T18:45:24.611045Z 0 [ERROR] [FATAL] InnoDB: Table flags are 0 in the data dictionary but the flags in file ./ibdata1 are 0x4800!
db_1 | 2019-01-28 18:45:24 0x7f00e013a740 InnoDB: Assertion failure in thread 139641736111936 in file ut0ut.cc line 942
db_1 | InnoDB: We intentionally generate a memory trap.
...
To fix this, easiest way is to delete the MySQL docker volume using docker-compose down -v. After that, docker-compose up -d should work.
If that still doesn't work, use the docker-compose example from https://hub.docker.com/_/wordpress/. Adding it here in case it gets pulled down in future.
version: '3.1'
services:
wordpress:
image: wordpress
restart: always
ports:
- 8080:80
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: exampleuser
WORDPRESS_DB_PASSWORD: examplepass
WORDPRESS_DB_NAME: exampledb
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: exampledb
MYSQL_USER: exampleuser
MYSQL_PASSWORD: examplepass
MYSQL_RANDOM_ROOT_PASSWORD: '1'

docker-compose mysql container denies access to wordpress container

I have a problem with mysql 5.7 container denying access to wordpress container. I'm using docker-compose and I'm running docker on Mac OSX. Docker should be on latest version available.
Here's my docker-compose.yml
version: '2'
services:
wordpress:
depends_on:
- db
image: wordpress:latest
container_name: wordpress
ports:
- "8000:80"
- "443:443"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_NAME: blog
WORDPRESS_DB_USER: blog_admin
WORDPRESS_DB_PASSWORD: userpasswd
networks:
- wordpress_net
db:
image: mysql:5.7
container_name: db
ports:
- "3306:3306"
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: rootpasswd
MYSQL_DATABASE: blog
MYSQL_USER: blog_admin
MYSQL_PASSWORD: userpasswd
networks:
- wordpress_net
networks:
wordpress_net:
volumes:
db_data:
Logs from db container are:
2017-05-12T23:28:06.138429Z 321 [Note] Access denied for user 'blog_admin'#'172.19.0.3' (using password: YES)
Logs from wordpress container are:
MySQL Connection Error: (1045) Access denied for user 'blog_admin'#'172.19.0.3' (using password: YES)
Warning: mysqli::mysqli(): (HY000/1045): Access denied for user 'blog_admin'#'172.19.0.3' (using password: YES) in - on line 22
docker ps:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1b02f0146fe7 wordpress:latest "docker-entrypoint..." 25 minutes ago Up 26 seconds 0.0.0.0:443->443/tcp, 0.0.0.0:8000->80/tcp wordpress
5d932ed6c269 mysql:5.7 "docker-entrypoint..." 25 minutes ago Up 25 minutes 0.0.0.0:3306->3306/tcp db
What have I tried:
Restarting docker host.
docker-compose rm -v and then docker-compose up -d again.
Logging in with those user credentials and root credentials outside of wordpress container.
Removing docker images and pulling them again from scratch.
Using root credentials in WORDPRESS_DB_HOST, WORDPRESS_DB_USER
I can see all the env vars for db when I connect to db container. Wordpress container keeps restarting it self. I saw one answer on stack overflow which recommended flushing privileges and setting new user account but I want to know if I'm doing something wrong that could cause this problem to appear again on other machine.
Change:
WORDPRESS_DB_USER: blog_admin
WORDPRESS_DB_PASSWORD: userpasswd
To:
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: rootpasswd
And then:
docker-compose up -d --build
Your username Blog_admin doesn't have access to create database.
What have I done:
docker-compose rm -v hasn't worked for me as I've always used docker-compose down to shutdown containers. And I think this is the root of the problem.
I deleted the folder with my docker-compose.yml and created a new one.
Then I created a compose file with just the config for mysql container, launched it and tried to connect to the mysql server as root.
It worked. Then I had to stop the container with docker stop containerID.
Then I ran docker-compose rm -v(For some reason rm -v works only when you stop the container. Not when you use docker-compose down this caused the db's state to persist, as I used a volume for the db container) and completed the yml file with the wordpress container config.
I've ended up with something like this:
version: '2'
services:
wordpress:
image: wordpress:latest
container_name: wordpress-blog
depends_on:
- mysql
ports:
- "8000:80"
- "443:443"
restart: always
environment:
WORDPRESS_DB_HOST: mysql
WORDPRESS_DB_USER: admin
WORDPRESS_DB_PASSWORD: password
WORDPRESS_DB_NAME: wordpress
mysql:
image: mysql:5.7
container_name: mysql-db
ports:
- "3306:3306"
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: admin
MYSQL_PASSWORD: password
NOTE: I had a problem previously not only connecting to the database from the wordpress container, but also from the db container itself. The method I described above helped me to solve this issue.
Simple solution
change
volumes:
- db_data:/var/lib/mysql
to something like
volumes:
- db_data:/var/lib/mysqlx