2017-08-12 78 views
-1

我在Ubuntu 14.04機器上安裝了MySQL。我可以從命令行以root身份登錄。但是當我運行下面的Java程序時,我收到了拒絕訪問異常。什麼問題?MySQL拒絕我從Java程序訪問但不能從命令行登錄

import java.sql.Connection; 
import java.sql.DriverManager; 

public class Conn { 
    public static void main(String[] args) throws Exception { 
     Class.forName("com.mysql.jdbc.Driver"); 
     Connection conn = DriverManager.getConnection("jdbc:mysql://" 
      + "localhost" + "/xi_tong?characterEncoding=utf8", "root", 
      "****"); 
    } 
} 

例外:

Exception in thread "main" java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3970) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3906) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:873) 
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1710) 
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1226) 
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2253) 
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284) 
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2083) 
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:806) 
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) 
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410) 
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328) 
    at java.sql.DriverManager.getConnection(DriverManager.java:571) 
    at java.sql.DriverManager.getConnection(DriverManager.java:215) 
    at Conn.main(Conn.java:7) 

我可以在命令行登錄:

[email protected]:~$ mysql -u root -p 
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g. 
Your MySQL connection id is 104 
Server version: 5.5.55-0ubuntu0.14.04.1 (Ubuntu) 

Copyright (c) 2000, 2017, 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> 

另一臺機器上:

[email protected]:~/Server/src$ java -cp .:mysql-connector-java-5.1.40-bin.jar Conn 
[email protected]:~/Server/src$ 
+0

用密碼替換**「****」** – Ali

+0

@Ali:出於安全原因,我用****替換了真實密碼。密碼不是問題,因爲我可以從shell登錄。 –

+0

我認爲你錯了東西,請檢查你的代碼或重建你的項目並運行 –

回答

0

問題是與特權! The problem is with privileges!

1

確保數據庫存在。

USE yourdb; 

我最近有一個這樣的問題,其中dbname輸入錯誤。

+0

我認爲............這樣的事情會幫助他 –

+0

我故意將DB名稱改爲錯誤的名稱,但仍然出現此錯誤。數據庫沒有進入數據庫選擇步驟。這是一個「拒絕訪問」的錯誤。我的原始數據庫名稱是正確的。 「使用xi_tong」返回沒有錯誤。 –

相關問題