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

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

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.

Docker compose does not execute .sql

Trying to use docker compose for my application, so using official mysql images and my own Dockerfile to create my app image. I think some how my initdb.sql is not executed during docker compose up.
Running this as :
docker-compose up --build
But I can not see that initdb.sql is executed.
My docker compose looks like this:
version: "3"
services:
db:
image: mysql:5.7
restart: always
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_DATABASE: "MYDB"
MYSQL_USER: "myuser"
MYSQL_PASSWORD: "mypassword"
MYSQL_ROOT_PASSWORD: "mypassword"
ports:
- "3306:3306"
expose:
- "3306"
volumes:
- my-db:/var/lib/mysql
- ./sqlinit:/docker-entrypoint-initdb.d/initdb.sql
networks:
vpcbr:
ipv4_address: 10.5.0.6
app:
restart: always
build: .
ports:
- 5000:5000
volumes:
- .:/app
depends_on:
- db
networks:
vpcbr:
ipv4_address: 10.5.0.5
volumes:
my-db:
networks:
vpcbr:
driver: bridge
ipam:
config:
- subnet: 10.5.0.0/16
# gateway: 10.5.0.1
As you can see I have my initdb.sql in this directory ./sqlinit and after I login to mysql container I can see this file in /docker-entrypoint-initdb.d/initdb.sql
But looks like is not executed because when I login to my app I can see:
pymysql.err.OperationalError: (1044, "Access denied for user 'myuser'#'%' to database 'MYDB'")
and on container mysql
2019-09-15T13:17:59.361447Z 2 [Note] Access denied for user 'myuser'#'%' to database 'MYDB
Of course I'm doing something wrong, I found some similar problems here but I still haven't fixed my issue
The problem should be in the way you are mapping the volume. You should map the source file into the target file instead of a source folder into a file, like so:
- ./sqlinit/initdb.sql:/docker-entrypoint-initdb.d/initdb.sql

How to fix 'The server requested authentication method unknown to the client [caching_sha2_password]' on docker compose?

I'm setting up a wamp. The docker-compose was working very well until I experience some problems with the phpmyadmin and mysql container. I couldn't connect nor from php or phpmyadmin and usually had this error message : mysqli_real_connect(): The server requested authentication method unknown to the client [caching_sha2_password]
I was able to solve the problem by just connecting entering the shell of the db container docker exec -it db mysql -uroot -p and running this command : ALTER USER 'root' IDENTIFIED WITH mysql_native_password by '123456'; But this kind of boring because I have a partner working on the same project and we have to change working posts a lot so it means rerun docker each time in the development phase so I was wondering what's wrong on my docker-compose...
Here it is :
version: "3.1"
services:
www:
build: .
container_name: app
ports:
- "8001:80"
volumes:
- ~/Desktop/WORK_in_progress/camagru/www/:/var/www/html/
links:
- db
networks:
- default
db:
image: mysql:8.0
container_name: db
restart: always
tty: true
ports:
- "3306:3306"
command: --default-authentication-plugin=mysql_native_password
command: --innodb-use-native-aio=0
environment:
MYSQL_DATABASE: CAMAGRU
MYSQL_USER: user
MYSQL_PASSWORD: 123456
MYSQL_ROOT_PASSWORD: 123456
volumes:
- ~/Desktop/WORK_in_progress/camagru/dump:/docker-entrypoint-initdb.d
- ~/Desktop/WORK_in_progress/camagru/conf:/etc/mysql/conf.d
- persistent:/var/lib/mysql
networks:
- default
phpmyadmin:
container_name: phpmyadmin
restart: always
tty: true
image: phpmyadmin/phpmyadmin
links:
- db:db
ports:
- 8000:80
environment:
MYSQL_USER: user
MYSQL_PASSWORD: 123456
MYSQL_ROOT_PASSWORD: 123456
volumes:
persistent:
note : I don't use any configurations file, I modified a bit a compose that I found online.
Your command is right, the issue appears because you have defined the command key two times, and the second one is overriding the first one. Instead you should define it only once and like this:
command: --default-authentication-plugin=mysql_native_password --innodb-use-native-aio=0

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.