简体   繁体   中英

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

I am following along with lecturer's code and videos. He has this set up, and I have followed exactly. His works, mine doesn't and I cant figure out why. It is set up as user "root" and password is blank. I have tried pip install mysql-connector-python. I want to keep the same user and password as his so as to follow along better. I am using python and mysql via Wampserver64. When I try to run the python file through cmd I get the error "mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)". I am new to this so trying to figure it out as I go along. Does the (using password: YES) mean that the passwords match? And how to I get script to connect to mysql?



db = mysql.connector.connect(
    host = "localhost",
    user= "root",
    password = " "
    #database ='datarepresentation'
)
        #print ("connection made")

cursor = db.cursor()

cursor.execute("CREATE DATABASE datarepresentation")

The same problem occurred when my friend tried to run a python script in the Ubuntu Windows Linux Subsystem that uses a MySQL database set up.

We fixed the problem by running the following three commands in the MySQL 8.0 Command Line Client and then restarting the machine to reboot everything. We are using Flask in our project and not Wamp so hopefully it will work the same. These commands were found here .

SELECT user, authentication_string, plugin, host FROM mysql.user;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Current-Root-Password';
FLUSH PRIVILEGES;

According to MySQL documentation( MySQL Documentation ), it states that (using password: YES) just means that you are in fact using a password. If you would have tried to login without using a password, it would say 'NO'.

As far as how to connect your script to your database, you pretty much have it. You can write a query to retrieve something from the database to check. Here is an example using the database you mentioned to retrieve some kind of data and make sure it's in the table.

cnx = mysql.connector.connect(
    host="localhost", 
    user='root', 
    password=" ", 
    database='datarepresentation')
cursor = cnx.cursor()
query = ("SELECT * FROM table-name WHERE key1 = %s")
dataName = 'randomValue'
cursor.execute(query, (dataName))
result = cursor.fetchone()
if result[0] == 1:
    return True
else:
    return False

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

Solution: Don't assign any value to password argument and pass as ie password=''

try to create new user

mysql> CREATE USER 'monty'@'localhost' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'@'localhost'
    ->     WITH GRANT OPTION;
mysql> CREATE USER 'monty'@'%' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'@'%'
    ->     WITH GRANT OPTION;

I also encountered it and solve such as below. import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  #remove this line or password="" 
)

It's remove the line of the password. if you want to insert the line of the password,it's unwanted the spase(for instance, password="" , this isn't password=" " ).

this program is (using password: YES). Why can't i solve?

I thought that this error is indicating already "The passwords match." so,description of the password is unwanted in mydb=mysql.connector.connect( ).

I have specified how to solve on my site [troubleshooting] ProgrammingError: **** (*****): Access denied for user 'root'@'localhost' (using password: YES) mysql-connector of python.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM