操作错误:(1045,“拒绝访问用户'root'@'localhost'(使用密码:YES)”)

2024-05-14 19:32:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用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。错误为e: 打印“\033[31mError%d:%s\033[0”%(e.args[0],e.args1

当我想用

    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但是我已经有了相同的eror并且我可以访问我的数据库

enter image description here


Tags: self数据库localhosthostinitdefconnectroot
3条回答

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

第一件事。

停止mysql服务器

sudo /etc/init.d/mysql stop

以根用户身份登录并停止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;

尝试在末尾添加授权选项:

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

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

$pip安装mysql连接器python

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

相关问题 更多 >

    热门问题