pythonmysqlxamppmysql-pythonmysql-error-1045

Cannot connect to MySQL in Python program


I am trying to establish a connection with a MySQL database in a python program

import mysql.connector
cnx = mysql.connector.connect(
    host='localhost',
    user='admin',
    password='admin1234',
    database='meu_banco'
)
cnx.close()

print(cnx)

but keep getting the following error:

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

Im am pretty sure that the username is admin and that I've setted 'admin1234' as the password. Anyone knows what might be happening?

Notes:

-I've also tried to use 'passwd' and 'db' instead of 'password' and 'database' keyword arguments.

-I'm using MySQL that comes with XAMPP version 8.0.2 / PHP 8.0.2 for Linux.


Solution

  • You are connecting to the database and afterwards you are closing the connection.

    Only use cnx.close() when you want to close the database connection, it may cause an error.

    If that's not the problem, you may have the user wrong, try to use the default username that MySQL Workbench has, it is root.