代码其实很简单,只有一小段,是在pycharm上运行的,所用的python版本为2.7,mysql版本为5.7.21

# -*- coding: UTF-8 -*-

import re

import MySQLdb

if __name__ == '__main__':

#打开数据库

conn = MySQLdb.connect(host='localhost',port=3306,user='root',passwd='1234567',db='shixiseng')

cursor = conn.cursor()

cursor.close()

conn.close()

运行后代码报错:

Traceback (most recent call last):

File "C:/Users/Braggart/PycharmProjects/exercise/33.py", line 57, in <module>

conn = MySQLdb.connect(host='localhost',port=3306,user='root',passwd='1234567',db='shixiseng')

File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect

return Connection(*args, **kwargs)

File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 187, in __init__

super(Connection, self).__init__(*args, **kwargs2)

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

首先,在mysql的命令行里我的密码是没有错误的。密码确实是1234567没错,是可以登录的。

ba2bddfb17ad01bad8974dec987b3dd9.png

是可以排除密码错误的问题。

1ba3f41728e441543857c3d38e11fd8e.png

数据库‘shixiseng’也是存在的

c716ec47f92ae25f8c8698cb8999114c.png

端口也是3306没错

我也上网搜过解决办法,依旧不行。

例如:

执行授权命令

mysql> grant all privileges on *.* to root@localhost identified by '1234567';

mysql>grant all privileges on shixiseng.* to root@localhost identified by '1234567';

mysql>flush privileges;

请各路大神帮忙看看,到底是哪里除了问题,小白真的捣鼓了2天了不知道如何是好。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐