Connect Streamlit to MySQL

This guide explains how to securely access a remote MySQL database from Streamlit Community Cloud. It uses the mysql-connector-python library and Streamlit's secrets management.



If you already have a database that you want to use, feel free to skip to the next step.

First, follow this tutorial to install MySQL and start the MySQL server (note down the username and password!). Once your MySQL server is up and running, connect to it with the mysql client and enter the following commands to create a database and a table with some example values:


USE pets;

CREATE TABLE mytable (
    name            varchar(80),
    pet             varchar(80)

INSERT INTO mytable VALUES ('Mary', 'dog'), ('John', 'cat'), ('Robert', 'bird');

Your local Streamlit app will read secrets from a file .streamlit/secrets.toml in your app's root directory. Create this file if it doesn't exist yet and add the database name, user, and password of your MySQL server as shown below:

# .streamlit/secrets.toml

host = "localhost"
port = 3306
database = "xxx"
user = "xxx"
password = "xxx"


When copying your app secrets to Streamlit Community Cloud, be sure to replace the values of host, port, database, user, and password with those of your remote MySQL database!

Add this file to .gitignore and don't commit it to your GitHub repo!

As the secrets.toml file above is not committed to GitHub, you need to pass its content to your deployed app (on Streamlit Community Cloud) separately. Go to the app dashboard and in the app's dropdown menu, click on Edit Secrets. Copy the content of secrets.toml into the text area. More information is available at Secrets Management.

Secrets manager screenshot

Add the mysql-connector-python package to your requirements.txt file, preferably pinning its version (replace x.x.x with the version you want installed):

# requirements.txt

Copy the code below to your Streamlit app and run it. Make sure to adapt query to use the name of your table.


import streamlit as st
import mysql.connector

# Initialize connection.
# Uses st.cache_resource to only run once.
def init_connection():
    return mysql.connector.connect(**st.secrets["mysql"])

conn = init_connection()

# Perform query.
# Uses st.cache_data to only rerun when the query changes or after 10 min.
def run_query(query):
    with conn.cursor() as cur:
        return cur.fetchall()

rows = run_query("SELECT * from mytable;")

# Print results.
for row in rows:
    st.write(f"{row[0]} has a :{row[1]}:")

See st.cache_data above? Without it, Streamlit would run the query every time the app reruns (e.g. on a widget interaction). With st.cache_data, it only runs when the query changes or after 10 minutes (that's what ttl is for). Watch out: If your database updates more frequently, you should adapt ttl or remove caching so viewers always see the latest data. Learn more in Caching.

If everything worked out (and you used the example table we created above), your app should look like this:

Finished app screenshot

Was this page helpful?

editSuggest edits

Still have questions?

Our forums are full of helpful information and Streamlit experts.