2017-02-13 101 views
0

我試圖連接到遠程MySQL服務器在Java中,從IP 111.111.111.111到ip 111.111.111.222使用JDBC連接器:設置MySQL用戶主機

conn = DriverManager.getConnection("jdbc:mysql://"+serverAddress+":3306/" + this.dBName + "?" + "user=" + this.userName + "&password=" + this.password + ""); 
  • [email protected] .111.111在數據庫中創建,GRANTS工作正常。
  • user @ localhost在數據庫中不存在,因爲我認爲這不是必須的。
  • 防火牆允許連接。

出於某種原因,當我嘗試啓動生成異常的連接:

java.sql.SQLException: The user specified as a definer ('user'@'localhost') does not exist 

但我請求從IP 111.111.111.111連接

注:當我在用戶名中輸入@表示正確的主機IP,但帶有一個額外的@,如下所示:

java.sql.SQLException: Access denied for user '[email protected]'@'111.111.111.111' (using password: YES) 

那麼,我在做什麼錯了?或者如何設置mysql用戶名的主機?

編輯:

而且我已經通過終端測試的MySQL連接,並能正常工作:(所以@ SSS IP 111.111.111.111是)

[email protected]:~$ mysql -h 111.111.111.222 -u user -p dbname 
Enter password: 
Reading table information for completion of table and column names 
You can turn off this feature to get a quicker startup with -A 

Welcome to the MySQL monitor. Commands end with ; or \g. 
Your MySQL connection id is 85 
Server version: 5.7.17 MySQL Community Server (GPL) 

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. 

Oracle is a registered trademark of Oracle Corporation and/or its 
affiliates. Other names may be trademarks of their respective 
owners. 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 

mysql> show tables; 
+---------------------+ 
| Tables_in_dbname | 
+---------------------+ 
| wh_with_customer | 
+---------------------+ 
10 rows in set (0,00 sec) 
+1

'user'簡化版,有權限與MySQL連接。您必須授予'用戶'權限 – haifzhan

+0

「,因爲我認爲這不是必須的。」 - 檢查你的假設。 – duffymo

+0

@duffymo因爲我只是遠程連接到數據庫。那麼它是否必要? –

回答

1

你沒有授予權限到user從我的角度

GRANT ALL PRIVILEGES ON *.* TO 'user'@'111.111.111.111' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION; 

執行它,喲取代PASSWORD你真正的密碼。

或者,嘗試使用DriverManager.getConnection(String url, String user, String password)以外

DriverManager.getConnection("jdbc:mysql://"+serverAddress+":3306/" + this.dBName + "?" + "user=" + this.userName + "&password=" + this.password + ""); 

DriverManager.getConnection(String url)預計URL中jdbc:subprotocol:subname格式,getConnection(url, username, password)更精確

+0

這是結果: 'show grants - >; + ------------------------------------------------ -------------------------------------------------- ---- + |撥款給[email protected]% | + ------------------------------------------------ -------------------------------------------------- - + |授予用戶*。*至'user'@'111.111.111.%'| | GRANT SELECT,INSERT,UPDATE,DELETE,EXECUTE,SHOW VIEW ON'dbname'。* TO'user'@'111.111.111.%'| 2排在組(0,00秒)' –

+0

和我只是試圖用'參數conn = \t \t \t \t \t的DriverManager.getConnection( 「JDBC:MySQL的://」 + serverAddress + 「:3306 /」 +此。 dBName +「」,this.userName +「」,this.password);'但它仍然顯示'java.sql.SQLException:用戶指定爲定義者('user'@'localhost')不存在' 但是我不是從本地主機請求,而是從不同的IP請求。 謝謝@HaifengZhang –

+0

運行'授予* *'用戶'@'本地主機'授予所有特權'用'授權'密碼'選項'和'FLUSH特權'的密碼 – haifzhan