2017-07-29 83 views
0

此問題有幾處發生,但沒有答案提供有關如何修復或診斷問題的信息。 我有使用MySQL JDBC連接到MySQL數據庫我的本地服務器上的一個簡單的Java應用程序:
使用JDBC連接到MySQL時訪問被拒絕

String url = "jdbc:mysql://localhost:3306/dbName"; 
    String userName = "parser"; 
    String password = "123"; 
    try { 
     Connection conn = DriverManager.getConnection(url,userName, password); 
    } catch (SQLException e1) { 
     e1.printStackTrace(); 
    } 

但是,Java拋出一個異常:

java.sql.SQLException: Access denied for user 'parser'@'localhost' (using password: YES) 

許多疑難呈三角問題建議用戶有connetction沒有特權,但在我的情況下,它具有:

+---------------------------------------------------------------+ 
| Grants for [email protected]%           | 
+---------------------------------------------------------------+ 
| GRANT ALL PRIVILEGES ON *.* TO 'parser'@'%' WITH GRANT OPTION | 
+---------------------------------------------------------------+ 

任何一個可以給我解釋一下爲什麼會這樣,如何診斷或理解問題是什麼?

更新1: 權限MySQL用戶

+--------------------------+---------------+-------------------------+--------------+ 
| GRANTEE     | TABLE_CATALOG | PRIVILEGE_TYPE   | IS_GRANTABLE | 
+--------------------------+---------------+-------------------------+--------------+ 
| 'root'@'localhost'  | def   | SELECT     | YES   | 
| 'root'@'localhost'  | def   | INSERT     | YES   | 
| 'root'@'localhost'  | def   | UPDATE     | YES   | 
| 'root'@'localhost'  | def   | DELETE     | YES   | 
| 'root'@'localhost'  | def   | CREATE     | YES   | 
| 'root'@'localhost'  | def   | DROP     | YES   | 
| 'root'@'localhost'  | def   | RELOAD     | YES   | 
| 'root'@'localhost'  | def   | SHUTDOWN    | YES   | 
| 'root'@'localhost'  | def   | PROCESS     | YES   | 
| 'root'@'localhost'  | def   | FILE     | YES   | 
| 'root'@'localhost'  | def   | REFERENCES    | YES   | 
| 'root'@'localhost'  | def   | INDEX     | YES   | 
| 'root'@'localhost'  | def   | ALTER     | YES   | 
| 'root'@'localhost'  | def   | SHOW DATABASES   | YES   | 
| 'root'@'localhost'  | def   | SUPER     | YES   | 
| 'root'@'localhost'  | def   | CREATE TEMPORARY TABLES | YES   | 
| 'root'@'localhost'  | def   | LOCK TABLES    | YES   | 
| 'root'@'localhost'  | def   | EXECUTE     | YES   | 
| 'root'@'localhost'  | def   | REPLICATION SLAVE  | YES   | 
| 'root'@'localhost'  | def   | REPLICATION CLIENT  | YES   | 
| 'root'@'localhost'  | def   | CREATE VIEW    | YES   | 
| 'root'@'localhost'  | def   | SHOW VIEW    | YES   | 
| 'root'@'localhost'  | def   | CREATE ROUTINE   | YES   | 
| 'root'@'localhost'  | def   | ALTER ROUTINE   | YES   | 
| 'root'@'localhost'  | def   | CREATE USER    | YES   | 
| 'root'@'localhost'  | def   | EVENT     | YES   | 
| 'root'@'localhost'  | def   | TRIGGER     | YES   | 
| 'root'@'localhost'  | def   | CREATE TABLESPACE  | YES   | 
| 'mysql.sys'@'localhost' | def   | USAGE     | NO   | 
| 'parser'@'127.0.0.1'  | def   | SELECT     | YES   | 
| 'parser'@'127.0.0.1'  | def   | INSERT     | YES   | 
| 'parser'@'127.0.0.1'  | def   | UPDATE     | YES   | 
| 'parser'@'127.0.0.1'  | def   | DELETE     | YES   | 
| 'parser'@'127.0.0.1'  | def   | CREATE     | YES   | 
| 'parser'@'127.0.0.1'  | def   | DROP     | YES   | 
| 'parser'@'127.0.0.1'  | def   | RELOAD     | YES   | 
| 'parser'@'127.0.0.1'  | def   | SHUTDOWN    | YES   | 
| 'parser'@'127.0.0.1'  | def   | PROCESS     | YES   | 
| 'parser'@'127.0.0.1'  | def   | FILE     | YES   | 
| 'parser'@'127.0.0.1'  | def   | REFERENCES    | YES   | 
| 'parser'@'127.0.0.1'  | def   | INDEX     | YES   | 
| 'parser'@'127.0.0.1'  | def   | ALTER     | YES   | 
| 'parser'@'127.0.0.1'  | def   | SHOW DATABASES   | YES   | 
| 'parser'@'127.0.0.1'  | def   | SUPER     | YES   | 
| 'parser'@'127.0.0.1'  | def   | CREATE TEMPORARY TABLES | YES   | 
| 'parser'@'127.0.0.1'  | def   | LOCK TABLES    | YES   | 
| 'parser'@'127.0.0.1'  | def   | EXECUTE     | YES   | 
| 'parser'@'127.0.0.1'  | def   | REPLICATION SLAVE  | YES   | 
| 'parser'@'127.0.0.1'  | def   | REPLICATION CLIENT  | YES   | 
| 'parser'@'127.0.0.1'  | def   | CREATE VIEW    | YES   | 
| 'parser'@'127.0.0.1'  | def   | SHOW VIEW    | YES   | 
| 'parser'@'127.0.0.1'  | def   | CREATE ROUTINE   | YES   | 
| 'parser'@'127.0.0.1'  | def   | ALTER ROUTINE   | YES   | 
| 'parser'@'127.0.0.1'  | def   | CREATE USER    | YES   | 
| 'parser'@'127.0.0.1'  | def   | EVENT     | YES   | 
| 'parser'@'127.0.0.1'  | def   | TRIGGER     | YES   | 
| 'parser'@'127.0.0.1'  | def   | CREATE TABLESPACE  | YES   | 
| 'simple_cms'@'localhost' | def   | USAGE     | NO   | 
| 'root'@'%'    | def   | SELECT     | YES   | 
| 'root'@'%'    | def   | INSERT     | YES   | 
| 'root'@'%'    | def   | UPDATE     | YES   | 
| 'root'@'%'    | def   | DELETE     | YES   | 
| 'root'@'%'    | def   | CREATE     | YES   | 
| 'root'@'%'    | def   | DROP     | YES   | 
| 'root'@'%'    | def   | RELOAD     | YES   | 
| 'root'@'%'    | def   | SHUTDOWN    | YES   | 
| 'root'@'%'    | def   | PROCESS     | YES   | 
| 'root'@'%'    | def   | FILE     | YES   | 
| 'root'@'%'    | def   | REFERENCES    | YES   | 
| 'root'@'%'    | def   | INDEX     | YES   | 
| 'root'@'%'    | def   | ALTER     | YES   | 
| 'root'@'%'    | def   | SHOW DATABASES   | YES   | 
| 'root'@'%'    | def   | SUPER     | YES   | 
| 'root'@'%'    | def   | CREATE TEMPORARY TABLES | YES   | 
| 'root'@'%'    | def   | LOCK TABLES    | YES   | 
| 'root'@'%'    | def   | EXECUTE     | YES   | 
| 'root'@'%'    | def   | REPLICATION SLAVE  | YES   | 
| 'root'@'%'    | def   | REPLICATION CLIENT  | YES   | 
| 'root'@'%'    | def   | CREATE VIEW    | YES   | 
| 'root'@'%'    | def   | SHOW VIEW    | YES   | 
| 'root'@'%'    | def   | CREATE ROUTINE   | YES   | 
| 'root'@'%'    | def   | ALTER ROUTINE   | YES   | 
| 'root'@'%'    | def   | CREATE USER    | YES   | 
| 'root'@'%'    | def   | EVENT     | YES   | 
| 'root'@'%'    | def   | TRIGGER     | YES   | 
| 'root'@'%'    | def   | CREATE TABLESPACE  | YES   | 
| 'parser'@'%'    | def   | SELECT     | YES   | 
| 'parser'@'%'    | def   | INSERT     | YES   | 
| 'parser'@'%'    | def   | UPDATE     | YES   | 
| 'parser'@'%'    | def   | DELETE     | YES   | 
| 'parser'@'%'    | def   | CREATE     | YES   | 
| 'parser'@'%'    | def   | DROP     | YES   | 
| 'parser'@'%'    | def   | RELOAD     | YES   | 
| 'parser'@'%'    | def   | SHUTDOWN    | YES   | 
| 'parser'@'%'    | def   | PROCESS     | YES   | 
| 'parser'@'%'    | def   | FILE     | YES   | 
| 'parser'@'%'    | def   | REFERENCES    | YES   | 
| 'parser'@'%'    | def   | INDEX     | YES   | 
| 'parser'@'%'    | def   | ALTER     | YES   | 
| 'parser'@'%'    | def   | SHOW DATABASES   | YES   | 
| 'parser'@'%'    | def   | SUPER     | YES   | 
| 'parser'@'%'    | def   | CREATE TEMPORARY TABLES | YES   | 
| 'parser'@'%'    | def   | LOCK TABLES    | YES   | 
| 'parser'@'%'    | def   | EXECUTE     | YES   | 
| 'parser'@'%'    | def   | REPLICATION SLAVE  | YES   | 
| 'parser'@'%'    | def   | REPLICATION CLIENT  | YES   | 
| 'parser'@'%'    | def   | CREATE VIEW    | YES   | 
| 'parser'@'%'    | def   | SHOW VIEW    | YES   | 
| 'parser'@'%'    | def   | CREATE ROUTINE   | YES   | 
| 'parser'@'%'    | def   | ALTER ROUTINE   | YES   | 
| 'parser'@'%'    | def   | CREATE USER    | YES   | 
| 'parser'@'%'    | def   | EVENT     | YES   | 
| 'parser'@'%'    | def   | TRIGGER     | YES   | 
| 'parser'@'%'    | def   | CREATE TABLESPACE  | YES   | 
+--------------------------+---------------+-------------------------+--------------+ 
+0

你可以在mysql工作臺中使用該密碼登錄嗎? –

+0

除了授予所有權限外,還必須將該用戶添加到允許連接的用戶列表中。你可以在mysql服務器配置中的某處找到它。 –

+0

是的,當然我可以使用命令行,工作臺甚至相同的證書在python腳本中工作 – iColdBeZero

回答

0

好像用戶 「解析」 的權限列表是不正確的。您可以使用語句檢查用戶的配置。

SELECT * FROM information_schema.USER_PRIVILEGES; 

如果您已授予用戶新的權限,則必須重新加載設置。爲此,您可以使用此FLUSH PRIVILEGES;

如何授予不同的用戶權限:

  • ALL特權─如我們前面看到的,這將允許MySQL用戶 所有訪問指定的數據庫(如果沒有數據庫被選中, 跨系統)
  • CREATE-使他們能夠創建新表或數據庫
  • 掇允許它們,將它們刪除表或數據庫
  • 刪除 - 允許他們從表中插入 - 允許他們 插入行到表中刪除行
  • 選擇 - 允許他們使用選擇命令 通過數據庫讀取
  • 更新 - 允許他們更新錶行
  • GRANT方案 - 允許他們授予或刪除其他用戶的權限

爲用戶提供一個允許特定的用戶,你可以使用這個框架:

GRANT [type of permission] ON [database name].[table name] TO ‘[username]’@'localhost’; 

我希望這會幫助你解決你的問題。永遠不要忘記沖洗!

+0

正如我在我的問題中提到的用戶具有所有權限,但仍然嘗試登錄時通過Java程序訪問被拒絕。 – iColdBeZero

+1

@iColdBeZero可以使用用戶解析器登錄並查看列表中的數據庫嗎? 如果你通過mysql命令行控制你的數據庫,那麼你的java代碼有問題。正如我提到的那樣,您是否沖洗了您的設置 – Noob

+0

是的,我可以登錄,是的,我刷新了特權,Java代碼不應該是一個問題,我只是不知道該怎麼做。 – iColdBeZero