'Error: 1045 (28000) when connecting to mysql with python

def create_server_connection(host_name, user_name, user_password):
    connection = None
    try:
        connection = mysql.connector.connect(
            host = host_name,
            user = user_name,
            passwd = user_password
        )
        print("database connection successfully completed")
    except Error as err:
        print(f"Error: '{err}'")
    return connection

pw = ""
connection = create_server_connection("localhost", "root", pw)

I am setting up a connection from Jupyter Notebook to MySQL. I'm using localhost and the root user, which has no password. I got this error message below:

Error: '1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)'

Could anyone help? Thanks.



Solution 1:[1]

Exactly, you are probably using the wrong variable which in your own case is specified passwd instead of password BUT in the event you still have an error even after this you can also refer to this post from Jetbrains Blog. https://blog.jetbrains.com/datalore/2022/02/14/how-to-run-sql-from-jupyter-notebook-two-easy-ways/

Solution 2:[2]

I think you just have the incorrect keyword name for password. It should be

connection = mysql.connector.connect(
    host = host_name,
    user = user_name,
    password = user_password
)

Here is the documentation for a simple connection from the MySQL website. https://dev.mysql.com/doc/connector-python/en/connector-python-example-connecting.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Oyindamola Victor
Solution 2