Python抛出’ProgrammingError: 1045’当数据从YAML传递到MYSQL数据库时。

huangapple go评论82阅读模式
英文:

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

问题

我在Python中使用mysql.connector创建了一个连接对象。如果我直接在函数中传入主机(host)、用户(user)、密码(password)和数据库(database)的参数,它可以正常工作,但如果我从一个YAML文件中获取信息后再传入,就会抛出以下错误: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()) # 这个可以正常工作
print(connection_2()) # 这个不行

这里是YAML文件内容的截图:

Python抛出’ProgrammingError: 1045’当数据从YAML传递到MYSQL数据库时。

我打印了从YAML文件创建的字典对象的内容,这部分工作正常,但在创建连接时仍然不起作用。

英文:

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

Python抛出’ProgrammingError: 1045’当数据从YAML传递到MYSQL数据库时。

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.

答案1

得分: 3

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

英文:

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

huangapple
  • 本文由 发表于 2020年1月6日 17:05:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609265.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定