Skip to content

MySQL User Permissions

New Course Coming Soon:

Get Really Good at Git

A quick introduction at User Permissions in a MySQL Database

Let’s see how to grant permissions (called privileges) to a user of the MySQL database

By default when you create a new MySQL user using the syntax

CREATE USER '<username>'@'localhost' IDENTIFIED BY '<password>';

the user cannot do much. We can say that it can’t to anything, actually.

It can’t read data from any existing database, let alone modifying the data. And it can’t even create a new database.

To make a user do anything, you have to grant privileges to it.

You can do so using the GRANT command.

We can use GRANT <permission>, using the following permission keywords:

Give privilege to create new databases to a user

GRANT CREATE ON *.* TO '<username>'@'localhost';

Give privileges to a user to create new tables in a specific database

GRANT CREATE ON <database>.* TO '<username>'@'localhost';

Give privilege to read (query) a specific database to a user

GRANT SELECT ON <database>.* TO '<username>'@'localhost';

Give privilege to read a specific database table to a user

GRANT SELECT ON <database>.<table> TO '<username>'@'localhost';

Give privilege to insert, update and delete rows in a specific database to a user

GRANT INSERT, UPDATE, DELETE ON <database>.* TO '<username>'@'localhost';

Give privilege to delete tables in a specific database to a user

GRANT DROP ON <database>.* TO '<username>'@'localhost';

Give privilege to delete databases to a user

GRANT DROP ON *.* TO '<username>'@'localhost';

Give all privilege on a specific database to a user

GRANT ALL PRIVILEGES ON <database>.* TO '<username>'@'localhost';

Give all privileges to a user

GRANT ALL PRIVILEGES ON *.* TO '<username>'@'localhost';

Revoke a privilege

Example to revoke the DROP privilege on <database>:

REVOKE DROP ON <database>.* TO '<username>'@'localhost';

To revoke all privileges, run:

REVOKE ALL PRIVILEGES ON *.* TO '<username>'@'localhost';

You can visualize the privileges of a single user by running:

SHOW GRANTS FOR '<username>'@'localhost';
Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching Summer 2024. Join the waiting list!
→ Read my SQL Tutorial on The Valley of Code

Here is how can I help you: