Member Avatar for DB12

I am trying to connect MySQL(8.0.25) and python(3.8) using mysql.connector but getting the error:

mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Windows 10

same code working for pymysql.connect

the code used:

import mysql.connector as sql
s=sql.connect(host='localhost' ,user='root', password='<correct password>)
The password I entered is the one I use to successfully login to MySQL command line client.
Didn't get any real help in Google. Looking forward for Help. Thanks in advance.

I gather that you are upgrading an older program from pyMySQL.connect to MySQL/Connector. What version of MySQL are you using, and if it is an older database, is it using the older security model from prior to MySQL 4.1? Going by the official documentation regarding the connection arguments, "MySQL Connector/Python does not support the old, less-secure password protocols of MySQL versions prior to 4.1." I don't personally know what those older protocols entailed, so I can't say what the relevance of it is, but it was something I thought worth pointing out.

Beyond that I would check the troubleshooting page of the MySQL documentation if you haven't done so already.

commented: MySQL version in 8.0.25 and Python3.8. +0

You might also look at this page to see if it applies to you, if you haven't already.

commented: I am not logging in remotely, I am doing on local mysql instance. Moreover, I had already checked it out. +0

The code you provided has an opening quote for the password but no ending quote. I suspect it was just a typo when you snipped out the password or else it would haven given a syntax error. But just checking that there are no silly mistakes.

Perhaps completely unrelated, and I have absolutely no python experience, but what if you change localhost to 127.0.0.1? A very long time ago, I ran into a performance issue with resolving hostnames.

Also, is there a chance you need to specify a port? Again, I don’t know python so just taking random stabs in the dark.

commented: I tried your advice but ,unfortunately, the error persists. +0

And what if you don’t use quotes around the username, password, etc. like that page suggests? Have you tried specifying the port? Please post your updated code. :)

commented: s=sql.connect(host=localhost,user=root,password=<correctpassword>,port=3306) removing quotes gives : NameError: name 'localhost' is not defined +0

Please try using the format described in the stack overflow question.

commented: s=sql.connect(username=root,password=<correctpassword>,host=localhost,database=empl) gave error:NameError: name 'root' is not defined ;database exists +0

Why would you use root for user? Why not create a separate user for that table? root is too powerful.

Plus there is no database name only device, user and password.

s=sql.connect(username=root,password=<correctpassword>,host=localhost,database=empl) gave error:NameError: name 'root' is not defined ;database exists

Seems you didn't create a root user when setting up mySQL, so you can't log in using that user either. You may have to log in anonymously...

See what credentials work on the commandline and use those.

commented: I checked the user and host of mysql commandline after logging in with the password and found root@localhost +0

have you check the firewall settings of the operating system? it is some times the firewall will block the connection.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.