Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grant privileges to application user when schemas are created #2

Open
jsierles opened this issue Jul 27, 2024 · 0 comments
Open

Grant privileges to application user when schemas are created #2

jsierles opened this issue Jul 27, 2024 · 0 comments

Comments

@jsierles
Copy link
Member

If an application user can create a schema, it must also be assigned privileges to use it. The following stored procedure and scheduled event could automate this process.

DELIMITER $$

CREATE PROCEDURE GrantPrivileges()
BEGIN
  DECLARE done INT DEFAULT 0;
  DECLARE schema_name VARCHAR(64);
  DECLARE cur CURSOR FOR SELECT schema_name FROM information_schema.schemata WHERE schema_name NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys');
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

  OPEN cur;
  read_loop: LOOP
    FETCH cur INTO schema_name;
    IF done THEN
      LEAVE read_loop;
    END IF;
    SET @grant_stmt = CONCAT('GRANT ALL PRIVILEGES ON ', schema_name, '.* TO ''your_username''@''localhost'';');
    PREPARE stmt FROM @grant_stmt;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
  END LOOP;

  CLOSE cur;
END $$

DELIMITER ;
CREATE EVENT GrantPrivilegesEvent
ON SCHEDULE EVERY 1 MINUTE
DO
  CALL GrantPrivileges();
  
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant