简体   繁体   中英

unable to access mysql database from wamp server online

i'm able to use phpmyadmin online using server ip address from myclient ..... i'm trying to retrive data from mysql using java code

here is my code

 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package wamp; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * * @author user */ public class Wamp { /** * @param args the command line arguments */ // TODO code application logic here public static void main(String args[]) throws InstantiationException, IllegalAccessException { try{ Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection con = DriverManager.getConnection("jdbc:mysql://serverip:3306/test","root",""); Statement stmt=con.createStatement(); // stmt.executeUpdate("Insert into student values(1,'abc','nagpur')"); ResultSet rs= stmt.executeQuery("Select names from sample where id=15"); rs.next(); String name= rs.getString("names"); System.out.println(name); System.out.println("DOne.."); //INSERT INTO `student`(`id`, `name`, `address`) VALUES (1,'amol','nagpur'); con.close(); } catch(ClassNotFoundException | SQLException e) { System.out.println("error"+e); } } } 

i'm geting the following error :

errorjava.sql.SQLException: null, message from server: "Host 'user-PC.Home' is not allowed to connect to this MySQL server"

MySQL userid's have a HOST association.

So the complete story of a MySQL Userid says : This userid is allowed to connect with this password and only from this client or clients, client(s) being the HOST from which it is allowed to connect from.

By default the 'root' userid's are only allowed to connect from the machine running the MySQL server is running on. Its a security thing, you would not want your Super user being used from anywhere, especially if you have not even given it a password.

What you should do is create a new userid, lets say 'test', give it a password, lets say 'Testing$' then make sure that that userid only has access to the single database that it will be used for.

If you want to be lazy then you can set % as the host and that saya its allowed to connect from anywhere. Alternatively you can use a subnet mask, lets say your client PC's IP address is 192.168.0.100 you can allow the userid to login from either that single ip address by using 192.168.0.100 or 192.168.0 whicg says its allowed access from any ip on that subnet.

I hope this gets you started.

Here is the MySQL documentation for Adding Users

If you are sure that the user does not harm your data, Just give all Privileges to the user root.

Perform the following actions

1) Goto phpMyAdmin by clicking on the W icon on your taskbar

2) Goto the Users tab. You will find 5 entries with three of them are named root remaining two as Any

3) On both of the users named Any click on Edit Privileges

4) Check the Checkbox named Global privileges Check all. Perform this for both users named Any . And hit Go

5) Restart the Wamp

6) Use the port 3306 for mysql access

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.

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