Sling Academy
Home/MySQL/MySQL 8: How to grant privileges to a user for a database

MySQL 8: How to grant privileges to a user for a database

Last updated: January 25, 2024

Overivew

In modern web development, databases play a crucial role in storing, retrieving, and managing data. MySQL, being one of the most popular relational database management systems, offers a comprehensive set of features for database administration, including user privilege management. This article provides a step-by-step guide on how to grant privileges to a user for a database in MySQL 8.

User Privileges in MySQL

User privileges are a fundamental aspect of database security, allowing database administrators to control what each user can and cannot do within the database environment. MySQL organizes privileges at various levels such as global, database, table, column, and routine. Understanding how to properly manage these privileges is essential for protecting your data and ensuring that users have the right level of access to perform their jobs.

Creating a New User

Before granting privileges, you typically need to create a new user. This can be done using the CREATE USER statement.

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

This will create a new user named newuser that can connect from localhost with the specified password.

Granting Basic Privileges

The next step after creating a user is to grant them necessary privileges for a specific database. The GRANT statement is used for this purpose.

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

The above command grants all privileges on every table of database_name to newuser. After executing this command, it is important to execute the FLUSH PRIVILEGES statement to apply the changes.

FLUSH PRIVILEGES;

Understanding Privilege Types

MySQL defines a variety of privileges that can be granted to users. Some of the most common include SELECT, INSERT, UPDATE, DELETE, and CREATE. Each one allows the user to perform a specific action on a database or a table.

Granting Specific Privileges

Instead of granting all privileges, you may want to grant specific privileges based on the user’s requirements.

GRANT SELECT, INSERT ON database_name.table_name TO 'newuser'@'localhost';

This command allows newuser to only read and insert data into table_name in the database_name database.

Revoking Privileges

To remove privileges from a user, you can use the REVOKE statement.

REVOKE INSERT ON database_name.table_name FROM 'newuser'@'localhost';

After running the above command, newuser will no longer be able to insert data into table_name.

Using Prefixes with Privileges

MySQL allows you to grant privileges to a user across all databases on the server or specific columns within a table using wildcard characters and column-specific privilege lists.

GRANT SELECT, INSERT ON *.* TO 'newuser'@'localhost';
GRANT UPDATE (columnName) ON database_name.table_name TO 'newuser'@'localhost';

The first command grants SELECT and INSERT privileges on all databases and tables to newuser, while the second command grants UPDATE privilege only on the specific columnName of table_name.

Advanced Privilege Granting

In more complex scenarios, you may need to grant privileges that allow the user to manage other user accounts, handle transactions, or execute stored procedures.

GRANT CREATE USER, RELOAD ON *.* TO 'adminuser'@'localhost' WITH GRANT OPTION;

This grants an adminuser the ability to create other users and execute the RELOAD command. The WITH GRANT OPTION clause allows the user to grant the same privileges to other users as well.

Conclusion

In conclusion, managing user privileges in MySQL 8 is a powerful feature that enhances security and ensures that users have access to the appropriate data and functionalities. By following the steps outlined in this guide, you can effectively control user permissions within your MySQL databases.

Next Article: MySQL 8: How to revoke/restrict privileges from a user

Previous Article: MySQL 8: How to create a new user with root privileges

Series: MySQL Tutorials: From Basic to Advanced

MySQL

You May Also Like

  • MySQL: How to reset the AUTO_INCREMENT value of a table
  • MySQL: How to add a calculated column to SELECT query
  • MySQL: Eliminate orphan rows in one-to-many relationships
  • MySQL: Using R-Tree Indexes for Spatial Data Types
  • How to Create Custom Collations in MySQL
  • Using Hash Indexes in MySQL: A Practical Guide
  • Understanding Full-Text Indexes in MySQL
  • Partial Indexes in MySQL: A Practical Guide
  • MySQL: How to Remove FOREIGN KEY Constraints
  • Using ENUM in MySQL 8: A Practical Guide (with Examples)
  • MySQL: Creating a Fixed-Size Table by Using Triggers
  • One-to-Many Relationship in MySQL 8: A Practical Guide
  • Using Regular Expressions in MySQL 8: The Complete Guide
  • Using Loops in MySQL: A Practical Guide (with Examples)
  • How to Execute an SQL File in VS Code
  • Making use of the JSON_REMOVE() function in MySQL 8
  • MySQL 8: How to count rows in related tables
  • Replication in MySQL 8: A Comprehensive Guide
  • MySQL 8: FIRST_VALUE(), LAST_VALUE(), and NTH_VALUE() functions – Explained with examples