How to resolve Access denied for user 'root'@'localhost' or Host '127.0.0.1' is not allowed to connect to this MySQL server when trying to access mysql with docker
In this post, I will demonstrate how to resolve the following error when trying to access MySQL in Docker.
2. What happened?
I am trying to deploy a mysql instance on my remote server, just as the following diagram shows:
I started a docker on my server , the command is as follows:
I can explain the above options :
-v: —volume ,bind a volume, from host to container
-p means to bind a host port and map it to container port, from host to container.
-d means detach
But when I go into the container and try to access mysql , I got this error:
3. How to solve it?
I tried to simplify the docker run command as follows:
I removed the -v and --restart options from the command line. Because there is no such directory mysql-vol in the directory where I run the command. And in the same time, I changed the docker image to mysql:5.7, which is a stable version.
Now test again, I got this:
You can see that I succeeded to log on mysql in docker.
4. The reason
I think the key reason for the error are as follows:
The ‘-v’ option bind a local directory to docker volume, but it does not exist in local path.
The docker image file for mysql is very important, we should choose a version that is stable.
5. Summary
In this post, I demonstrated how to solve the ‘Access denied for user ‘root’@‘localhost” or ‘Host ‘127.0.0.1’ is not allowed to connect to this MySQL server’ when using docker to install mysql. The key point is to find the right options to start your mysql container.
Final Words + More Resources
My intention with this article was to help others who might be considering solving such a problem.
So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by
email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope: