2

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

Community
  • 1
  • 1
parik
  • 2,313
  • 12
  • 39
  • 67

3 Answers3

2

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;
fjmodi
  • 66
  • 4
  • I added a photo to my question, please check it out – parik May 30 '16 at 15:16
  • i got this error: mysqld_safe --skip-grant-tables 2016-05-30T15:22:14.6NZ mysqld_safe Logging to '/usr/local/var/mysql/pc-2.home.err'. 2016-05-30T15:22:14.6NZ mysqld_safe Starting mysqld daemon with databases from /usr/local/var/mysql /usr/local/bin/mysqld_safe: line 135: /usr/local/var/mysql/pc-2.home.err: Permission denied rm: /tmp/mysql.sock: Permission denied /usr/local/bin/mysqld_safe: line 169 – parik May 30 '16 at 15:18
  • 1
    You've launched with sudo? – fjmodi May 30 '16 at 15:22
  • with sudo i have this error: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) – parik May 30 '16 at 15:25
  • 1
    I'm sorry, I forgot comment that you need to stop mysql server "sudo /etc/init.d/mysql stop" . I edited my solution because there are code errors. Now is correct – fjmodi May 30 '16 at 15:38
  • i don't have /etc/init.d/mysql, but when i try to install mysql by brew install mysql i have Warning: mysql-5.7.12 already installed – parik May 30 '16 at 15:50
  • 1
    find your mysql installed location or mysql server management and stop server. I don't know the location in other OS. What is your OS? – fjmodi May 30 '16 at 15:55
  • el capitain 10.11.3 – parik May 30 '16 at 15:55
  • i can do all this in phpmyadmin? i change the password but i have the same eror – parik May 30 '16 at 15:59
  • For mysql pre 5.7 _sudo /usr/local/mysql/support-files/mysql.server stop_ for post 5.7 _sudo launchctl unload -F /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist_ in my os x El capitan there are a mysql management and I can stop server from this – fjmodi May 30 '16 at 15:59
  • i tried the second one and i have this : /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist: Could not find specified service – parik May 30 '16 at 16:06
  • Check your System preferences [link]http://dev.mysql.com/doc/refman/5.7/en/images/mac-installer-preference-pane-location.png and select MySQL Preference Panel to stop server [link]http://dev.mysql.com/doc/refman/5.7/en/images/mac-installer-preference-pane-usage.png – fjmodi May 31 '16 at 06:57
  • thanks, but i have this error mysql: [ERROR] unknown option '--u' when i type mysql --u root mysql – parik May 31 '16 at 08:44
  • Sorry is mysql -u root mysql – fjmodi May 31 '16 at 09:21
2

had same issue ,changed connector and it worked for me, but first install the module

$ pip install mysql-connector-python

import mysql.connector
cnx = mysql.connector.connect(user='root', password='psw', host='127.0.0.1', database='db')
Medi
  • 1,026
  • 2
  • 9
  • 12
1

Try adding Grant Option at the end:

GRANT all privileges on dbx.* to 'x'@'localhost' IDENTIFIED BY 'x' WITH GRANT OPTION;
Lucas
  • 154
  • 1
  • 12