How to create MySQL user with limited privileges?

MySQLMySQLi Database

To create MySQL user with limited privileges, following is the syntax −

CREATE USER 'yourUserName'@'yourHostName' IDENTIFIED BY 'yourPassword';

Following is the syntax to set limited privileges for user −

GRANT SELECT, INSERT, UPDATE, etc. REFERENCES
ON yourDatabaseName.* TO 'yourUserName'@'yourHostName';

Let us implement the above syntaxes in order to create a MySQL user with limited privileges −

mysql> CREATE USER 'David'@'localhost' IDENTIFIED BY 'david';
Query OK, 0 rows affected (0.20 sec)
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, ALTER, CREATE, REFERENCES
   ON test.* TO 'David'@'localhost';
Query OK, 0 rows affected (0.21 sec)

Let us check the user has been created with name ‘David’ or not.

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 |
| David            | localhost |
| James            | localhost |
| John             | localhost |
| John Doe         | localhost |
| User1            | localhost |
| am               | localhost |
| hbstudent        | localhost |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
+------------------+-----------+
17 rows in set (0.00 sec)
raja
Published on 29-Apr-2019 12:38:35
Advertisements