MySQL with docker Access denied - mysql

I need to use MySQL on docker. The steps I've followed :
Installed docker
Pulled MySQL docker image
Ran docker container for MySQL
Installed MySQL client.
But When I run this
sudo docker exec -it mysql8 MySQL -uroot -p
It asks for a password, I enter the password and it gives me this :
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password : Yes)
How do I fix this and what am I missing?
I followed the tutorial here :
https://www.techrepublic.com/article/how-to-deploy-and-use-a-mysql-docker-container/

In order to create a docker container for mysql, you can use this command given below.
docker run -e MYSQL_ROOT_PASSWORD=password -e MYSQL_USER=user -e MYSQL_PASSWORD=password -d -p 3308:3306 -v $HOME/docker/volumes/mysql:/var/lib/mysql mysql
You can also set more environment variables for mysql in this given command.
In order to access mysql inside this container you have 2 easy ways:
Use docker exec -it container_name. Then write mysql -u root -p inside the container or along with the command given is your wish.
Use mysql client and access from local itself.

The article you have followed is outed and also running container and the change password is not the proper way when working in the container. With Offical image, you can set using environment variable.
use below command and further details you can find on Mysql Docker hub.
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=mypass -d mysql
then try to connect
docker exec -it some-mysql bash -c "mysql -u root -pmypass"

Found the answer.
Have to run
docker logs <container_name>
which gives a generated password using which we have to use when prompted for password.
Afterward, we can change the password by :
ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';

Related

How to restore Data from a MySQL dump file, from host to Docker container via. Windows

I've read through a lot of alternative solutions but I haven't found anything specific to my problem. I'm using Windows PowerShell, and I need to restore/populate my DB Docker container that I created using the following code below.
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 -e MYSQL_DATABASE=DB -e MYSQL_USER=PASSWORD -e MYSQL_PASSWORD=PASSWORD -d mysql:8
Then to populate my DB (Restoring data from an sql dump file) I enter in the following code below to point my .sql file to the DB container I created.
p.s. I have to place in dbl quotes due to windows syntax. I also can't place in the following character '<' before entering in my path because it's improper syntax for Docker on Windows)
docker exec -i some-mysql sh -c "exec mysql -uroot -p "$root"" "C:/filePathHere/all-databases.sql"
After I hit enter I receive the fallowing error below
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
Even after creating a new DB connection and adding in a new user/password, which I use root for both User/Pwd. I still get denied access.
Anyone know what I may be doing wrong ? It's important I populate my DB container this way and avoid manually restoring the dump file.
I've found the solution to my question, and first to start off, the cmmd using the fallowing syntax with an '<' operator is not supported in PowerShell v1 and as of v5, it's still not... You can either use regular cmmd line or place your docker cmmd syntax inside a .bat file and run it through windows powershell.
You cab refer to this site that provides the fallowing solution for the file path syntax using windows (I provided the code from the link below). Windows: Back up & restore MySQL data from Docker container
And if anyone is getting a similar error
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
A solution that worked for me was to add the fallowing cmmd mysql -u root to your Restore cmmd. Found this solution via this link here Access denied for user 'root'#'localhost'
BACK-UP
C:\docker\directory> docker ps
C:\docker\directory> docker exec container-id /usr/bin/mysqldump -u username database-name > dump.sql
RESTORE
C:\docker\directory> docker ps
C:\docker\directory> docker exec -i container-id /usr/bin/mysql -u username database-name < dump.sql
If you want to use docker-compose command
# To BACK BACKUP
C:\docker\directory> docker-compose exec -T container-name /usr/bin/mysqldump -u username database-name > dump.sql
# To ROLLBACK
C:\docker\directory> docker-compose exec -T container-name /usr/bin/mysql -u username database-name < dump.sql

Logging in to phpmyadmin - docker

Hi i am having trouble logging into phpmyadmin on localhost port 8080.
These are the steps i have carried out:
Running an sql container
~$ docker run -d mysql/mysql-server
I then use docker logs to get the generated password.
I ran this command to enter sql:
$ docker exec -it ce9a316 mysql -uroot -p
I then changed the password with this command:
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'mysql-password';
I then run my phpadmin and link this container to the sql
docker run --name phpmyadmin -d --link ce9a316046f0:db -p 8080:80 phpmyadmin
I am then trying to login to phpmyadmin on port 8080 with username: root and the password set above but it wont let me login. any help is much appricated.
This command fixed the issue:
CREATE USER 'root'#'%' IDENTIFIED BY 'some_pass';
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%';

Delphi Firedac connect to MySQL in docker container

I have created a MySQL image on my Windows 10 using the default settings from Docker.
I started the container using this command:
docker run --name local-mysql --network="host" -p 3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -d <your-docker-Image>
I used the --network parameter in the hope that I could connect to the container from my host computer.
Then I ran this command to connect to the container from the MySQL shell
docker exec -it mysql bash -l
I was able to connect using this
mysql -h localhost -P 3306 --protocol=tcp -u root -p
Using Delphi and setting FireDac to use DriverId MySQL, I specified host as localhost, port 3306, user as root and the password.
But I get this connection error
[FireDAC][Phys][MySQL] Cannot connect to MySQL server on 'localhost:3306' (10061)
I have tried using 127.0.0.1 and 0.0.0.0 without success and with the same error.
I would appreciate it if anyone has tried it with Delphi FireDac to connect to a MySQL container hosted on the same computer.
Thank you in advance.
I was able to solve the issue by using this command to run the image
docker run --name local-mysql -p 127.0.0.1:3307:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -d <your-docker-Image>
I used a different host port (3307) to map to the default 3306
I was able to test it using the bash first
mysql -h 127.0.0.1 -P 3306 -u root -p
And in Delphi FireDac, I used the following to connect
Host=127.0.0.1
Port=3307
User_Name=root
Password=my-secret-pw
And all is good. Hope this helps someone who is trying to the same.

How to commit changed data in a mysql container to a new image

I am trying to create a custom docker image with mysql database in it. I don't need to mount volumes or persist any data between container runs and removals. The goal is to always start containers in the same state. Use them for tests and remove them.
Steps taken:
Start a mariadb container from a generic mariadb image.
docker run --name some-mariadb -e MYSQL_ROOT_PASSWORD=root -d mariadb:10.1.24
1a657ebec5ba
Connect to db with mysql client to create a db, user and grant permissions.
mysql -uroot -proot -h 0.0.0.0
mysql> CREATE DATABASE store
mysql> CREATE USER 'store'#'%' IDENTIFIED BY 'store';
mysql> GRANT ALL ON store.* TO 'store'#'%';
mysql> exit
Import data.
mysql -ustore -pstore -h 0.0.0.0 store < ~/mysqldumps/store-2017-06-22-13-55.sql
At this point the container has the needed user, permissions, data and database.
Commit to the new image.
docker commit 1a657ebec5ba my-registry/store-test-db:2017-06-30-15-26
Now, when I start a container from this image on another host. Its just a plain mysql container without my user or database.
What is the correct way to create such image?
Your commands looks ok. How are you connecting to mysql container? It seems that you are working in your host mysql instead of the mysql that is running in the container. Add the port mapping configuration:
So, run as this:
docker run -p 3307:3306 --name some-mariadb -e MYSQL_ROOT_PASSWORD=root -d mariadb:10.1.24
Then connect as this to the container mysql:
mysql -P 3307 -h 127.0.0.1 -uroot -proot
Alternatively, you can access to the mysql terminal using docker exec:
docker exec -it <container-id> mysql -uroot -proot
mysql>
Edit
As #raitisd investigated, this is due to the VOLUME that is already defined in the mysql/mariadb base image.
You can use this to solve the issue:
Dockerfile:
FROM mariadb:10.1.24
RUN cp -r /var/lib/mysql /var/lib/mysql-no-volume
CMD ["--datadir", "/var/lib/mysql-no-volume"]
Build & run:
docker build . -t my-mariadb
docker run -e MYSQL_ROOT_PASSWORD=root -it my-mariadb

Accessing a MySql database from external IP (Docker containers)

Using Docker containers, I am trying to access a MySQL Docker container (https://hub.docker.com/_/mysql/) from other containers.
To do that, I've been using the PhpMyAdmin docker container (https://hub.docker.com/r/phpmyadmin/phpmyadmin/) which is the most simple way to view mysql databases.
Access denied
However... my phpmyadmin container cannot connect to the mysql container, giving the following error :
#1045 - Access denied for user 'root'#'172.17.0.7' (using password:
YES)
This is the config I have used for the mysql container:
docker run --name mysql001 -v /path/to/volumes/mysql001:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=1234 -p 33067:3306 -d mysql/mysql-server:latest --character-set-server=utf8 --collation-server=utf8_general_ci
The phpmyadmin configuration is:
docker run --name phpmyadmin1 -d --link mysql001:db -p 3949:80 phpmyadmin/phpmyadmin --env MYSQL_ROOT_PASSWORD=1234
Failed attempts
I've tried to log on in bash mode with docker exec -i mysql001 bash and run mysql commands from here, but each time I try for example to do mysql -u root -p so I get rejected with "Access denied for user 'root'#'localhost'"
What did I miss?