Mysql connect is failed

I connect mysql by this code,The code works fine locally, but an error occurs after being deployed in streamlit cloud:

def create_connection():
    """Create a database connection and return the connection object."""
    connection = None"连接mysql数据库")"host: {}")"user: {st.secrets.user}")"password: {st.secrets.password}")"database: {st.secrets.database}")"port: {st.secrets.port}")
        connection = mysql.connector.connect(
        if connection.is_connected():
            print("Successfully connected to the database")
            return connection
    except Error as e:
        print(f"The error '{e}' occurred")
        return None

the error is:
raceback (most recent call last):
File “/mount/src/learnenglishword/”, line 218, in get_all_words
with connection.cursor(buffered=True) as cursor:
AttributeError: ‘NoneType’ object has no attribute ‘cursor’
During handling of the above exception, another exception occurred
Traceback (most recent call last):
File “/home/adminuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/”, line 541, in _run_script
exec(code, module.dict)
File “/mount/src/learnenglishword/首页.py”, line 21, in
wordlist = models.get_all_words()
File “/mount/src/learnenglishword/”, line 236, in get_all_words
AttributeError: ‘NoneType’ object has no attribute ‘close’

Hi @yx135

There’s a step by step tutorial on connecting a Streamlit app to a MySQL database (Connect Streamlit to MySQL - Streamlit Docs).

Please ensure that

  • the database that you’re connecting to has data.
  • your MySQL database credentials are specified in the Streamlit secrets management
  • prerequisite libraries (mysqlclient and SQLAlchemy) are specified in requirements.txt.

Hope this helps!