leaf
leaf

Reputation: 35

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")

Upvotes: 2

Views: 25463

Answers (5)

Chen
Chen

Reputation: 1

I also encounter a similar issue when I try to move the DB connection setup credential into a .env file and solve it by printing what I get from the .env file. so basically when using USER=='user_name' in the .env file, it actually gets the user from the environment as in the official document stats:

By default, load_dotenv doesn't override existing environment variables. therefore, it gets my environment, USER, than what I set up in the .env file. just changing the USER to another variable name would solve the problem.

  • .env
USER_NAME='readonly'
USER='cannot_say'
  • conftest.py
@pytest.fixture
def connect_db():
    print(f"\nthis is USER_NAME: ", os.getenv('USER_NAME'))
    print(f"this is USER: ",  os.getenv('USER'))
  • console
this is USER_NAME:  read-only
this is USER:  chihchencheng

Upvotes: 0

user20544559
user20544559

Reputation:

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.

Upvotes: 0

damp11113
damp11113

Reputation: 51

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;

Upvotes: 0

Murtaza Ali
Murtaza Ali

Reputation: 1

Don't assign any value to password argument and pass as i.e password=''

Upvotes: 0

Sophia MB
Sophia MB

Reputation: 1

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

Upvotes: 0

Related Questions