skip to Main Content

Is there a way to perform a DROP at specific prefix users (lets say tenant_* ) of MySQL?

IE :

DROP USER 'tenant_george'@'%';
DROP USER 'tenant_john'@'%';

etc

2

Answers


  1. I don’t think that is possible.

    according to the doc to delete multiple users you need to so:

    DROP USER [IF EXISTS] user [, user] ...
    

    in your case, I would rather write a script that generates such line 😉

    (edit: typo)

    Login or Signup to reply.
  2. Create the drop statements using

    SELECT CONCAT("DROP USER '", user, "'@'", host, "';")
        FROM mysql.user
        WHERE user LIKE 'tenant_%'
    

    Adjust the WHERE clause as needed (or simply ignore some of the output.)

    Then copy/paste the output to perform the DROPs.

    (This could be turned into a stored proc, but, assuming it is a one-time task, I suggest including the manual step. And it gives you a chance to desk-check that you have not accidentally included ‘root’ or have quoting problems.)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search