Mysql.connector issue

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

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

Code:

import  mysql.connector as sql 
s =sql.connect(host='localhost',user='root', password='<correct password>',database='<existing database>')

The password I entered is the one I use to successfully login to MySQL 8.0 Command line client.
Same code is working for pymysql.connect without any error.
I checked out the Host and User Name by logging in to MYSQL 8.0 Command Line client with my password and typing:

mysql>Select current_user;

Output:

+----------------+
| current_user   |
+----------------+
| root@localhost |
+----------------+

Thanks in advance.
Kindly help me out as I tried but couldn’t solve it.

Eagerly waiting for someone to help me out.

Hey DB1,

you can format your code in the forum so it becomes more readable for others.
Here is a nice tutorial that tells you how you can do that:

To your question. I guess you forgot to add the database parameter to your sql.connect() call.

best
Dennis

I have formatted the post and also added database argument. It gives the same error even after adding database name which exists. Kindly help me out.

Hey DB1,

that’s the point where I would check with wireshark what connecting stinrg gets used by your code to connect or the MySQL service. But I guess that’s not an option for you?

Plan b would be to debug into the sql.connect function call and to find out how the connection that the mysql library creates looks like. Do you know how you can debug Python code?

If not, please tell us what IDE or code edtor you use. So maybe we can get you started into this road.

Debugging is a very important skill that you will need as a software developer. So the sooner you learn it, the better. :slight_smile:

Sorry that there is no immediate suggestion what is wrong with your code or setup. We can only speculate because we have not enough information to really figure it out.

best
Dennis

I don’t know how to debug. Will need help in this regard too. I am using Python IDLE 3.9 64 bit, downloaded from official Python website python.org.

Oh, to be honest I have never heard about Python IDLE. I can only help you with instructions for VsCode and Intellij & PyCharm.

But I found this article that explains how you can debug with Python IDLE:

https://www.cs.uky.edu/~keen/help/debug-tutorial/debug.html

Try that out. If it doesn’t work I would suggest that you install VsCode with the required Python Plugin.

Here is a nice Post that explains how you can install all necessary plugins for VsCode:

https://www.cs.uky.edu/~keen/help/debug-tutorial/debug.html

Perhaps a few side notes about VsCode. VsCode is a code editor that supports a plethora of Plugins. It is one of the most popular code editors and supports many programming languages. It is also completely free and supported under Windows, Linux and MacOs.

It might be a good idea to use VsCode if you want to make programming your profession at some point. I worked with different programming languages using VsCode in different companies. It hasn’t failed me yet. :slight_smile:

best
Dennis