代码之家  ›  专栏  ›  技术社区  ›  Zirui Wang

MySQL拒绝了我从Java程序而不是从命令行的访问

  •  -1
  • Zirui Wang  · 技术社区  · 7 年前

    我在Ubuntu 14.04机器上安装了MySQL。我可以从命令行用root登录。但是当我运行下面的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)
    

    zirui@52:~$ 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>
    

    在另一台机器上:

    zirui@45:~/Server/src$ java -cp .:mysql-connector-java-5.1.40-bin.jar Conn
    zirui@45:~/Server/src$
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   MrViK    7 年前

    USE yourdb; 
    

    我最近遇到了这样一个问题,dbname输入错误。

        2
  •  1
  •   Zirui Wang    7 年前

    问题在于特权! The problem is with privileges!