2

I am trying to get root access to my Docker MySQL image that I created. I placed environment variables for ROOT_PASSWORD and DATABASE so I'm sure that a password does exist and I am able to access root using the command line. The problem lies in trying to access root with MySQLWorkbench or SequelPro. I have hostname set to localhost, user as root and port as 3307 (I had mapped this port when running the command to create the database). When I attempt to connect I get an error

"Your connection attempt failed for user 'root' to the MySQL server at localhost:3307: Host is not allowed to connect to this MySQL server"

I've tried changing the port number to 3306, changing the IP to 127.0.0.1 as well as but the error doesn't go away. I checked a GitHub post that suggested that root user should have "%" as host and that could be achieved by changing my bind-address. Problem is I cannot find the mysql.cnf config file...I don't even think one exists on my system

https://i.stack.imgur.com/BSSYc.png

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • Actually you have two containers running with the same image. Close all containers and start new one. Also paste the command you are using to launch the container here. – Michał Krzywański May 26 '19 at 07:45
  • I struggled with this and finally got it working and I wrote it all up at: https://stackoverflow.com/questions/33001750/connect-to-mysql-in-a-docker-container-from-the-host/77003515#77003515 – raddevus Aug 30 '23 at 14:55

2 Answers2

1

The easiest way to allow external connections to your docker based mysql is to pass a variable as follows:

docker run --name=name -p 3306:3306 -v your-volume:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -e MYSQL_ROOT_HOST:"%" -d mysql

use MYSQL_ROOT_HOST:"%" to allow remote connect from any IP. For defined IP's replace % with your external IP address.

For More information https://devopsbuild.com/docker-host-is-not-allowed-to-connect-to-this-mysql-server/

-1

A surefire way to get access is to:

docker inspect mysql_container_name

to find the ip address.

Then you can use something like:

mysql -u root -p -h <docker container ip address>

A couple of other notes:

  • The default mysql port is 3306, so you should also double check that you didn't accidentally map it backward.
  • The environment variable is called MYSQL_ROOT_PASSWORD not ROOT_PASSWORD
  • You may need to supply a config that binds and listens on a network interface rather than a socket
  • The config file is inside the docker container. You can map a volume to use a local file instead:

docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

See here for more info: https://docs.docker.com/samples/library/mysql/

Dov Rine
  • 810
  • 6
  • 12
  • This `docker inspect` path is not a best practice. It does not work on MacOS hosts, and the IP address it returns is inaccessible from anywhere other than the current host. The way the questioner is setting up `docker run -p` and connecting to the database is correct. – David Maze May 26 '19 at 11:27