鍍金池/ 問答/Java  Python  數(shù)據(jù)庫/ pycharm連接mysql數(shù)據(jù)庫連接不上

pycharm連接mysql數(shù)據(jù)庫連接不上

代碼其實很簡單,只有一小段,是在pycharm上運行的,所用的python版本為2.7,mysql版本為5.7.21

# -*- coding: UTF-8 -*-
import re
import MySQLdb

if __name__ == '__main__':
    #打開數(shù)據(jù)庫
    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沒錯,是可以登錄的。

clipboard.png
是可以排除密碼錯誤的問題。

clipboard.png

數(shù)據(jù)庫‘shixiseng’也是存在的

clipboard.png
端口也是3306沒錯

我也上網(wǎng)搜過解決辦法,依舊不行。
例如:

執(zhí)行授權(quán)命令
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天了不知道如何是好。

回答
編輯回答
朕略傻
1. 授權(quán)
GRANT ALL PRIVILEGES ON shixiseng.* TO 'root'@'localhost' IDENTIFIED BY '1234567' WITH GRANT OPTION; 
2. 刷新
FLUSH PRIVILEGES;

此處你得注意一點,你的hosts文件里面有沒有把127.0.0.1指向localhost

2017年6月6日 16:13