Mysql problems connecting to a python database

Asked

Viewed 150 times

0

Good morning,

When I try to connect python to a database I get the following error

MySQLdb._exceptions.OperationalError: (1045, "Access denied for user 'k4os'@'localhost' (using password: YES)")

I’m testing directly on the linux terminal so I don’t need to assign a variable and such

the code is

import MySQLdb
MySQLdb.connect(user='k4os',passwd='#mypass#')

se alguem souber ajudar agradeço :)
  • vc summed up or is missing host name on your connection (and maybe db too)?

2 answers

1


Hello,

You are not defining which database (db) will use for connection. Set like this:

db = MySQLdb.connect(
             host="localhost", 
             user="k4os", 
             passwd="#mypass#",
             db="SEU_BD"
)

If even then the error persists and you are sure that the access data is correct, then problem may be the lack of permission of that user for this database. You can try to fix it this way:

GRANT all privileges on SEU_BD.* to 'k4os'@'localhost' IDENTIFIED BY '#mypass#' WITH GRANT OPTION;

Give feedback if any of these solutions worked for you.

Note: change the SEU_BD for the name of your database.

  • File "<stdin>", line 1, in <module> File "/home/k4os/. local/lib/python3.6/site-Packages/Mysqldb/init.py", line 84, in Connect Return Connection(*args, **kwargs) File "/home/k4os/. local/lib/python3.6/site-Packages/Mysqldb/Connections.py", line 164, in init super(Connection, self).init(*args, **kwargs2) Mysqldb. _exceptions.Operationalerror: (2002, "Can’t connect to local Mysql server through socket '/var/run/mysqld/mysqld.Sock' (2)") Isto apareceu calculo que seja por nao ter a base de dados pronta eu apenas estava a criar ligações é esse o problema certo?

  • One more thing, that second sentence you wrote in case it’s necessary where do I write it? and that as it has ; in the end I do not see where it can be placed

0

Is the user configured correctly? Try to reset his password. I will leave here a reference that can be useful to you.

Accessing the system as root:

  • If you have root access:

    1. Start a customer (with password) $ mysql -u root -p
    2. Change the password (see below).
  • If you nay have root access (and need to set a password):

    1. Stop mysql daemon. Example: sudo /etc/init.d/mysql stop

    2. Start a new process with the option --Skip-Grant-Tables. Example: sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &

    3. Start a client as root (no password): mysql -u root

    4. In the mysql prompt, recharge the privileges: FLUSH PRIVILEGES;

    5. Change the password (see below).

Changing the passwords:

  • For versions of mysql up to 5.7.5, inclusive:
SET PASSWORD FOR 'root/k4os'@'localhost' = PASSWORD('nova_senha');
  • For higher versions:
ALTER USER 'root/k4os'@'localhost' IDENTIFIED BY 'nova_senha';

Be sure to restart the mysql daemon if you stopped it by the above procedure. Example: sudo /etc/init.d/mysql start

Browser other questions tagged

You are not signed in. Login or sign up in order to post.