0
votes

I tried to connect python to mysql database, but it throws Access denied for user 'root'@'localhost'. I'm using linux os.

Here is my code:

import mysql.connector as sa
db = sa.connect(host="localhost",user="root",password="sandy123",database="school")
print(db)

And this is the error.

Traceback (most recent call last): File "/home/sandytom/.local/lib/python3.6/site-packages/mysql/connector/connection_cext.py", line 216, in _open_connection self._cmysql.connect(**cnx_kwargs) _mysql_connector.MySQLInterfaceError: Access denied for user 'root'@'localhost'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "swip.py", line 2, in db = fuck.connect(host="localhost",user="root",password="sandy123") File "/home/sandytom/.local/lib/python3.6/site-packages/mysql/connector/init.py", line 264, in connect return CMySQLConnection(*args, **kwargs) File "/home/sandytom/.local/lib/python3.6/site-packages/mysql/connector/connection_cext.py", line 80, in init self.connect(**kwargs) File "/home/sandytom/.local/lib/python3.6/site-packages/mysql/connector/abstracts.py", line 966, in connect self._open_connection() File "/home/sandytom/.local/lib/python3.6/site-packages/mysql/connector/connection_cext.py", line 219, in _open_connection sqlstate=exc.sqlstate) mysql.connector.errors.ProgrammingError: 1698 (28000): Access denied for user 'root'@'localhost' sandytom@sandytom-Lenovo-ideapad-320-15ISK:~/Desktop/ruber$ python3 swip.py Traceback (most recent call last): File "/home/sandytom/.local/lib/python3.6/site-packages/mysql/connector/connection_cext.py", line 216, in _open_connection self._cmysql.connect(**cnx_kwargs) _mysql_connector.MySQLInterfaceError: Access denied for user 'root'@'localhost'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "swip.py", line 2, in db = fuck.connect(host="localhost",user="root",password="sandy123",database="school") File "/home/sandytom/.local/lib/python3.6/site-packages/mysql/connector/init.py", line 264, in connect return CMySQLConnection(*args, **kwargs) File "/home/sandytom/.local/lib/python3.6/site-packages/mysql/connector/connection_cext.py", line 80, in init self.connect(**kwargs) File "/home/sandytom/.local/lib/python3.6/site-packages/mysql/connector/abstracts.py", line 966, in connect self._open_connection() File "/home/sandytom/.local/lib/python3.6/site-packages/mysql/connector/connection_cext.py", line 219, in _open_connection sqlstate=exc.sqlstate) mysql.connector.errors.ProgrammingError: 1698 (28000): Access denied for user 'root'@'localhost'

1
With what user did you create the MySQL DB? Does the user 'root' have access to the DB? Note that root user voor MySQL is not the same as your local root user. - JarroVGIT
yu should check if you can log in with mysql client - nbk

1 Answers

0
votes

This is a case where reading the error message carefully will help. Here's the important part of the message:

Access denied for user 'root'@'localhost'

It looks like the username / password combination you gave MySQL is not valid. root is probably correct, so you should check your password.

A good way to do that is to try to log in with a MySQL client program, like the mysql client program or maybe HeidiSQL. That way you can make sure you have the correct password before you run your python program.