MySQL: Check if the user exists and drop it

前端 未结 14 1685
遇见更好的自我
遇见更好的自我 2020-12-02 16:43

There’s not standard way to check if a MySQL user exists and based on that drop it. Are there any workarounds for this?

Edit: I need a straight way to run this wi

相关标签:
14条回答
  • 2020-12-02 16:52

    Since MySQL 5.7 you can do a DROP USER IF EXISTS test

    More info: http://dev.mysql.com/doc/refman/5.7/en/drop-user.html

    0 讨论(0)
  • 2020-12-02 16:53

    This worked for me:

    GRANT USAGE ON *.* TO 'username'@'localhost';
    DROP USER 'username'@'localhost';
    

    This creates the user if it doesn't already exist (and grants it a harmless privilege), then deletes it either way. Found solution here: http://bugs.mysql.com/bug.php?id=19166

    Updates: @Hao recommends adding IDENTIFIED BY; @andreb (in comments) suggests disabling NO_AUTO_CREATE_USER.

    0 讨论(0)
  • 2020-12-02 16:55

    in terminal do:

    sudo mysql -u root -p
    

    enter the password.

    select user from mysql.user;
    

    now delete the user 'the_username'

    DROP USER the_unername;
    

    replace 'the_username' with the user that you want to delete.

    0 讨论(0)
  • I wrote this procedure inspired by Cherian's answer. The difference is that in my version the user name is an argument of the procedure ( and not hard coded ) . I'm also doing a much necessary FLUSH PRIVILEGES after dropping the user.

    DROP PROCEDURE IF EXISTS DropUserIfExists;
    DELIMITER $$
    CREATE PROCEDURE DropUserIfExists(MyUserName VARCHAR(100))
    BEGIN
      DECLARE foo BIGINT DEFAULT 0 ;
      SELECT COUNT(*)
      INTO foo
        FROM mysql.user
          WHERE User = MyUserName ;
       IF foo > 0 THEN
             SET @A = (SELECT Result FROM (SELECT GROUP_CONCAT("DROP USER"," ",MyUserName,"@'%'") AS Result) AS Q LIMIT 1);
             PREPARE STMT FROM @A;
             EXECUTE STMT;
             FLUSH PRIVILEGES;
       END IF;
    END ;$$
    DELIMITER ;
    

    I also posted this code on the CodeReview website ( https://codereview.stackexchange.com/questions/15716/mysql-drop-user-if-exists )

    0 讨论(0)
  • 2020-12-02 17:05

    Um... Why all the complications and tricks?

    Rather then using DROP USER... You can simply delete the user from the mysql.user table (which doesn't throw an error if the user does not exist), and then flush privileges to apply the change.

    DELETE FROM mysql.user WHERE User = 'SomeUser' AND Host = 'localhost';
    FLUSH PRIVILEGES;
    

    -- UPDATE --

    I was wrong. It's not safe to delete the user like that. You do need to use DROP USER. Since it is possible to have mysql options set to not create users automatically via grants (an option I use), I still wouldn't recommend that trick. Here's a snipet from a stored procedure that works for me:

    DECLARE userCount INT DEFAULT 0;
    SELECT COUNT(*) INTO userCount FROM mysql.user WHERE User = userName AND Host='localhost';
    IF userCount > 0 THEN
        SET @S=CONCAT("DROP USER ", userName, "@localhost" );
        PREPARE stmt FROM @S;
        EXECUTE stmt;
        SELECT CONCAT("DROPPED PRE-EXISTING USER: ", userName, "@localhost" ) as info;
    END IF;
    FLUSH PRIVILEGES;
    
    0 讨论(0)
  • 2020-12-02 17:06

    Found the answer to this from one of the MySQL forums. We’ll need to use a procedure to delete the user.

    User here is “test” and “databaseName” the database name.

    
    SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='ANSI';
    USE databaseName ;
    DROP PROCEDURE IF EXISTS databaseName.drop_user_if_exists ;
    DELIMITER $$
    CREATE PROCEDURE databaseName.drop_user_if_exists()
    BEGIN
      DECLARE foo BIGINT DEFAULT 0 ;
      SELECT COUNT(*)
      INTO foo
        FROM mysql.user
          WHERE User = 'test' and  Host = 'localhost';
       IF foo > 0 THEN
             DROP USER 'test'@'localhost' ;
      END IF;
    END ;$$
    DELIMITER ;
    CALL databaseName.drop_user_if_exists() ;
    DROP PROCEDURE IF EXISTS databaseName.drop_users_if_exists ;
    SET SQL_MODE=@OLD_SQL_MODE ;

    CREATE USER 'test'@'localhost' IDENTIFIED BY 'a'; GRANT ALL PRIVILEGES ON databaseName.* TO 'test'@'localhost' WITH GRANT OPTION

    0 讨论(0)
提交回复
热议问题