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

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

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)

  1. import mysql.connector
  2. import yaml
  3. config_data = yaml.load(open("config.yaml"), Loader=yaml.FullLoader)
  4. def connection_1():
  5. conn = mysql.connector.connect(
  6. host="127.0.0.1",
  7. user="test",
  8. passwd="Orion@123",
  9. database="socketTestDB"
  10. )
  11. c = conn.cursor()
  12. return c, conn
  13. def connection_2():
  14. conn = mysql.connector.connect(
  15. host=config_data["host"],
  16. user=config_data["user"],
  17. passwd=config_data["password"],
  18. database=config_data["database"]
  19. )
  20. c = conn.cursor()
  21. return c, conn
  22. print(connection_1()) # 这个可以正常工作
  23. 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)

  1. import mysql.connector
  2. import yaml
  3. config_data = yaml.load(open("config.yaml"), Loader=yaml.FullLoader)
  4. def connection_1():
  5. conn = mysql.connector.connect(
  6. host="127.0.0.1",
  7. user="test",
  8. passwd="Orion@123",
  9. database="socketTestDB"
  10. )
  11. c = conn.cursor()
  12. return c, conn
  13. def connection_2():
  14. conn = mysql.connector.connect(
  15. host=config_data["host"],
  16. user=config_data["user"],
  17. passwd=config_data["password"],
  18. database=config_data["database"]
  19. )
  20. c = conn.cursor()
  21. return c, conn
  22. print(connection_1()) # THIS ONE WORKS FINE
  23. 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.

  1. host: 127.0.0.1
  2. user: test
  3. password: Orion@123
  4. 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:

确定