OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")

parik picture parik · May 30, 2016 · Viewed 13k times · Source

I use Scrapy and wnat to insert the Data in my database.

in my database.py i have

def __init__(self, host='', user='', password='', database=''):
    self.__host     = 'localhost'
    self.__user     = 'root'
    self.__password = 'mypass'
    self.__database = 'drive'
## End def __init__

def __open(self):
    try:
cnx = MySQLdb.connect(self.__host, self.__user, self.__password, self.__database, port="3308")
self.__connection = cnx
self.__session    = cnx.cursor()

except MySQLdb.Error as e: print "\033[31mError %d: %s\033[0m" % (e.args[0],e.args1)

and in manager when i want to connect to mysql with

    self.mysql = MysqlPython(self.host, self.user, self.password, self.db)
    self.connection = MySQLdb.connect(self.host, self.user, self.password, self.db)
    self.cursor = self.connection.cursor()

i have this error:

OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")

I read this question and this one but i have already the same eror and i can' access to my database

enter image description here

Answer

fjmodi picture fjmodi · May 30, 2016

Check if you are correctly setting the root password, but remember you can reset the password.

First things first.

Stop mysql server

sudo /etc/init.d/mysql stop

Log in as root and stop the mysql daemon. Now lets start up the mysql daemon and skip the grant tables which store the passwords.

mysqld_safe --skip-grant-tables &

You should see mysqld start up successfully. If not, well you have bigger issues. Now you should be able to connect to mysql without a password.

mysql -u root mysql

update user set Password=PASSWORD('new-password') where user='root';
flush privileges;

exit;