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);
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"
I have tried with port 3306 too. Whats wrong?
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';
On the other hand; I wouldn't use root
for access to the database; you should use a regular user account for this.
You should not use the root account for development. To solve your problem lets create a hypothetical user called dbasedev .
CREATE USER 'dbasedev'@'%' IDENTIFIED BY 'passw0rd!';
GRANT ALL PRIVILEGES ON *.* TO 'dbasedev'@'%';
FLUSH PRIVILEGES;
This will allow you to connect to the MySQL server with user id: dbasedev
, password: passw0rd!
, from any host.
This is a complement to @Icarus's answer, I couldn't post all the code in a comment.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.