Please Help, can't connect my App to a local MySQL database

Hello All,
I have successfully built an Attendance App on my Windows system in Pycharm. This App simply collects a student’s email address and has a submit button. Once the student types their email and hits submit, the App creates a MySQL table if not exist and changes the student’s status from 0 to 1 in the table. While printing out a successful message for the student. And if a wrong email is submitted, a message is printed out to correct it.

Very simple and straightforward.

The App works perfectly on my local machine, using
connection = mysql.connector.connect(**st.secrets["mysql"])

I have the .streamlit/secrets.toml file as well as the secrets section of the App with the right database name, username, password and so on.

I read and re-read and do every single step listed in the streamlit doc for connecting MySQL databases and it works perfectly on my system, but fails to deploy on streamlit cloud, with message:

Can't connect to MySQL server on 'localhost:3306' (99)

Please see the detailed log below and help me point out what could be wrong. I really need this for my students and I am frustrated at the failures to deploy.

[22:08:23] 📦 Processed dependencies!

2022-06-06 22:10:40.801 Uncaught app exception
Traceback (most recent call last):
  File "/home/appuser/venv/lib/python3.9/site-packages/mysql/connector/", line 246, in _open_connection
_mysql_connector.MySQLInterfaceError: Can't connect to MySQL server on 'localhost:3306' (99)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/scriptrunner/", line 554, in _run_script
    exec(code, module.__dict__)
  File "", line 21, in <module>
    connection = mysql.connector.connect(**st.secrets["mysql"])
  File "/home/appuser/venv/lib/python3.9/site-packages/mysql/connector/", line 272, in connect
    return CMySQLConnection(*args, **kwargs)
  File "/home/appuser/venv/lib/python3.9/site-packages/mysql/connector/", line 94, in __init__
  File "/home/appuser/venv/lib/python3.9/site-packages/mysql/connector/", line 1052, in connect
  File "/home/appuser/venv/lib/python3.9/site-packages/mysql/connector/", line 251, 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 'localhost:3306' (99)

You just cannot connect from a webapp running on a dedicated server to a database running on your local system. You need a database not running on your local system to which you can connect from outside, i.e. from the server where streamlit is running.

Ok thank you, will try using Google CloudSQL, and will see how that works

Hi, I am currently facing the same problem, Could you help me with the solution if in case you figured it out?

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