连接到MySQL数据库Java [英] Connect to mysql database java
问题描述
我正在尝试从Java连接到MySQL数据库(MySQL托管在WAMP服务器中)
I am trying to connect to MySQL database from Java (MySQL is hosted in WAMP server)
String userName = "root";
String password = "pass";
String url = "jdbc:mysql://localhost/dbase";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection (url, userName, password);
当我从本地主机运行时,连接很好.但是,当我从另一台计算机(在同一网络内)将localhost替换为计算机的IP(在同一网络中)运行此代码时,出现错误
The connection is fine when I am running from localhost. However when I run this code from another computer replacing localhost with my computer's IP (within the same network), I get the error,
message from server: "Host '<name>' is not allowed to connect to this MySQL server"
我也尝试使用端口3306.怎么了?
I have tried with port 3306 too. Whats wrong?
推荐答案
这是数据库方面的权限问题;您需要授予用户root
的权限以通过您的特定IP地址进行连接.
That's a permission issue on the database side; you need to grant permissions to user root
to connect from your specific IP address.
类似的东西应该可以工作:
Something like this should work:
GRANT ALL ON foo.* TO root@'1.2.3.4' IDENTIFIED BY 'PASSWORD';
另一方面;我不会使用root
来访问数据库;您应该为此使用常规用户帐户.
On the other hand; I wouldn't use root
for access to the database; you should use a regular user account for this.
这篇关于连接到MySQL数据库Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!