代码其实很简单,只有一小段,是在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
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没错,是可以登录的。
是可以排除密码错误的问题。
数据库‘shixiseng’也是存在的
端口也是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天了不知道如何是好。