Checking if mysql user exists

前端 未结 7 1751
被撕碎了的回忆
被撕碎了的回忆 2021-02-02 09:20

How can I check if a user exists?

Im doing an installer for a mysql database, and I need to check if a user exits, if not create user, if yes delete user and create it a

7条回答
  •  眼角桃花
    2021-02-02 09:57

    MySQL 5.7 already includes DROP USER IF EXISTS, but for older versions I use pt-show-grants --drop from percona-toolkit and feed back the DROP USER part to mysql:

    pt-show-grants --drop --only=$username | grep '^DROP USER' | mysql -v
    

    If there are multiple username-hostname pairs this removes all of them.

提交回复
热议问题