2015-10-11 52 views
1
import MySQLdb 
db = MySQLdb.connect("localhost","root","root","aditya") 
cursor = db.cursor() 
cursor.execute("SELECT VERSION()") 
data = cursor.fetchone() 
print(data) 
db.close() 

結果:不能與Python 2.7的錯誤連接到MySQL 1045

Traceback (most recent call last): 
    File "C:\Users\Aditya\Desktop\database.py", line 4, in <module> 
    db = MySQLdb.connect("localhost","root","root","aditya") 
    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) 
OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)") 

這是很基本的東西?我無法理解我哪裏錯了。我使用Python 2.7版中,MySQL 5.6版,操作系統:Windows 10

mysql> show grants for 'root'@'localhost'; 
+----------------------------------------------------------------------------------------------------------------------------------------+ 
| Grants for [email protected]                            | 
+----------------------------------------------------------------------------------------------------------------------------------------+ 
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B' WITH GRANT OPTION | 
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION 


mysql> select * from mysql.user where user='root'\G 
*************************** 1. row *************************** 
        Host: localhost 
        User: root 
       Password: *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B 
      Select_priv: Y 
      Insert_priv: Y 
      Update_priv: Y 
      Delete_priv: Y 
      Create_priv: Y 
      Drop_priv: Y 
      Reload_priv: Y 
     Shutdown_priv: Y 
      Process_priv: Y 
      File_priv: Y 
      Grant_priv: Y 
     References_priv: Y 
      Index_priv: Y 
      Alter_priv: Y 
      Show_db_priv: Y 
      Super_priv: Y 
Create_tmp_table_priv: Y 
     Lock_tables_priv: Y 
      Execute_priv: Y 
     Repl_slave_priv: Y 
     Repl_client_priv: Y 
     Create_view_priv: Y 
     Show_view_priv: Y 
    Create_routine_priv: Y 
    Alter_routine_priv: Y 
     Create_user_priv: Y 
      Event_priv: Y 
      Trigger_priv: Y 
Create_tablespace_priv: Y 
       ssl_type: 
      ssl_cipher: 
      x509_issuer: 
      x509_subject: 
     max_questions: 0 
      max_updates: 0 
     max_connections: 0 
    max_user_connections: 0 
       plugin: mysql_native_password 
authentication_string: 
     password_expired: N 

我有所有的權限來訪問數據庫。它在MySQL命令行客戶端中完美工作。

+0

在嘗試授予權限時,[拒絕用戶root'@'localhost'的訪問被拒絕。如何授予權限?](http://stackoverflow.com/questions/8484722/access-denied-for-user-rootlocalhost-while-attempting-to-grant-privileges) – tlastowka

+0

試試這個答案的解決方案:http: //sackoverflow.com/a/489502/2104879 – mertyildiran

+0

我試過這個問題提到了什麼,仍然沒有爲我工作。 –

回答

0

您可以使用MySQL命令行工具使用相同憑據進行連接嗎?