1

I have a docker container up and running. When I log in to the container and try to connect to MySQL using python SQL connector I am not able to connect. I am trying to connect using the internal IP after changing the bind-address properties of running MySQL on the host system.

Error

mysql.connector.errors.DatabaseError: 2003 (HY000): Can't connect to MySQL server on '****' (113)

Count2.py

import mysql.connector
cnx = mysql.connector.connect(user='root', password='root',
                               host='172.31.37.139',port=3306,
                               database='test')

Docker command

sudo docker run -it -d -p 4040:4040 -v $(pwd)/count2.py:/count2.py d9e1d42543f9 bin/spark-submit --jars /usr/spark-2.4.1/mysql-connector-java-5.1.49/mysql-connector-java-5.1.49.jar --class com.mysql.jdbc.Driver /count2.py

Can someone help me here?

user7422128
  • 902
  • 4
  • 17
  • 41
  • Can you please elaborate your problem? Information like how you are running mysql container, how you are trying to connect would be helpful. – user1401472 May 13 '20 at 09:28
  • Are you running these on windows? – user1401472 May 13 '20 at 09:49
  • ubuntu @user1401472 – user7422128 May 13 '20 at 10:01
  • I have only a single container on my host machine which is posting a request to the mysql which is again my host. I didn't get what are you asking for?? @J.ScottElblein – user7422128 May 13 '20 at 12:53
  • As i understood: You are having a mysql database running on your host machine. On the same host machine you're running a docker container with the python script, right? – jdickel May 13 '20 at 15:33
  • 1
    You might wanna read up on Container Host Mode (https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) and try to simply connect to 127.0.0.1 (which is localhost) from inside your python container. Be sure to run the container in host mode. Otherwise 127.0.0.1 refers to the container itself, not to the host machine. – jdickel May 13 '20 at 15:34
  • I have pasted the answer below please accept it so that others don't keep searching for it – user7422128 May 13 '20 at 18:59

1 Answers1

1
sudo docker run  --network="host"  -it -d  -p  4040:4040 -d    -v $(pwd)/count.py:/count.py      d9e1d42543f9 bin/spark-submit    --jars /usr/spark-2.4.1/mysql-connector-java-5.1.49/mysql-connector-java-5.1.49.jar  --class com.mysql.jdbc.Driver /count.py

Just adding the new host tag worked for me

user7422128
  • 902
  • 4
  • 17
  • 41