Docker Community Forums

Share and learn in the Docker community.

Docker container to connect with MySQL locally installed

Hi,

I have been searching for this query alot, found many similar post but yet couldn’t get there.

Problem:
I have MySQL Locally Installed, and I’m a beginner in docker, I have built my first docker app which has pymysql library inside the python app. But I’m unable to docker run it and access the docker, I understand docker is isolated and yet can’t access the MySQL.

But please sugggest me how to do it??

Tried this cmd:

docker run --add-host=“localhost:192.168.0.xx” --name back-container -p 5000:5000 back-image,

But Getting this error:
pymysql.err.OperationalError: (1045, “Access denied for user ‘root’@‘host.docker.internal’ (using password: YES)”)

Please helpout

Network wise, a container is seen as a remote machine. They dont share the same “localhost”.
Adding a --add-host=“localhost:192.168.0.xx” flags in your docker command will add a line in the /etc/hosts file inside the container, so you’re creating an alias for physical host as “localhost” which is un true.
What you want to do is :

  • Change your application code to use “database” instead of “localhost” as database host
  • Use a --add-host=“database:192.168.0.xx”

So everything will be clearer. Then follow the 1st answer here : https://stackoverflow.com/questions/11223235/mysql-root-access-from-all-hosts
Because, from your mysqld perspective, your application’s connection is coming from a remote host and thus root have to be allowed to connected from a remote host (whose IP wont be known before hand).

Hi Sebt3,

Sure will do this, Thank you for your reply.

One more thing, I have installed MySQL in windows. So does it follow then?

Hi,

I gave #bind-address=127.0.0.1 under my.ini file (under mysqld),
and also ran docker run --add-host=“database:192.168.0.xx” --name back-container -p 5000:5000 back-image,

and also did; ```
GRANT ALL PRIVILEGES ON . TO ‘root’@‘localhost’;

Still getting;;;; pymysql.err.OperationalError: (2003, “Can’t connect to MySQL server on ‘localhost’ ([Errno 99] Cannot assign requested address)”)

In mysql, you need this :

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password';

But about this :

(emphasis mine)

You need to change the code (or the configuration) from the application living in that back-image docker image. Many images have an environement parameter to change the database hostname

1 Like

Hi,

I tried ```
GRANT ALL PRIVILEGES ON . TO ‘root’@’%’ IDENTIFIED BY ‘password’;


So I gave; GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost';  But still error.

And my Python code is:
db = pymysql.Connect("localhost","root","root_admin223","safety_" ) 

This is how I access the mysql.

GRANT ALL PRIVILEGES ON . TO ‘root’@’%’ IDENTIFIED BY ‘password’;

Gave me error in syntax as , “root is not valid at this position for this version”,

So gave the alternate as above.

It worked Sebt3, Thanks u so much for your assistance…