英文:
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文件内容的截图:
我打印了从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
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论