简体   繁体   English

用Java连接到MySQL数据库错误

[英]Connecting to MySQL database error in Java

I have a java code and I am connecting to the mysql database with the following connection string. 我有一个Java代码,并使用以下连接字符串连接到mysql数据库。

 String userName = "admin";
 String password = "pass";
 String url = "jdbc:mysql://<my IP>/dbase"; //not localhost
 Class.forName ("com.mysql.jdbc.Driver").newInstance ();
 conn = DriverManager.getConnection (url, userName, password);

When I make a JAR (Runnable JAR through eclipse), and take it to another machine in the same network I get an error 当我制作一个JAR(通过Eclipse运行可运行的JAR)并将其带到同一网络中的另一台机器时,出现错误

Access denied for user 'admin'@'<another machine IP' (using password: YES) //not localhost

The IP magically changed to another machine IP when I take the JAR to another machine. 当我将JAR转移到另一台计算机时,IP神奇地更改为另一台计算机IP。 admin user has all privileges possible. admin用户拥有所有可能的权限。

Whats wrong? 怎么了? Please help !! 请帮忙 !!

The IP address listed in the error message is the IP address of the machine your program is running on . 错误消息中列出的IP地址是您的程序在其上运行的计算机的IP地址 It's telling you that you're not allowed to connect to MySQL from that IP address as root 告诉您,不允许您以root身份该IP地址连接到MySQL

You will need to talk to the person who configures/administers your MySQL database. 您将需要与配置/管理MySQL数据库的人员交谈。 More than likely this is an intentional security measure. 这很有可能是一种故意的安全措施。

Maybe you should run the below command. 也许您应该运行以下命令。

grant all privileges on *.* to 'admin'%'@' identified by 'pass';
flush privilges;

I think There is no problem with your Java code. 我认为您的Java代码没有问题。 Also try to connect the mysql server from command prompt 也尝试从命令提示符连接mysql服务器

mysql -u root -h <ip address> -p

If you got the same error then the possible reason would be 1. In correct password 2. Permission is denied to access via network 如果您遇到相同的错误,则可能的原因是1.使用正确的密码2.拒绝通过网络访问权限

You can give permission using below command 您可以使用以下命令授予权限

GRANT ALL ON db.* to root@'ip' IDENTIFIED BY 'PASSWORD';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM