I am trying to connect to mysql database from docker image. However it's throwing errors.
following is the docker image I am using. https://hub.docker.com/_/mysql/
And following is the command I have used to run the docker image.
docker run -p 3306:3306 --name mysql_80 -e MYSQL_ROOT_PASSWORD=password -d mysql:8
Following is the output of docker ps
command
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9f35d2e39476 mysql:8 "docker-entrypoint.s…" 5 minutes ago Up 5 minutes 0.0.0.0:3306->3306/tcp
if I check the IP using docker inspect and ping that IP, it shows IP is not reachable.
docker inspect 9f35d2e39476 | grep -i ipaddress
And if i try to connect using localhost
and 127.0.0.1
I am getting following error.
Unable to load authentication plugin 'caching_sha2_password'.
This is probably considered solved, but I wanted to document what it took for me to overcome this. I was having a similar problem, and reverting back to the old password standard was not a solution. I needed to use the caching_sha2_password, so none of the above worked for me and I had to use this command:
docker run -it --rm mysql mysql -h 172.31.116.20 -p -P6603
Where the 172.31.116.20 is my local IP address where the container is running and -P6603 is the port it's running on.
I found this solution on the Docker site for the MySQL container: https://hub.docker.com/_/mysql/
It's in the "Connect to MySQL from the MySQL command line client" section.
In case you are trying to connect to MySQL using the terminal itself, you might have a buggy build. But if you are trying to connect to MySQL using a GUI client, for example, Sequel Pro, it might not support the new auth feature with MySQL 8.
As a workaround for this, you start your docker container with
--default-authentication-plugin=mysql_native_password
command at the end and it will default the MySQL to use the old authentication:docker run -p 3306:3306 --name mysql_80 -e MYSQL_ROOT_PASSWORD=password -d mysql:8 --default-authentication-plugin=mysql_native_password
If you want to use MySQL 8.0 and not get the "caching_sha2_password plugin" error, then check out a couple blog posts I wrote on how to setup MySQL 8.0 in Docker with persistent data, as well as a post on how to run your MySQL 8.0 container with mysql_native_password.
In short, you can create a local "my.cnf" config file:
Add the necessary config statement to it:
And then include that file as a volume bind in your "docker run" statement:
You can read more detail on these steps here:
https://medium.com/@crmcmullen/how-to-run-mysql-in-a-docker-container-on-macos-with-persistent-local-data-58b89aec496a
https://medium.com/@crmcmullen/how-to-run-mysql-8-0-with-native-password-authentication-502de5bac661
I found a fix here when firing up from docker-compose:
That is, revert from the new MySQL password/auth mechanism when starting MySQL.
First of all, be aware that you're using non stable software, so there can be major changes between releases and unexpected behaviour.
Secondly, you cannot ping directly your container, it's in other net, but you can easily use another container to ping him.
mysql 8 uses
caching_sha2_password
as the default authentication plugin instead ofmysql_native_password
. More info here.Many mysql drivers haven't added support for
caching_sha2_password
yet.If you're having problems with it, you can change to the old authentication plugin with something like this:
docker run -p 3306:3306 --name mysql_80 -e MYSQL_ROOT_PASSWORD=password -d mysql:8 mysqld --default-authentication-plugin=mysql_native_password
I had the same problem, but this didn't do it for me with a Docker container running mysql 8.X. I loged in the container
then log into mysql as root
Enter the password for root (Default is 'root') Finally Run:
You're all set.
This has already been answered here: post