Quantcast
Channel: Active questions tagged 22.04 - Ask Ubuntu
Viewing all articles
Browse latest Browse all 4477

ProgrammingError: 1045 (28000) Issue with logging into MySQL DB using Python script and Ubuntu

$
0
0

I have created a Python script that does some calculations and then points to a DB to fill out some tables. The DB is hosted on a Ubuntu Azure VM, where I set up a MySQL database. I have been able to write tables to the DB using a local DB (in Windows) and the following code, but when I try loging into the DB using Linux and the mysql.connector package, I get a:ProgrammingError: 1045 (28000): Access denied for user 'admin'@'localhost' (using password: NO).

'admin' is the name of the Azure VM that is hosting the server. I have tried using the DB root and its p/w, as well as another DB user name & p/w combination I have created and I get the same error. Any help is kindly appreciated.

def save_df_to_mysql_db(df, database_name, table_name, username='', password='', host='', port=''):    # Initialize the configparser    config = configparser.ConfigParser()    # Read the config.ini file    config.read('config_linux.ini')    # Get the database configuration values    db_config = {'port': config.get('DB', 'port'),'host': config.get('DB', 'host'),'username': config.get('DB', 'username'),'password': config.get('DB', 'password')    }    # Connect to MySQL database    conn = mysql.connector.connect(user=username, password=password, host=host, port=port, auth_plugin='mysql_native_password')    cursor = conn.cursor()    # Create database if it doesn't exist    cursor.execute(f"CREATE DATABASE IF NOT EXISTS {database_name}")    conn.database = database_name    # Create SQLAlchemy engine    engine = create_engine(f"mysql+mysqlconnector://{db_config['user']}:{db_config['password']}@{db_config['host']}:{db_config['port']}/{db_config['database']}")    # Save DataFrame to MySQL database    df.to_sql(table_name, con=engine, if_exists='replace', index=False)    # Close connection    cursor.close()    conn.close()    print(f"Dataframe {df} saved to {table_name} of {database_name}.")    

Viewing all articles
Browse latest Browse all 4477

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>