Error in authenticate_user: mysql.connector.pooling.connect() argument after ** must be a mapping, not str

I am getting an error message:

Error in authenticate_user: mysql.connector.pooling.connect() argument after ** must be a mapping, not str

with the following code:

# Connect to your MySQL database 
db_config = st.secrets["db_credentials"]

def connect_db():
    try:
        return mysql.connector.connect(**db_config)
    except mysql.connector.Error as e:
        st.error(f"Error while connecting to MySQL: {e}")
        return None
I have "db_credentials" as my key for the connection credentials saved in secrets management which are as:

dialect = "mysql"
host = " "
port = 3306
database = "applogin"
username = " "
password = " "
ssl_ca = "DigiCertGlobalRootCA.crt.pem"
ssl_disabled = false

Kindly assist

Chris

Hi @chitemerere,

Thanks for posting!

The error is pointing out that the db_config is being passed to mysql.connector.connect() is not in the correct format; supposed to be a dictionary where the keys are the parameter names expected by the connect() function, such as user , password , host , database , etc. and not a string.

{
    'user': 'your_username',
    'password': 'your_password',
    'host': 'your_host',
    'database': 'your_database',
    'ssl_ca': 'path/to/DigiCertGlobalRootCA.crt.pem',
    'ssl_disabled': False
}

You should also consider using st.connection which makes connecting to dbโ€™s easier. Hereโ€™s an example code and you can find the full documentation below for more.

import streamlit as st

conn = st.connection("mysql")
df = conn.query("select * from pet_owners")
st.dataframe(df)

Let me know if this resolves the issue.

Many thanks, let me try it out and will come back to you.

I still get the error message:

Error in authenticate_user: mysql.connector.pooling.connect() argument after ** must be a mapping, not str

I have tried the second method user st.connection and I donโ€™t know if I am doing it right. Please assist.

Thank you in advance

One quick debugging trick is to do st.write(db_config) (just locally, DONโ€™T push your code that way) and see if db_config is actually a dictionary. If itโ€™s not, you might not have your config.toml set up properly.

If you want to use st.connection, see the docs here Connect Streamlit to MySQL - Streamlit Docs

Thank you, I will try it out.

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