Implementing refresh permissions in MySQL
This tutorial explains the refresh permissions operation and its implementation with examples.
Implementing refresh permissions in MySQL
MySQL implements user management through grant tables to ensure security and access control in the server. Usually, grant tables are modified root 用户
directly through UPDATE
statements or indirectly through keywords.GRANT
However, modifying the grant tables directly requires a flush privileges operation or a restart/reload of the server to reflect the changes.
In MySQL, you can use three keywords to invoke the refresh privileges operation, which is more convenient and efficient for performing multiple changes to the server.
FLUSH PRIVILEGES
Order.mysqladmin flush-privileges
Order.mysqladmin reload
Order.
Grant tables in MySQL
Understanding the MySQL grant tables can provide a better context for refreshing privileges. As mentioned earlier, the grants table is a system table that stores information about various users and their privileges in a MySQL server connection.
To check permissions in the grant tables, use SHOW GRANTS
the keyword.
-- Showing Grant privileges for the current user
SHOW GRANTS FOR CURRENT_USER();
/* Showing Grant privileges for specific user
SHOW GRANTS FOR [USERNAME]
*/
The privileges listed in the grant tables describe the restrictions or permissions available to a user. Let's create a test_user
user named and mysql.user
view the privileges for this user in the grant table.
-- Creating a sample user
CREATE USER 'test_user'@'localhost' IDENTIFIED BY '20202010';
-- checking assigned privileges
SELECT * FROM mysql.user where user='test_user' \G;
In MySQL, use FLUSH PRIVILEGES
the keyword to commit direct permission changes to the server.
UPDATE
Statements flushprivilege
are combined with commands to grant privileges to users and reflect changes at the same time.
test_user
For example, let's grant read-only permission ( ) to all databases in the server SELECT
.
UPDATE mysql.user -- Directly modifying the user table
SET Select_priv = 'Y' -- Granting SELECT privilege for test_user
WHERE user = 'test_user';
SELECT * FROM mysql.user where user='test_user' \G; -- Viewing changes
Output:
*************************** 1. row ***************************
Host: localhost
User: test_user
Select_priv: Y <- This is the updated permission
Insert_priv: N
Update_priv: N
Delete_priv: N (OUTPUT HAS BEEN TRUNCATED)
The query results show that the user now has SELECT
permissions on all databases. However, this action has not yet been reflected in the server.
SHOW GRANTS FOR test_user@localhost; -- Viewing the grants for the user
Output:
+-----------------------------------------------+
| Grants for test_user@localhost |
+-----------------------------------------------+
| GRANT USAGE ON *.* TO `test_user`@`localhost` |
+-----------------------------------------------+
1 row in set (0.00 sec)
Now, let's perform the flush privileges operation.
FLUSH PRIVILEGES; -- This affects the changes made
SHOW GRANTS FOR test_user@localhost; -- Viewing the grants for the user, again
Output:
+------------------------------------------------+
| Grants for test_user@localhost |
+------------------------------------------------+
| GRANT SELECT ON *.* TO `test_user`@`localhost` |
+------------------------------------------------+
1 row in set (0.00 sec)
The same method can be used for other refresh permission commands, namely mysqladmin flush-privileges
and mysqladmin reload
.
The recommended way to modify user permissions is through GRANT
the command, as changes are automatically reflected without the need for a refresh permissions operation.
The difference between direct and indirect modification of the grant tables is explained in sufficient detail in the official documentation.
GRANT
Modify user privileges in MySQL using keywords
Let's give the test_user
SYSCONFDIRECT privilege to all databases and tables on the server INSERT
. This time, we use GRANT ON
the SYSCONFDIRECT command.
GRANT ON
The command syntax is as follows.
GRANT [privilege(s)] ON [Db_name . table_name] TO user
To specify all databases and tables, use the wildcard character *.*
instead [Db_name . table_name]
.
GRANT INSERT ON *.* TO test_user@localhost; -- Giving test_user Insert privileges
SHOW GRANTS FOR test_user@localhost; -- Checking for reflected changes
Output:
+--------------------------------------------------------+
| Grants for test_user@localhost |
+--------------------------------------------------------+
| GRANT SELECT, INSERT ON *.* TO `test_user`@`localhost` |
+--------------------------------------------------------+
1 row in set (0.00 sec)
As expected, the changes reflect the absence of FLUSH PRIVILEGES
the command.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
If ELSE in MySQL
Publish Date:2025/04/11 Views:85 Category:MySQL
-
In this tutorial, we aim to explore how to use IF ELSE the statement in MySQL. One of the key roles of a data analyst is to gather insights from the data and produce meaningful results. It can be done with the help of several data filtering
DATETIME vs. TIMESTAMP in MySQL
Publish Date:2025/04/11 Views:117 Category:MySQL
-
DATETIME and TIMESTAMP are two different data types that can be used to store values that must contain both a date and a time portion. In this article, we will understand how it is stored in the database and the memory required for ea
Execute multiple joins in one query in MYSQL
Publish Date:2025/04/11 Views:94 Category:MySQL
-
Have you ever wondered how to include multiple joins in one query in MySQL? You have come to the right place. Remember that joins allow us to access information from other tables. This information is included separately to avoid redundancy.
Joining 3 tables in MySQL
Publish Date:2025/04/11 Views:187 Category:MySQL
-
In this tutorial, we will learn how to join three tables in MySQL. Businesses and organizations may have to visualize three tables simultaneously based on certain matching columns common to all three tables. This operation is allowed in MyS
Use of UPDATE JOIN in MySQL
Publish Date:2025/04/11 Views:85 Category:MySQL
-
This tutorial will explain how to use the statement in MySQL database UPDATE JOIN . We generally use joins to iterate over the rows in a particular table which may or may not have similar rows in other tables. We can UPDATE use JOIN the cla
How to use the Row_Number() function in MySQL
Publish Date:2025/04/11 Views:142 Category:MySQL
-
In this tutorial, we will explain how to use the VALUES function in MySQL ROW_NUMBER() . This is a sorting method that assigns consecutive numbers within a partition starting from 1. It is important to note that no two rows within a partiti
Multiple primary keys in MySQL
Publish Date:2025/04/11 Views:66 Category:MySQL
-
In this tutorial, our goal is to explore the concept of multiple primary keys for a table in MySQL. Many times, businesses and organizations have to assign certain columns as primary keys. This primary key has multiple uses and reasons to b
Displaying foreign keys in MySQL
Publish Date:2025/04/11 Views:55 Category:MySQL
-
In this tutorial, we aim to explore how to display foreign keys for tables and columns in MySQL. The type of key that references a primary key, also known as the primary key of another table, is called a foreign key. Understanding the forei
Select first N rows in MySQL
Publish Date:2025/04/11 Views:85 Category:MySQL
-
Sometimes, you have to select first N rows of MySQL database according to your project requirements. n The value of varies according to the requirement; it can be TOP 1 row or TOP 30 rows. We will learn how to select top N rows using the cl