docker-compose mysql container denies access to wordpress container - mysql

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

Related

Docker mysql root or user won't login, just access denied. Using docker compose

Using docker compose i have a mysql 5.7 database container, i set the root password and a user password but they don't work, the docker-compose:
version: '3.8'
services:
viprs-proxy:
platform: linux/amd64
image: nginx:alpine
container_name: viprs-proxy
depends_on:
- viprs-website
volumes:
- ./nginx/proxy.conf:/etc/nginx/nginx.conf
ports:
- 80:80
networks:
- viprs-net
viprs-website:
platform: linux/amd64
image: nginx
container_name: viprs-website
depends_on:
- php
- viprs-website-database
volumes:
- ./website/nginx/site.conf:/etc/nginx/conf.d/default.conf
- ./website:/usr/share/nginx/html
- ./website/logs:/var/log/nginx
- viprs-uploads:/usr/share/nginx/html/wp-content/uploads
ports:
- 80
links:
- php
networks:
- viprs-net
php:
platform: linux/amd64
#image: php:7-fpm
image: viprs-php
container_name: php
volumes:
- ./website:/usr/share/nginx/html
ports:
- 9000
networks:
- viprs-net
viprs-website-database:
platform: linux/amd64
image: mysql:5.7
container_name: viprs-db
command: --init-file /usr/share/nginx/website.sql
volumes:
- ./website.sql:/usr/share/nginx/website.sql
- viprs-db:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: testpassword
MYSLQ_DATABASE: viprs
MYSQL_USER: viprs
MYSQL_PASSWORD: testpassword
networks:
- viprs-net
networks:
viprs-net:
volumes:
viprs-uploads:
viprs-db:
Now if i log into bash:
docker exec -it viprs-db bash
and try to log into mysql:
mysql -u root -p
It just says access is denied, it doesn't matter if i am using the root user or the viprs user.
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
If i output $MYSQL_ROOT_PASSWORD it gives:
echo $MYSQL_ROOT_PASSWORD
testpassword
I have seen loads of people with this issue but can't find any solution that works. The environment is set as per the image documentation on docker hub so i'm confused.
In fact, i can log in as root without a password, so something isn't right.
The environment variables are only used if there is no database present when then container starts and MySQL has to create one.
If there already is a database, the users defined in that database are used and no new users are created. Since you have a volume mapping on /var/lib/mysql chances are that you already have a database.
To verify if that's the issue, you can try removing the /var/lib/mysql mapping. That will cause the container to create a new database when it starts using the environment variable values.

Laravel dockerized app taking DB_HOST as SERVER_ADDR instead of container

I have a laravel app and the database containers are configured in this way:
db:
image: "mysql:5.7"
command: ["--sql- mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"]
environment:
MYSQL_DATABASE: mydb
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- db_data:/var/lib/mysql
restart: unless-stopped
networks:
- simple_net
app:
build:
context: .
dockerfile: ./setup/Dockerfile
restart: unless-stopped
tty: true
ports:
- "${APP_PORT}:80"
working_dir: /var/www/
volumes:
- ./:/var/www/
environment:
DB_CONNECTION: mysql
DB_HOST: db
DB_DATABASE: mydb
DB_USERNAME: root
# SERVER_ADDR: localhost
When I tried to start the app with docker-compose up it throws an exception that says SQLSTATE[HY000] [1045] Access denied for user 'root'#'172.23.0.6' (using password: NO). The IP refers to the environmental variable SERVER_ADDR, I tried to set it up with the values localhost and '' (empty) but nothing has worked so far.
You should set the .env's DB_HOST to the name of your container, in your case db.
DB_HOST=db
I do not believe injecting environment variables replaces filling the .env.
Also, if you want to Dockerize your Laravel applications, several options already exist and work out of the box:
Laradock
Docker Compose Laravel
Laradose (which I maintain)
You don't need DB_CONNECTION: mysql there.
Instead inside your .env file in lavel you will use this like:
DB_CONNECTION: mysql
DB_HOST: db
DB_PORT: 3306
DB_DATABASE: mydb
DB_USERNAME: root
DB_PASSWORD: password
Your app container does not need this environment, that's your server running. You try to configure the environment of the db container inside server container which will run your app.
Change your docker-compose to:
db:
image: "mysql:5.7"
command: ["--sql- mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"]
environment:
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: password
MYSQL_DATBASE: mydb
MYSQL_USER: root
MYSQL_PASSWORD: password
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- db_data:/var/lib/mysql
restart: unless-stopped
networks:
- simple_net
app:
build:
context: .
dockerfile: ./setup/Dockerfile
restart: unless-stopped
tty: true
ports:
- "${APP_PORT}:80"
working_dir: /var/www/
volumes:
- ./:/var/www/
networks:
- simple_net
Bridge your containers under your network so they can interact with each other.
Run docker-compose up --build and then access your laravel container and run inside the container php artisan optimize to configure cache for routes,env variables, files etc etc.
docker exec -it db mysql -u root -p password
Based on your comments you have issues to access the container and the reason is you don't use credentials for it and mysql tries to use the default ones. Above command defines the right user and password, you will also be asked to fill the password.
Also mysql has more parameters to define the host etc etc you can have a look further for that.

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.

Host 'X' is not allowed to connect to this MySQL server

I wanna deploy MySQL+PHPMyAdmin. My docker-compose.yml:
version: "3"
services:
db:
image: mysql:5.7
restart: always
container_name: db
volumes:
- ./~mysql:/var/lib/mysql
- ./mysql.cnf:/etc/mysql/conf.d/my.cnf
environment:
MYSQL_DATABASE: "dbtest"
MYSQL_ROOT_PASSWORD: "123456"
MYSQL_ROOT_HOST: "%"
networks:
- db
command: --default-authentication-plugin=mysql_native_password
healthcheck:
test: "mysqladmin ping -h localhost"
interval: 1s
timeout: 1s
retries: 60
phpmyadmin:
image: phpmyadmin/phpmyadmin:4.7
restart: always
container_name: phpmyadmin
ports:
- 8080:80
networks:
- external-net
- db
environment:
PMA_HOST: db
depends_on:
- db
networks:
external-net:
external:
name: external-net
db:
driver: bridge
After some time later I getting subject error. MYSQL_ROOT_HOST don't helped. When I trying to connect to mysql from db-container:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
I really don't know what to do with this magic... Thx.
This problem might occur, if the files on your local system were created in a corrupted way or are not correctly accessible by the Docker daemon. This might be due to the following reasons:
Docker is lacking the access rights on your local hard drive, on Windows e.g. C.
While building up your containers Docker didn't have the access rights to your local hard drive. Even though Docker asks during the first --build process to allow an access to C on Windows these files might still be corrupted.
The solution could be do delete the according local files after the access to Docker has been granted, in your case these are files in /~mysql and the file mysql.cnf.
You can pass an extra environment variable when starting the MySQL container MYSQL_ROOT_HOST= this will create a root user with permission to login from given IP address. In case where you want to allow login from any IP you can specify MYSQL_ROOT_HOST=%.
This will work only for a newly created containers.
When spinning new container:
docker run --name some-mysql -e MYSQL_ROOT_HOST=% -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest
In compose file it would be
version: '2'
services:
### Mysql container
mysql:
image: mysql:latest
ports:
- "3306:3306"
volumes:
- /var/lib/mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: test_db
MYSQL_USER: test
MYSQL_PASSWORD: test_pass
MYSQL_ROOT_HOST: '%' # needs to be enclosed with quotes
I've recreated your setup and was just adding some ENV configuration to do the trick, I've removed volumes section because there was no problem with it:
docker-compose.yml
version: "3"
services:
db:
image: mysql:5.7
restart: always
container_name: db
environment:
- MYSQL_ROOT_PASSWORD=rootpasswd
- MYSQL_DATABASE=phpmyadmin
- MYSQL_USER=user
- MYSQL_PASSWORD=userpasswd
networks:
- db
command: --default-authentication-plugin=mysql_native_password
healthcheck:
test: "mysqladmin ping -h localhost"
interval: 1s
timeout: 1s
retries: 60
phpmyadmin:
image: phpmyadmin/phpmyadmin:4.7
restart: always
container_name: phpmyadmin
ports:
- 8080:80
networks:
- external-net
- db
environment:
PMA_HOST: db
depends_on:
- db
networks:
external-net:
external:
name: external-net
db:
driver: bridge
Accessing PHPMyadmin with root:rootpasswd works fine.
For some reason using "~" before the volume path solves the problem for me.
volumes:
- ./~mysql:/var/lib/mysql
- ./mysql.cnf:/etc/mysql/conf.d/my.cnf
Change this code to
volumes:
- ~/mysql:/var/lib/mysql
for MySQL container.
In Genetal settings of Docker enable daemon without TLS. I think It works.
Docker image

docker phppyadmin container Access denied for user 'root'

I have installed docker on digitalocean droplet successfully and below shows my docker-compose.yml configurations:
version: '2.1'
services:
mysql:
build:
context: ./docker/mysql
image: mysql:latest
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: root_pass
MYSQL_USER: root
MYSQL_PASSWORD: root_pass
volumes:
- mysqldata:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
links:
- mysql:db
environment:
MYSQL_USERNAME: root
MYSQL_ROOT_PASSWORD: root_pass
restart: always
ports:
- 8080:80
And once I up the docker services everything works fine as you can see by below screen capture.
And I can access my mysql database inside terminal perfectly with my user credentials.
But the problem is when I try to access phpmyadmin with droplet_ip:8080 its says:
#1045 - Access denied for user 'root'#'172.18.0.4' (using password: YES)
And here I used same username (root) password (root_pass) as well.
Any suggestions regarding this problem would be grateful. Thank you.
The following works for me
version: '3.1'
volumes:
mysql-volume:
services:
mysql:
image: mysql
container_name: mysql
volumes:
- mysql-volume:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_DATABASE: db
MYSQL_USER: user
MYSQL_PASSWORD: supersecret
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
environment:
PMA_HOST: mysql
MYSQL_USER: user
MYSQL_PASSWORD: supersecret
ports:
- 80:80
depends_on:
- mysql
don't mix up root user and MySQL user. In my example above I use the MySQL user to login with phpadmin. if you want to login using your root user you don't have to specify MySQL user and it will look like this (you don't need to specify a user for phpmyadmin because it's always root):
version: '3.1'
volumes:
mysql-volume:
services:
mysql:
image: mysql
container_name: mysql
volumes:
- mysql-volume:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_DATABASE: db
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
environment:
PMA_HOST: mysql
MYSQL_ROOT_PASSWORD: rootpass
ports:
- 80:80
depends_on:
- mysql
Also very important, remove your mysql volume when you want to recreate the whole setup. (docker volume rm ..). Because maybe your mysql is started with the same volume again after making changes.
I got the access denied error because I had a dollar sign in my password, which the docker compose file parses as variable substitution (I've only seen the ${} syntax) on this page you will read "Both $VARIABLE and ${VARIABLE} syntax are supported."
Therefore if I had for example this as my database password in the docker compose file: hello$world, the world variable would get substituted, of course I don't have a 'world' variable, the password that would be set in that case would be hello, so without knowing the docker compose syntax you would be trying to log in with hello$world, and you'd be denied access.