连接到远程数据库时出现问题

时间:2011-08-12 13:37:18

标签: java mysql jdbc

我想访问远程数据库(Mysql),但总是发生错误: 什么可能是错误?

try{
Class.forName("com.mysql.jdbc.Driver");         
String url = "jdbc:mysql://@IP:3306/jira313";
String user = "jirauser";
String passwd = "jirauser";
Connection conn = DriverManager.getConnection(url, user, passwd);
}catch (Exception e) {
e.printStackTrace();
 }

错误是:

java.sql.SQLException: Access denied for user 'jirauser'@'MyIP' (using password: NO)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:910)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3923)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1273)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2031)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at javaapplication1.connexion.main(connexion.java:29)

5 个答案:

答案 0 :(得分:2)

您的问题的答案在您的例外中:

java.sql.SQLException: Access denied for user 'jirauser'@'MyIP' (using password: NO)

登录数据库,如果您有权访问并尝试:

GRANT ALL ON YOUR_DATABASE.* TO jirauser@'YOUR_IP' IDENTIFIED BY 'YOUR_PASSWORD';

答案 1 :(得分:1)

您的用户可能是本地用户,请先尝试使用mysql客户端。如果它也提供错误修改用户权限,以便能够从远程连接。

答案 2 :(得分:1)

检查您的代码,错误表明未指定密码。

答案 3 :(得分:0)

您无权访问远程mysql服务器。 转到服务器上,登录MySql并运行GRANT ALL PRIVILEGES ON *.* TO 'jirauser'@'%'(这将为您提供完全访问权限 - &gt;您应首先阅读有关权限的信息并添加您需要的权限adding users to MySql

答案 4 :(得分:0)

设置mysql以允许用户进行远程连接。

grant <permission> on <database> to <user>@<location> identified by <password>