pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 99] Cannot assign requested address)")


I am facing an issue with my Streamlit application when connecting to my local MySQL database. Here are the details of the error:

[22:41:36.439] Uncaught app exception

Traceback (most recent call last):

  File "/home/appuser/venv/lib/python3.9/site-packages/pymysql/", line 644, in connect
    sock = socket.create_connection(

  File "/usr/local/lib/python3.9/", line 844, in create_connection
    raise err

  File "/usr/local/lib/python3.9/", line 832, in create_connection

OSError: [Errno 99] Cannot assign requested address

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/", line 552, in _run_script
    exec(code, module.__dict__)

  File "/app/av/1_🔐", line 9, in <module>
    db = pymysql.connect(**db_config)

  File "/home/appuser/venv/lib/python3.9/site-packages/pymysql/", line 358, in __init__

  File "/home/appuser/venv/lib/python3.9/site-packages/pymysql/", line 711, in connect
    raise exc

pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 99] Cannot assign requested address)")

Additionally, I noticed that Streamlit mentions a new version is available, but I’m not sure if I should upgrade before resolving this issue.

Can you provide guidance on how to resolve this error, and would upgrading Streamlit possibly help?

Thanks in advance for your assistance.

If streamlit runs on any hosted environment, you cannot connect to a database running on your local client. The database must have a public ip address and be accessible from the internet.

Thank you for your quick response to my issue! Which platform do you recommend for hosting my database?

It depends at least on:

  • your skills
  • database type
  • database size
  • performance requirements
  • costs
  • maintenance effort
  1. You can install and run a native database server on almost any hosting provider that offers containerized, virtual or dedicated servers.
  2. The bigger hosting providers mostly offer also managed databases (aka DBaaS) in different flavors.
  3. There are also some providers who specialize in DBaaS and only offer these services.
  4. Here is a list of “free” database hosting providers for testing or small projects. Of course, the features are limited and their service can be shutdown at any time:

I want something simple, and costs are not necessarily a concern for me initially. I was thinking of leaning towards Google Cloud SQL. I don’t have much expertise, but I am self-taught.

Then go for it. Maybe it is not the cheapest option in the long run, but any DBaaS is much simpler than installing and managing your own database server.

Thank you for your support and responses! I hope to make progress in my project! Can I come to you if I have more questions? I am self-taught, but sometimes there are answers I can’t find on the internet. Thanks again.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.