How to prevent a user from accessing a specific schema in MySQL?


To prevent a user from accessing a specific schema, you need to use delete command. Following is the syntax −

DELETE FROM mysql.db WHERE Db IN("yourSpecificSchema","yourSpecificSchema\_%")
   AND User = "yourUserName" AND Host = "yourHostName";

Let us implement the above syntax to prevent a user from accessing a specific schema. First of all, let us display all users and host from MySQL.user table.

mysql> select user,host from MySQL.user;

This will produce the following output −

+------------------+-----------+
| user             | host      |
+------------------+-----------+
| Bob              | %         |
| User2            | %         |
| mysql.infoschema | %         |
| mysql.session    | %         |
| mysql.sys        | %         |
| root             | %         |
| @UserName@       | localhost |
| Adam Smith       | localhost |
| Chris            | localhost |
| David            | localhost |
| James            | localhost |
| John             | localhost |
| John Doe         | localhost |
| User1            | localhost |
| am               | localhost |
| hbstudent        | localhost |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
+------------------+-----------+
18 rows in set (0.00 sec)

Here, we will prevent user ‘Chris’ with host name ‘localhost’ from accessing a specific schema which has the name ‘sample’ −

mysql> DELETE FROM mysql.db WHERE Db IN("sample","sample\_%") AND User="Chris" AND Host="localhost";
Query OK, 0 rows affected (0.00 sec)

After executing the above query, you need to use FLUSH.

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.06 sec)

Updated on: 30-Jul-2019

373 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements