0

I want to connect to MySQL inside Docker container. I have a running instance of MySQL in a docker container. Since I already have 3306 port busy on my host, I decide to use port 8081 to start my MySQL container. Basically, I started my container with docker run -p 8080:80 -p 8081:3306 --name test test. When I connect to my container, I can connect to my MySQL without error. On the other hand, I have a web app that is able to connect to MySQL with the exact same port (8081) from my host. This means that MySQL is working properly and is reachable from outside. But now in my python script, I cannot connect. I am unable to connect to MySQL with CLI either. It seems like the port number is simply not interpreted. I see that if use for example mysql -P 8081 -u root -p. This is just trying to connect to host MySQL (port 3306) instead of container MySQL on port 8081(when I enter host MySQL credentials, it connect to host MySQL). In my python script, I used this: conn = MySQLdb.connect( host = 'localhost', port=8081, user='root', passwd=''). But this is not working either. In the MySQL man page, I see this :

   ·   --port=port_num, -P port_num
       The TCP/IP port number to use for the connection.

What am I doing wrong, please?

mysql --version: 
mysql  Ver 14.14 Distrib 5.7.18, for Linux (x86_64) using  EditLine wrapper

update here is my docker ps:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                                    NAMES
b370c91594d3        test            "/bin/sh -c /start.sh"   14 hours ago        Up 14 hours         8080/tcp, 0.0.0.0:8080->80/tcp, 0.0.0.0:8081->3306/tcp   test
dmx
  • 1,862
  • 3
  • 26
  • 48
  • try changing localhost for 0.0.0.0 (could also try 127.0.0.1) – Nigel Ren Jun 25 '17 at 07:42
  • Is the web app also in a container? What platform are you running it on? – Adam Jun 25 '17 at 07:43
  • @Adam the web is in the same container, but I think the connection is made from outside. basically, it is a teampass (https://hub.docker.com/r/teampass/teampass/) container. I have used localhost as address. I am on Ubuntu 16.04. – dmx Jun 25 '17 at 07:54
  • You can access the web app from your local machine right? – Adam Jun 25 '17 at 08:00
  • @Adam yes, but when I try to connect to MySQL from CLI with `mysql -u root --port=8081`, I have this: `ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) ` – dmx Jun 25 '17 at 08:02
  • @Adam It is like it does not take into account the port – dmx Jun 25 '17 at 08:04
  • try using `--host=0.0.0.0` you may need `-p` for the password – Adam Jun 25 '17 at 08:04
  • @Adam `mysql -h 0.0.0.0 -u root --port=8081 -p Enter password: ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0` it has blank password. – dmx Jun 25 '17 at 08:07
  • Yeah then drop the `-p` flag. This is what I could find on your error. https://stackoverflow.com/questions/21091850/error-2013-hy000-lost-connection-to-mysql-server-at-reading-authorization-pa – Adam Jun 25 '17 at 08:11

0 Answers0