title: mysql.connector.errors.DatabaseError: 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)
body: <p>I am creating a docker file that contains a flask app and mysql. This should run on localhost for now.</p>
  
  <p>The flask app is running and so as the mysql server. I am able to connect to mysql server. The app is not able to connect to db.</p>
  
  <p>Python code connecting</p>
  
  <pre class="lang-py prettyprint-override"><code>def establish_connection():
      config = {
          'user': 'root',
          'password': 'root',
          'host': '127.0.0.1',
          'port': '3306',
          'database': 'persist'
      }
  
      cnx: str = mysql.connector.connect(**config)
      print(cnx)
      return cnx
  </code></pre>
  
  <p>Dockerfile</p>
  
  <pre><code>FROM python:3.7.4-buster
  WORKDIR /stocksite
  ENV FLASK_APP main.py
  ENV FLASK_RUN_HOST 0.0.0.0
  EXPOSE 5000 32000 3306
  COPY . .
  COPY requirements.txt requirements.txt
  RUN pip install -r requirements.txt
  COPY . .
  CMD ["flask", "run"]
  </code></pre>
  
  <p>docker-compose.yml</p>
  
  <pre><code>version: '3'
  services:
    web:
      build: .
      ports:
        - "5000:5000"
  
    db:
      image: mysql
      container_name: db
      ports:
        - "32000:3306"
      environment:
        MYSQL_ROOT_PASSWORD: root
      volumes:
        - ./data/db:/docker-entrypoint-initdb.d
  </code></pre>
  
  <p>I receive the below error:</p>
  
  <p>mysql.connector.errors.DatabaseError: 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)</p>