Home > Database > Mysql Tutorial > body text

How to Grant Database Creation Privileges While Restricting Access to Specific Databases in MySQL?

Patricia Arquette
Release: 2024-11-04 19:31:01
Original
256 people have browsed it

How to Grant Database Creation Privileges While Restricting Access to Specific Databases in MySQL?

Granting Database Creation Privileges and Restricting Access

Problem:

Users need to be able to create databases within a MySQL instance, but each user should be granted access only to their own databases.

Solution:

Step 1: Create a Replacement Role

Create a role that includes the necessary privileges for creating and modifying databases:

CREATE ROLE db_creator;
GRANT CREATE, DROP, ALTER, DELETE, INSERT, UPDATE, SELECT ON *.* TO db_creator;
Copy after login

Step 2: Grant the Role to Target Users

Grant the role to the desired users, specifying that the role's privileges should be applied to databases with a specific naming convention:

GRANT db_creator TO 'testuser'@'%';
Copy after login

In this example, the testuser will have the privileges granted by the db_creator role, but only for databases that begin with testuser_.

Additional Notes:

  • Replace testuser with the actual user name.
  • Specify '%' as the host to grant access from any host (adjust as needed for your environment).
  • The naming convention for databases (e.g., testuser_%) can be customized as desired.

The above is the detailed content of How to Grant Database Creation Privileges While Restricting Access to Specific Databases in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template