views:

31

answers:

2

I'm using MySQL and a web-service connect to the database to read some values.
The web-service and the database are on the same server.
I want to deny access to the database through the public static IP of the server and the port of MySQL. That means only allow access to the database through localhost, so that only the web-service can connect to it, How to do so ? What configuration for example in MySQL should i do ?

+3  A: 

It's not possible to restrict access to mySQL to specific applications as such.

You can, however, create a user account (e.g. named webservice) that is restricted to connect from 127.0.0.1 - that's the best you can do as far as I know, and should be totally sufficient.

Pekka
Can't we disable anonymous logins to mysql DB (i mean by anonymous through the public IP) ? and grant login to the DB through 127.0.0.1 (localhost) only ?
Ashraf Bashir
@Ashraf yes, that is exactly what I'm talking about. You just need to `GRANT` the correct permissions. Another thing would be to configure your firewall so requests from outside don't get through at all.
Pekka
That is exactly what Pekka (and myself) are saying. You can restrict access to connections from `localhost`, but if there was another program running locally, you cannot stop it accessing the database (assuming it has the correct password/username...)
Stephen
Thanks Pekka and Stephen
Ashraf Bashir
+2  A: 

There is no way to restrict access to only the web-service. You can restrict it to just applications running on the same host. To do this, create a new user with a host of either 127.0.0.1, or localhost should also work. You can either do this graphically or through the command line:

CREATE USER 'webservice'@'localhost' IDENTIFIED BY 'webservicepassword';
// Grant privileges here... 
// For example, GRANT ALL PRIVILEGES ON *.* TO 'webservice'@'localhost' - but it's a far better idea to restrict access to only what it needs...
Stephen
create user is unnecessary. just GRANT query will do everything
Col. Shrapnel