App.py not connect to mysqldb

I tried to reproduce the example on https://docs.docker.com/language/python/develop/#connect-the-application-to-the-database but unfortunately the app.py not connect to mysqldb
This is the error:

* Running on all addresses.

WARNING: This is a development server. Do not use it in a production deployment.

* Running on http://172.18.0.3:5000/ (Press CTRL+C to quit)

* Environment: production

WARNING: This is a development server. Do not use it in a production deployment.

Use a production WSGI server instead.

* Debug mode: off

[2021-11-06 18:44:57,587] ERROR in app: Exception on /initdb [GET]

Traceback (most recent call last):

File "/usr/local/lib/python3.8/site-packages/mysql/connector/connection_cext.py", line 237, in _open_connection

self._cmysql.connect(**cnx_kwargs)

_mysql_connector.MySQLInterfaceError: Can't connect to MySQL server on '127.0.0.1:3306' (111)

``

During handling of the above exception, another exception occurred:

``

Traceback (most recent call last):

File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 2073, in wsgi_app

response = self.full_dispatch_request()

File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1518, in full_dispatch_request

rv = self.handle_user_exception(e)

File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1516, in full_dispatch_request

rv = self.dispatch_request()

File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1502, in dispatch_request

return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)

File "/app/app.py", line 35, in db_init

mydb = mysql.connector.connect(

File "/usr/local/lib/python3.8/site-packages/mysql/connector/__init__.py", line 273, in connect

return CMySQLConnection(*args, **kwargs)

File "/usr/local/lib/python3.8/site-packages/mysql/connector/connection_cext.py", line 86, in __init__

self.connect(**kwargs)

File "/usr/local/lib/python3.8/site-packages/mysql/connector/abstracts.py", line 1015, in connect

self._open_connection()

File "/usr/local/lib/python3.8/site-packages/mysql/connector/connection_cext.py", line 242, in _open_connection

raise errors.get_mysql_exception(msg=exc.msg, errno=exc.errno,

mysql.connector.errors.DatabaseError: 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306' (111)

172.18.0.1 - - [06/Nov/2021 18:44:57] "GET /initdb HTTP/1.1" 500 -

The container is correctly up&running. I tried successully the command:
docker exec -ti mysqldb mysql -u root -p

sql>

Could you help me?
Daniele

It says it tried to connect to the local IP. Why? Have you changed the example python code which uses “mysqldb” as hostname?

Thanks, now it works well!