0
0
MysqlHow-ToBeginner · 3 min read

How to Use FLUSH PRIVILEGES in MySQL: Syntax and Examples

In MySQL, use the FLUSH PRIVILEGES command to reload the grant tables and apply changes made to user privileges without restarting the server. This command ensures that permission changes take effect immediately.
📐

Syntax

The FLUSH PRIVILEGES command reloads the grant tables in MySQL so that changes to user privileges take effect immediately.

It has a simple syntax with no parameters.

sql
FLUSH PRIVILEGES;
💻

Example

This example shows how to create a new user, grant privileges, and then use FLUSH PRIVILEGES to apply the changes immediately.

sql
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password123';
GRANT SELECT ON mydatabase.* TO 'newuser'@'localhost';
FLUSH PRIVILEGES;
Output
Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.00 sec)
⚠️

Common Pitfalls

  • Forgetting to run FLUSH PRIVILEGES after manually editing the grant tables can cause permission changes to not take effect.
  • Using GRANT or REVOKE commands automatically reloads privileges, so FLUSH PRIVILEGES is not needed in those cases.
  • Running FLUSH PRIVILEGES unnecessarily can cause a small performance hit, so use it only when needed.
sql
/* Wrong: Editing grant tables directly without flushing privileges */
UPDATE mysql.user SET authentication_string=PASSWORD('newpass') WHERE User='user1';
-- Permissions won't update until flush

/* Right: Flush privileges after manual changes */
FLUSH PRIVILEGES;
📊

Quick Reference

CommandDescription
FLUSH PRIVILEGES;Reloads the grant tables to apply privilege changes immediately.
GRANT ...;Grants privileges and automatically reloads privileges.
REVOKE ...;Revokes privileges and automatically reloads privileges.
ALTER USER ...;Changes user properties and automatically reloads privileges.

Key Takeaways

Use FLUSH PRIVILEGES to reload MySQL grant tables after manual privilege changes.
GRANT and REVOKE commands automatically reload privileges, so FLUSH PRIVILEGES is not needed then.
Always run FLUSH PRIVILEGES after editing grant tables directly to apply changes.
Avoid unnecessary use of FLUSH PRIVILEGES to prevent minor performance impacts.