How To Grant and Revoke Privileges in MySQL
How To Grant and Revoke Privileges in MySQL
Privileges in MySQL
When it comes to keeping databases and data secure, it is recommended to use privileges. They
can help you control who can access database data and do not allow users without specified
permissions to view, modify, or delete database objects or data.
In the article, we'll explore how to grant and revoke privileges on any level using
the GRANT and REVOKE commands and dbForge Studio for MySQL, a powerful GUI tool for
database development, management, testing, and deployment.
You can assign a privilege to the user account using the GRANT statement.
GRANT privilege
ON privilege_level TO account_name;
where:
privilege is an operation that the user account will be allowed to perform, such as SELECT,
INSERT, DROP, etc. If you want to grant multiple privileges, separate them by commas.
privilege_level is the level of access, such as global, database, or database object, to which
the privileges can be applied.
account_name is the username to which the privileges will be granted.
MySQL supports the following types of privileges to access and control the database:
Global
They can be used to manage all databases located on the current MySQL server and control
administrative tasks, such as creating or deleting databases, managing users, and setting server
parameters. To assign the global privilege, use the following syntax:
GRANT privilege
ON *.* TO username;
Database
They give permissions to manage all objects from the specified database and determine what
operations users can perform within those databases. To assign the database-level privilege, use
the following syntax:
GRANT privilege
ON database_name.* TO username;
Table
They provide users with the right to manage rows from the specified database table and
determine what operations users can perform on those tables. To assign the table-level privilege,
use the following syntax:
GRANT privilege
ON database_name.table_name TO username;
Column
They allow users to perform the defined operations, for example, SELECT or DELETE, for the
specified columns of the database table. Note that the column must be specified for each
privilege. To assign the column-level privilege, use the following syntax:
GRANT privilege (column1), privilege (column2)
ON table_name TO username;
Stored routine
They control access to stored procedures and functions within the database, including the ability
to execute, alter, or drop them. To assign the stored routine privilege, use the following syntax:
GRANT privilege
Proxy user
They allow one user to be a proxy for another. The proxy user receives all the privileges of the
proxied user. To assign the proxy user privilege, use the following syntax:
GRANT PROXY
ON proxied_user TO proxy_user;
So, managing privileges ensures that data access is restricted to authorized users, helps
organizations protect their sensitive information, and maintains compliance with regulatory
requirements.
SHOW GRANTS;
For example, you can execute the SHOW GRANTS command to list all global privileges for the
user - root on the dbfmylast server.
You can also see the privileges granted to the specific user account from which the user can be
connected. In such cases, if the current server and the hostname of the user whose permissions
you want to check differ, it is recommended to specify the hostname or IP address in the
command. To show the privileges, execute the following command:
Let us consider the following example. The command SHOW GRANTS FOR
'root'@'localhost'; displays the privileges granted to the user account root when connecting from
the hostname 'localhost'. These privileges may include permissions to perform different actions,
such as accessing specific databases, executing certain SQL queries (for example, SELECT,
INSERT, UPDATE, DELETE, etc.), creating or dropping databases and tables, etc.
In the command, replace the following parameters with your actual data:
username is the username of the user whose privileges you want to view.
hostname is the hostname or IP address from which the user is connecting.
However, it should be noted that you need to have administrative privileges or at least the
SELECT privilege on the mysql system database.
Another way to show privileges granted to all users that exist in a MySQL database is to
query user_privileges in information_schema.
SELECT * FROM information_schema.user_privileges;
When you execute this command, it will return a result set containing the user account to whom
the permissions were granted, a host, a privilege type, and information about whether the user
has the privilege to grant the same privilege to other users. In the latter, if
the IS_GRANTABLE column has the YES value, the user has the privilege to assign the same
privilege to other users. In other words, the user has the right to manage access permissions for
other users. If the value is NO, the user does not have permission to assign the same privilege to
others.
Grant all privileges in MySQL
Now, we'll go through some examples of how to grant privileges to users in MySQL. For
instance, you need to provide a specific user with full access and all permissions over all
databases, so only this person can have complete ownership to manage databases, tables, access,
functions, etc. You can achieve this using the GRANT ALL PRIVILEGES statement:
WITH GRANT OPTION allows a user to share or remove permissions from other users.
When you give someone permission using the WITH GRANT OPTION clause, they can
share those privileges with other users. But if you don't include WITH GRANT OPTION,
users can't transfer those permissions further, even if they have been given all permissions.
PROXY allows a user to connect as another user without providing the password for that
user. This privilege is not granted by default with ALL PRIVILEGES. It should be assigned
explicitly if required.
So, to assign the WITH GRANT OPTION or PROXY privileges along with ALL PRIVILEGES,
you would need to include them explicitly in the GRANT statement as follows:
If you want to give all privileges over the specified database, modify the statement as follows:
You can also provide full ownership over one table from the database using the following
statement:
For example, assign all privileges over the sakila database to the user - jordansanders:
Then, verify that the permissions were granted running the SHOW GRANTS FOR statement:
SHOW GRANTS FOR jordansanders;
The results grid displays all the permissions available for the user jordansanders on
the sakila database:
Don't forget to replace the following parameters with your actual data:
database_name is the name of the database to which you want to grant read-only access. If
you want to do this for all databases, use *.*.
username is the username of the account.
hostname is the hostname or IP address from which the user will connect.
In the output, you'll see the permissions the user has, including the read-only access privilege.
Grant privileges using a GUI tool
We have demonstrated how you can give permissions using the GRANT statement. Now, we
would like to show the easiest method to assign permissions using Security Manager available
in dbForge Studio For MySQL.
Security Manager is an advanced administration tool that allows you to create, update, or delete
logins, users, and roles, manipulate user accounts, assign or modify roles visually, grant object
permissions, and revoke user privileges and access rights.
Security Manager allows global and object privileges to be granted. Let us now see how to grant
permissions on the database object. Begin by opening the Studio.
1. On the ribbon, select Database > Security Manager. This will open the Security Manager.
2. Select the user you want to grant permissions to and go to the Object Privileges tab.
3. In the Objects tree, select the database object for which you want to set permissions.
4. In the Available Privileges pane, a list of privileges you can assign is displayed - select the
checkboxes next to the required privileges.
Note that the grid under the Objects tree displays the current privileges of the selected user
(account).
In MySQL, the REVOKE command can be used to remove privileges from a user account. The
syntax of the command is as follows:
where:
*.* means that the privileges will be removed from any database and any database object.
user_name is the user account from which you want to remove privileges.
hostname is the hostname or IP address from which the specified user is connecting.
After executing this command, the user will no longer have any privileges within the MySQL
server. However, it is important to note that this command does not remove the user account
itself; it only deletes the privileges associated with the user account.
Let us revoke all privileges for the user jordansanders we granted before, namely, the read-only
access to the adventureworks database and all privileges to the sakila database. To do this,
execute the following query:
Then, run the SHOW GRANTS FOR query to verify that the privileges have been removed:
As you can see, the previously assigned privileges have been revoked. Although the GRANT
USAGE ON *.* TO 'jordansanders'@'%' is displayed, it only grants the user
account jordansanders the permission to connect to the MySQL server from any host ('%'); it
does not give any specific privileges on any databases or tables. In other words, it allows the user
to establish a connection to the MySQL server without any additional permissions.
where:
privileges specifies the privileges you want to revoke from the user. It could be a single
privilege or a comma-separated list of multiple privileges - for example, SELECT,
INSERT, UPDATE, DELETE, etc.
database_name specifies the database from which you want to revoke privileges.
user_name is the user account from which you want to remove privileges.
hostname is the hostname or IP address from which the specified user is connecting.
Let us take the user manager as an example and view the privileges it has been assigned.
As you can see, the user has been granted all privileges on the adventureworks database and the
SELECT, INSERT, and UPDATE privileges on the customer table of the sakila database.
Now, execute the command to remove a privilege from the adventureworks database:
Finally, remove the SELECT, INSERT, and UPDATE privileges for the customer table in
the sakila database for the user manager.
Take advantage of the user-friendly GUI for managing privileges, which can be helpful for
those users who are not familiar with SQL syntax or who prefer a visual approach to
privilege management.
Minimize the number of errors when granting and revoking privileges in Security Manager
instead of typing SQL commands manually.
Improve accuracy and efficiency in privilege management tasks by visually selecting
specific databases, tables, or columns when granting or revoking privileges.
Manage privileges using role-based access control, which allows administrators to define
roles with specific sets of privileges and assign users to these roles.
Automate routine MySQL and MariaDB admin tasks.
Conclusion
In this guide, we examined how to regulate the level of access granted to user accounts across
databases or database objects. These privileges can be assigned globally, to databases, or to
specific database objects. We have also discussed the GRANT command as a way to assign user
account privileges, thus enhancing their access levels, and the REVOKE command, which
illustrates how to revoke privileges granted to accounts. In addition to using these commands, we
demonstrated the easiest way to grant and revoke permissions - dbForge Studio for MySQL, an
ultimate IDE for MySQL and MariaDB database administration and management. This GUI tool
can offer a more user-friendly, efficient, and integrated approach to privilege management. So,
try the Studio and evaluate its cutting-edge features and capabilities within a free 30-day trial
period.
I have only found a place where I can give rights to single users.
What I need is granting some rights to several users at the same time like
Share
Edit
Follow
Get updates on questions and answers
edited May 4, 2015 at 20:40
3 Answers
Sorted by:
Highest score (default)
9
You can do that. Follow the pictures: In the "Privileges" tab which list your users
click the "Edit Privileges" of the
user.
Share
Edit
Follow
answered Jan 6, 2014 at 16:29
shgnInc
1,99644 gold badges2222 silver badges2929 bronze badges
Yes, this gives rights to a single user, but not to several users as mentioned
in my question. I don't want to do this over and over again for every single
user I want to give rights to. It would be ok if MySQL understood the concept
of roles, then I could give a role to a bunch of users and then grant the rights
to that role. Unfortunately, MySQL lacks this important feature. So I will to it
rather using the command line.
– Holger Jakobs
CommentedJan 7, 2014 at 10:30
1
Depending on your host, this might not be an option in your phpMyAdmin
interface either.
– artomason
CommentedMar 28, 2019 at 21:20
I don't see an option for table.
– khatchad
CommentedFeb 14, 2021 at 0:12
Add a comment
1
phpMyAdmin can't do such a granular rights setup, but you can do this with pure
SQL. See this reference for details. It's basically just writing "GRANT" statements
yourself which you can pass to phpMyAdmin.
The GRANT syntax allows you to specify multiple users in a single query.:
GRANT SELECT,INSERT,UPDATE,DELETE ON customers.* TO 'john', 'annie', 'max';
With PHP my admin, anything you do shows you the query that it ran to do it at
the top of the page. Copy these querys. Switch out the username and and host
and rerun them.
Not GUI but if doing several users then you just do it through the GUI once and a
quick copy paste for each user.
Another option for creating accounts is to use the GUI tool MySQL Workbench. Also, several third-
party programs offer capabilities for MySQL account administration. phpMyAdmin is one such
program.
This section discusses the following topics:
Two accounts have a user name of finley. Both are superuser accounts with full global
privileges to do anything. The 'finley'@'localhost' account can be used only when
connecting from the local host. The 'finley'@'%.example.com' account uses
the '%' wildcard in the host part, so it can be used to connect from any host in
the example.com domain.
The 'finley'@'localhost' account is necessary if there is an anonymous-user account
for localhost. Without the 'finley'@'localhost' account, that anonymous-user account
takes precedence when finley connects from the local host and finley is treated as an
anonymous user. The reason for this is that the anonymous-user account has a more
specific Host column value than the 'finley'@'%' account and thus comes earlier in
the user table sort order. (For information about user table sorting, see Section 8.2.6, “Access
Control, Stage 1: Connection Verification”.)
The 'admin'@'localhost' account can be used only by admin to connect from the local
host. It is granted the global RELOAD and PROCESS administrative privileges. These privileges
enable the admin user to execute the mysqladmin reload, mysqladmin refresh,
and mysqladmin flush-xxx commands, as well as mysqladmin processlist . No privileges
are granted for accessing any databases. You could add such privileges
using GRANT statements.
The 'dummy'@'localhost' account has no password (which is insecure and not
recommended). This account can be used only to connect from the local host. No privileges are
granted. It is assumed that you grant specific privileges to the account using GRANT statements.
The previous example grants privileges at the global level. The next example creates three accounts
and grants them access at lower levels; that is, to specific databases or objects within databases.
Each account has a user name of custom, but the host name parts differ:
CREATE USER 'custom'@'localhost'
IDENTIFIED BY 'password';
GRANT ALL
ON bankaccount.*
TO 'custom'@'localhost';
REVOKE ALL
ON *.*
FROM 'finley'@'%.example.com';
REVOKE RELOAD
ON *.*
FROM 'admin'@'localhost';
Revoke database-level privileges:
REVOKE CREATE,DROP
ON expenses.*
FROM 'custom'@'host47.example.com';
Revoke table-level privileges:
REVOKE INSERT,UPDATE,DELETE
ON customer.addresses
FROM 'custom'@'%.example.com';
To check the effect of privilege revocation, use SHOW GRANTS:
mysql> SHOW GRANTS FOR 'admin'@'localhost';
+---------------------------------------------+
| Grants for admin@localhost |
+---------------------------------------------+
| GRANT PROCESS ON *.* TO `admin`@`localhost` |
+---------------------------------------------+
Dropping Accounts
To remove an account, use the DROP USER statement. For example, to drop some of the accounts
created previously:
DROP USER 'finley'@'localhost';
DROP USER 'finley'@'%.example.com';
DROP USER 'admin'@'localhost';
DROP USER 'dummy'@'localhost';
Introduction
Prerequisites
How do privileges work in MySQL?
Conclusion
SHARE ON
Introduction
Privilege management is an important part of system and database
administration. Deciding who should have what access to which components
and powers and then designing an implementation that enables those
policies requires a good deal of thought and care.
Prerequisites
To follow along with this guide, you'll need an account on a MySQL server
with the appropriate privileges.
Required privileges
To manage privileges for MySQL users, you need to have the following
privileges:
GRANT OPTION: the GRANT OPTION privilege allows you to grant or revoke
any privilege that you have been granted
To follow along with this guide, we will assume that you are using an account
with full administrative privileges (including the GRANT OPTION privilege). This
could be the common 'root'@'localhost' user that is configured during
installation, or any other user with full privileges.
MySQL stores the information about which users have what privileges in a
number of different tables in the mysql system database. Here is a review the
where MySQL keeps different types of privilege information as was covered
in the introduction to MySQL authentication and authorization article:
user: The user table defines each user's static global privileges. These
privileges apply to the whole MySQL server and are not affected by the
availability of any plugins or components.
global_grants: The global_grants table defines each user's dynamic global
privileges. Any privileges defined by a plugin or component are registered
in this table.
columns_priv: A step further than the tables_priv table, the columns_priv table
determines access at the column level. To add this additional granularity,
a column called Column_name is included in addition to the columns
available within the tables_priv table.
MySQL defines many privileges appropriate for various system scopes. Some
of these are useful for everyday use and management of databases, tables,
and functions, while others are designed for administrative tasks
like replication, backups, and connection management.
You can find a comprehensive list of static privileges (core privileges built
into MySQL itself) and their respective scopes in the Permissible Static
Privileges for GRANT and REVOKE table in the MySQL documentation. The
related Static Privilege Descriptions section of the MySQL documentation
provides a detailed overview of what each privilege allows and in many
cases, guidance on what scenarios they would be most useful.
Dynamic privileges are the other type of privilege. Dynamic privileges are
defined in plugins or components and are registered with MySQL to enable
them. They are always global in scope and provide additional capabilities or
features. The Permissible Dynamic Privileges for GRANT and REVOKE table in
the MySQL documentation lists each dynamic privilege and its context. You
can find full descriptions of what each is used for in the associated Dynamic
Privilege Descriptions section of the MySQL documentation.
To find out which privileges are enabled and available on your MySQL server,
as well as the context in which they're relevant, you can use the following
command:
SHOW PRIVILEGES
This can help you understand what privileges are best suited for your users'
responsibilities.
You can always view the privileges granted to your own user by typing:
SHOW GRANTS;
+--------------------------------------------------------------------+
Grants for exampleuser@localhost |
+--------------------------------------------------------------------+
GRANT USAGE ON *.* TO `exampleuser`@`localhost` |
GRANT ALL PRIVILEGES ON `exampledb`.* TO `exampleuser`@`localhost` |
+--------------------------------------------------------------------+
2 rows in set (0.00 sec)
Here, we see that 'exampleuser'@'localhost' has two sets of privileges defined.
The first entry shows that it has been granted USAGE globally (indicated by
the wildcard <database>.<table> scope of *.*). Despite its name, USAGE in this
context actually means "no privileges are granted". So, by default, this user
hasn't been given any privileges. The second record shows that they have
been granted ALL PRIVILEGES, or complete access, to the exampledb database.
If the user account you are logged in as has SELECT privileges on the
internal mysql database, you can see the privileges granted to other user
accounts. To show the privileges of other accounts, use the following format:
Basic syntax
The <database>.<object> part of the syntax above dictates the scope where
the privileges will be granted. This will determine which objects the
privileges will be granted for and the specific table in the mysql database
where the new privileges will be recorded.
To grant a privilege globally, allowing a user account to use the privilege
throughout the entire system, use wildcards for both the database and
database object part of the scope component:
For example, to grant the ability to update the value of the due_by column in
the library.loans table, you can type:
It is important to realize that the WITH GRANT OPTION clause applies to the
account ('librarymanager'@'localhost') and the scope (library.*), not the specific
privileges in the statement. This means that although we've assigned four
new privileges to the 'librarymanager'@'localhost' account in this statement,
the WITH GRANT OPTION allows it to pass on any of its privileges at
the library.* scope. Since the account now has the GRANT OPTION for this
scope, if we give 'librarymanager'@'localhoast' additional privileges in the
future, it'll also be able to pass on those privileges automatically.
Although you can use the WITH GRANT OPTION clause as demonstrated above
to allow an account to pass on its privileges while you are giving them
additional privileges, it's often more clear if you separate these two actions,
like this:
When you handle GRANT OPTION as a regular privilege, you can also combine
it in the list of privileges you are assigning:
You can assign full privileges to a user at a specific scope using the ALL or ALL
PRIVILEGES shorthand:
This will grant every privilege that your user is capable of assigning on
the sales database to the 'salesadmin'@'localhost' user, with a couple important
exceptions. The ALL PRIVILEGES privilege bundle does not include the GRANT
OPTION or PROXY privileges, which must be assigned separately. This is to
make it easier to assign full privileges without passing on privilege
administration and user substitution privileges.
To assign all privileges except GRANT OPTION and PROXY globally, use
the *.* scope:
To assign full privileges and also give the user the ability to pass on any of its
privileges, include the GRANT OPTION in the statement. For example, to give
the 'salesadmin'@'localhost' account from the last example the ability to control
other users' access to the sales database, you could instead type:
This same logic can be applied globally using the *.* context. In this cases,
it'll make the given account a full administrative user:
Often, at the database or table level, you'll have some accounts that need to
be able to access information but should not have the ability to alter the
database or object in any way. These may include reporting tools or any
scenario where data needs to be accessible but not modifiable, like with
many non-interactive webpages.
The SELECT privilege is adequate to give the user read-only privileges on the
database or object. To give the 'salesreport'@'localhost' user read-only access
to the sales database, type:
This user will be able to query and extract any data it requires from
the sales database, but it cannot make any changes.
The typical companion to the read-only use case is the user who needs read
and write access. This type of access is appropriate for any processes that
need to manage the data within the database or the object. For instance, a
process that creates or edits website user profiles would need both read
and write privileges.
To assign read and write access to a user, grant them SELECT, INSERT, UPDATE,
and DELETE privileges on the object. For example:
Another common scenario is making an account that can only append data
to a table or other object. This way, the process always has additive
permissions to the object, but cannot rewrite or modify entries that are
already present. This can be useful for append-only event logging or
scenarios where updates are actually stored as new records to preserve
history.
If you want the account to selectively be able to update certain parts of the
record, you can additionally grant them UPDATE privileges on the appropriate
columns:
Basic syntax
The REVOKE command mirrors the GRANT command fairly closely. Aside from
the command name, you revoke privileges from an account rather than
granting them to the account.
Since privileges are tied to a specific scope (global, database, table, etc.),
the REVOKE command must specify the scope from which to remove the
privilege, just as you do when adding privileges.
To remove a privilege at the global level, use the *.* wildcard to match any
database and any database object:
And finally, to remove a privilege from a database object, name the database
and the object name separated by a dot:
It's a good idea to check the user's available privileges after revoking to
make sure that they do not still have unwanted access granted through any
other means:
For example, you can set up an account that has full privileges over the
database except for on the mysql database, which is used to store system
information like privileges, authentication details, and more for users. A
partial revoke would allow you to grant full privileges and then add a special
exception for that database.
To enable partial revocation in MySQL, you need to enable it. You can turn it
on persistently by typing the following in supported versions (MySQL 8.0.16
or later):
Now, to set up the user account described above, you could type:
Here, we've created a user and granted them full privileges for the entire
MySQL server. Afterwards, we revoke those privileges specifically in the
context of the mysql database. We then re-grant the SELECT privilege so that
the account can still read values from the database.
If you look at the privileges for this account, something similar to this will be
displayed:
Th