Create a New User and Grant Permissions in MySQL

MySQL is an open-source database management software that helps users store, organize, and later retrieve data. It has a variety of options to grant specific users nuanced permissions within the tables and databases—this tutorial will give a short overview of a few of the many options.

How to Create a New User

We did all of the editings in MySQL as the root user, with full access to all of the databases. However, in cases where more restrictions may be required, there are ways to create users with custom permissions.

Let’s start by making a new user within the MySQL shell:

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';

At this point, the new user has no permissions to do anything with the databases. In fact, even if the new user tries to log in (with the password, password), they will not be able to reach the MySQL shell.

Therefore, the first thing to do is to provide the user with access to the information they will need.

GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';
FLUSH PRIVILEGES;

How To Grant Different User Permissions

Here is a shortlist of other common possible permissions that users can enjoy.

  • ALL PRIVILEGES– as we saw previously, this would allow a MySQL user full access to a designated database (or if no database is selected, global access across the system)
  • CREATE– allows them to create new tables or databases
  • DROP– allows them to them to delete tables or databases
  • DELETE– allows them to delete rows from tables
  • INSERT– allows them to insert rows into tables
  • SELECT– allows them to use the SELECT command to read through databases
  • UPDATE– allow them to update table rows
  • GRANT OPTION- allows them to grant or remove other users’ privileges

To provide a specific user with permission, you can use this framework:

GRANT type_of_permission ON database_name.table_name TO 'username'@'localhost';

You can review a user’s current permissions by running the following:

SHOW GRANTS FOR 'username'@'localhost';

Just as you can delete databases with DROP, you can use DROP to delete a user altogether:

DROP USER 'username'@'localhost';

To test out your new user, log out by typing:

quit

and log back in with this command in the terminal:

mysql -u [username] -p

Conclusion

After completing this tutorial, you should have a sense of how to add new users and grant them a variety of permissions in a MySQL database. 

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post

Install and Use PostgreSQL on Ubuntu 18.04

Next Post

Set Up SSH Keys

Related Posts
Total
0
Share