【问题标题】:OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")
【发布时间】:2016-09-28 11:13:16
【问题描述】:

我使用 Scrapy 和 wnat 将数据插入到我的数据库中。

在我的 database.py 中有

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()

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

当我想连接到 mysql 时在管理器中

    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()

我有这个错误:

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

我读过这个questionthis one,但我已经遇到了同样的错误,我无法访问我的数据库

【问题讨论】:

    标签: python mysql xampp scrapy mysql-error-1045


    【解决方案1】:

    检查您是否正确设置了root密码,但请记住您可以重置密码。

    首先要做的事情。

    停止mysql服务器

    sudo /etc/init.d/mysql stop
    

    以 root 身份登录并停止 mysql 守护程序。现在让我们启动 mysql 守护进程并跳过存储密码的授权表。

    mysqld_safe --skip-grant-tables &
    

    您应该会看到 mysqld 成功启动。如果没有,那么你有更大的问题。现在你应该可以在没有密码的情况下连接到mysql了。

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

    【讨论】:

    • 我在我的问题中添加了一张照片,请查看
    • 我收到此错误:mysqld_safe --skip-grant-tables 2016-05-30T15:22:14.6NZ mysqld_safe 记录到 '/usr/local/var/mysql/pc-2.home.呃'。 2016-05-30T15:22:14.6NZ mysqld_safe 使用来自 /usr/local/var/mysql /usr/local/bin/mysqld_safe 的数据库启动 mysqld 守护程序:第 135 行:/usr/local/var/mysql/pc-2。 home.err:权限被拒绝 rm:/tmp/mysql.sock:权限被拒绝 /usr/local/bin/mysqld_safe:第 169 行
    • 你已经用 sudo 启动了?
    • 对不起,我忘记了您需要停止 mysql 服务器 "sudo /etc/init.d/mysql stop" 的评论。我编辑了我的解决方案,因为存在代码错误。现在是正确的
    • 找到你的mysql安装位置或mysql服务器管理并停止服务器。我不知道在其他操作系统中的位置。你的操作系统是什么?
    【解决方案2】:

    有同样的问题,改变了连接器,它对我有用,但首先安装模块

    $ pip install mysql-connector-python

    import mysql.connector
    cnx = mysql.connector.connect(user='root', password='psw', host='127.0.0.1', database='db')
    

    【讨论】:

      【解决方案3】:

      尝试在最后添加 Grant Option:

      GRANT all privileges on dbx.* to 'x'@'localhost' IDENTIFIED BY 'x' WITH GRANT OPTION;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-28
        • 1970-01-01
        • 1970-01-01
        • 2022-06-25
        • 2021-10-29
        • 2023-03-23
        相关资源
        最近更新 更多