Search code examples
pythonmysqlpython-3.xyamlpyyaml

Python throws 'ProgrammingError: 1045' when data is passed from YAML to connect with MYSQL database


I am creating a connection object with mysql.connector in Python. If I pass in the parameters for host, user, password and database directly in the function it works fine but if I pass the information after fetching it from a YAML file it throws the following error: mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'test'@'localhost' (using password: YES)

import mysql.connector
import yaml

config_data = yaml.load(open("config.yaml"), Loader=yaml.FullLoader)

def connection_1():
    conn = mysql.connector.connect(
        host="127.0.0.1",
        user="test",
        passwd="Orion@123",
        database="socketTestDB"
    )
    c = conn.cursor()
    return c, conn

def connection_2():
    conn = mysql.connector.connect(
        host=config_data["host"],
        user=config_data["user"],
        passwd=config_data["password"],
        database=config_data["database"]
    )
    c = conn.cursor()
    return c, conn

print(connection_1()) # THIS ONE WORKS FINE
print(connection_2()) # THIS ONE DOES NOT

Here's a screenshot of the YAML file content

enter image description here

I printed the content of the dictionary object created from the YAML file and that works fine as well but it still does not work while creating the connection.


Solution

  • I can see an additional , in your yaml file in the password section. remove it.

    So the final file looks as follows.

    host: 127.0.0.1
    user: test
    password: Orion@123
    database: socketTestDB