LoginSignup
1
2

More than 5 years have passed since last update.

MySQL 存在しないユーザー削除時のエラー回避方法

Last updated at Posted at 2018-03-27

MySQLで存在しないユーザーを削除したときにエラーになるのを回避する方法を調べました。

MySQL 5.7.6以降

MySQL 5.7.6以降ではDROP USER構文でIF EXISTSオプションが利用できる。

DROP USER IF EXISTS user

MySQL 5.7.5以前

IF EXISTSオプションが使えないため、DROP USER構文で存在しないユーザーを削除しようとするとエラーになる。これを回避するために、GRANT構文でユーザー権限を権限なし(USAGE)に変更(ユーザーが存在しない場合には作成される)してからDROP USERを実行する。

GRANT USAGE ON *.* TO user IDENTIFIED BY password;
DROP USER user;

※ このSQLはMySQL 5.7.6以降では以下の警告が出る。

1287 Using GRANT statement to modify existing user's properties other than privileges is deprecated and will be removed in future release. Use ALTER USER statement for this operation.
参考
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2