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?
Related
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
Environment:
I've created and started a Docker container on Windows 11 via the following command:
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:latest
Then I've created a new HeidiSQL session with the following options:
library: libmysql.dll
host: 127.0.0.1
user: root
password: root
port: 3306
After click on Open it says "Can't connect to MySQL server on '127.0.0.1' (10061)".
I'am able to connect to the container via the terminal.
After some research and some experiment I am able to find a solution.
First of all the port fordwarding must be forced to be 3306:3306.
You must create and run the Docker container with the parameter -p 3306:3306.
docker run --name some-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:latest
Now the error is changed, but at least it can connect to the server.
"Access denied for user 'root'#'172.17.0.1' (using password: YES)"
Now most solutions found on the web tells to create a new user 'root'#'172.17.0.1' and grant all privileges to the user.
But, even after creating the user, HeidiSQL is unable to login again (same "access denied" error).
The strange thing is that if you login with the new user with a terminal, and then, try to login with HeidiSQL, now it works, this happens everytime you restart the container.
After searching I found this: https://github.com/HeidiSQL/HeidiSQL/issues/980
So the problem is that libmysql.dll is outdated and not supported, and it fails everytime you try to access for the first time with HeidiSQL.
You must switch to libmariadb.dll that is also compatible with mysql, is not needed to create the new user.
Do not use libmysql-6.1.dll, because crashes and not works for some reasons...
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';
I have installed mysql-client in my Mac by the following command:
brew install mysql-client
And when I run the following command:
mysql -h 127.0.0.1 -u root -p
An ERROR 1045 occured:
ERROR 1045 (28000): Access denied for user 'root'#'172.17.0.1' (using password: NO)
but when I use Sequel Pro I can login the local mysql server.
Why the 127.0.0.1 becomes 172.17.0.1 ?
MORE DETAIL INFO
I run this command on local host iTerm. I use the docker pull to pull a mysql:5.7 image from docker registry:
docker run --name mysql -e MYSQL_ROOT_PASSWORD=xxxxxx -d -p 0.0.0.0:3306:3306 mysql:5.7
And the mysql container is just running, then I use Sequel Pro GUI client to connect into the database in docker, and it works. And then I try to use
brew install mysql-client
to install the mysql command (because I want to use it to run a sql file to prepare data for my project's tests suite). after installed mysql-client, i use the command
mysql -h 127.0.0.1 -u root -p
try to connect database, but it promote the
**ERROR 1045 (28000): Access denied for user 'root'#'172.17.0.1' (using password: NO)**.
I did not do any configuration for the mysql docker container.
After hours try and search, finally I found the solution:
mysql -h 127.0.0.1 -P 3306 -u root -p
you need a -P parameter
I've got this configuration for a MySQL Docker image:
mysqldata:
image: busybox
volumes:
- /var/lib/mysql
mysql:
image: mysql:5.6
ports:
- "8203:3306"
volumes_from:
- mysqldata
environment:
- "MYSQL_ALLOW_EMPTY_PASSWORD=yes"
- "MYSQL_USER=root"
and I lift it up with docker-compose up -d mysql, and I can run commands against the DB in the image, access it without problems, etc.
Then I setup a docker image to be lifted in Bamboo:
But it fails when running commands against the DB in that image, with the error:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
From the build logs of the test run I can see that the env variables are applied:
/usr/bin/docker run --workdir /data --detach --name mysql-user-service -p 8203:3306 -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -e MYSQL_USER=root mysql:5.6
But still no success.
Anyone knows how to overcome this problem?
Where are you connecting from? The host, another container?
Did you give permissions to the user#localhost MySQL user so it has access to the database? That error usually means the user you are connecting with doesn't have access to the db, and you need to grant the permissions.
If you do a docker ps you will see the name and the ID of the container you started for MySQL. Take that name or ID and run
docker exec -it <name or id> sh
That will get you into the container and you can run your MySQL commands to grant access something like this.
GRANT ALL ON *.* TO 'root'#'localhost
Since you are using compose you can do something like this as well
docker-compose run mysql GRANT ALL ON *.* TO 'root'#'localhost
You might also have to create the database of there isn't one already, and you would use the same steps just change the sql that is run