MySQLのユーザー作成と権限追加

概要

MySQLでユーザーを作成する手順です。
5.7, 8.X でも実行可能です。

ユーザー作成

MySQL [(none)]> create user ユーザー名@'ここにホストを指定する(指定しないとMySQL8ではエラーになる)' identified by 'パスワード';

権限の追加

MySQL [(none)]> grant all privileges on DB名.* to ユーザー名@'ホスト(%や10.%など)'

権限の確認

MySQL [(none)]> show grants for testuser01@'%';
+--------------------------------------------------------+
| Grants for testuser01@%                                |
+--------------------------------------------------------+
| GRANT USAGE ON *.* TO 'testuser01'@'%'                 |
| GRANT ALL PRIVILEGES ON `testdb`.* TO 'testuser01'@'%' |
+--------------------------------------------------------+
2 rows in set (0.00 sec)

権限の剥奪

MySQL [(none)]> revoke all privileges on testdb.* from testuser01@'%';
Query OK, 0 rows affected (0.01 sec)

MySQL [(none)]> show grants for testuser01@'%';
+----------------------------------------+
| Grants for testuser01@%                |
+----------------------------------------+
| GRANT USAGE ON *.* TO 'testuser01'@'%' |
+----------------------------------------+
1 row in set (0.00 sec)