端くれプログラマの備忘録 MySQL [MySQL] グローバル権限とデータベースレベルの権限

[MySQL] グローバル権限とデータベースレベルの権限

権限には、グローバルな権限とデータベースレベルの権限がある。

例えば SUPER はグローバルな権限である。なので

mysql> GRANT SUPER ON db1.* to user1@'%';

といったようにデータベース指定で設定しようとすると、以下のように怒られる。

Incorrect usage of DB GRANT and GLOBAL PRIVILEGES

グローバル権限を付与する場合は、以下のようにデータベースをワイルドカード指定するのが正解らしい。

mysql> GRANT SUPER ON *.* to user1@'%';

グローバル権限 – mysql.userテーブルで管理

mysql> desc mysql.user;
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field                  | Type                              | Null | Key |  Default               | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host                   | char(60)                          | NO   | PRI |                        |       |
| User                   | char(32)                          | NO   | PRI |                        |       |
| Select_priv            | enum('N','Y')                     | NO   |     | N                      |       |
| Insert_priv            | enum('N','Y')                     | NO   |     | N                      |       |
| Update_priv            | enum('N','Y')                     | NO   |     | N                      |       |
| Delete_priv            | enum('N','Y')                     | NO   |     | N                      |       |
| Create_priv            | enum('N','Y')                     | NO   |     | N                      |       |
| Drop_priv              | enum('N','Y')                     | NO   |     | N                      |       |
| Reload_priv            | enum('N','Y')                     | NO   |     | N                      |       |
| Shutdown_priv          | enum('N','Y')                     | NO   |     | N                      |       |
| Process_priv           | enum('N','Y')                     | NO   |     | N                      |       |
| File_priv              | enum('N','Y')                     | NO   |     | N                      |       |
| Grant_priv             | enum('N','Y')                     | NO   |     | N                      |       |
| References_priv        | enum('N','Y')                     | NO   |     | N                      |       |
| Index_priv             | enum('N','Y')                     | NO   |     | N                      |       |
| Alter_priv             | enum('N','Y')                     | NO   |     | N                      |       |
| Show_db_priv           | enum('N','Y')                     | NO   |     | N                      |       |
| Super_priv             | enum('N','Y')                     | NO   |     | N                      |       |
| Create_tmp_table_priv  | enum('N','Y')                     | NO   |     | N                      |       |
| Lock_tables_priv       | enum('N','Y')                     | NO   |     | N                      |       |
| Execute_priv           | enum('N','Y')                     | NO   |     | N                      |       |
| Repl_slave_priv        | enum('N','Y')                     | NO   |     | N                      |       |
| Repl_client_priv       | enum('N','Y')                     | NO   |     | N                      |       |
| Create_view_priv       | enum('N','Y')                     | NO   |     | N                      |       |
| Show_view_priv         | enum('N','Y')                     | NO   |     | N                      |       |
| Create_routine_priv    | enum('N','Y')                     | NO   |     | N                      |       |
| Alter_routine_priv     | enum('N','Y')                     | NO   |     | N                      |       |
| Create_user_priv       | enum('N','Y')                     | NO   |     | N                      |       |
| Event_priv             | enum('N','Y')                     | NO   |     | N                      |       |
| Trigger_priv           | enum('N','Y')                     | NO   |     | N                      |       |
| Create_tablespace_priv | enum('N','Y')                     | NO   |     | N                      |       |
| ssl_type               | enum('','ANY','X509','SPECIFIED') | NO   |     |                        |       |
| ssl_cipher             | blob                              | NO   |     | NULL                   |       |
| x509_issuer            | blob                              | NO   |     | NULL                   |       |
| x509_subject           | blob                              | NO   |     | NULL                   |       |
| max_questions          | int(11) unsigned                  | NO   |     | 0                      |       |
| max_updates            | int(11) unsigned                  | NO   |     | 0                      |       |
| max_connections        | int(11) unsigned                  | NO   |     | 0                      |       |
| max_user_connections   | int(11) unsigned                  | NO   |     | 0                      |       |
| plugin                 | char(64)                          | NO   |     |  mysql_native_password |       |
| authentication_string  | text                              | YES  |     | NULL                   |       |
| password_expired       | enum('N','Y')                     | NO   |     | N                      |       |
| password_last_changed  | timestamp                         | YES  |     | NULL                   |       |
| password_lifetime      | smallint(5) unsigned              | YES  |     | NULL                   |       |
| account_locked         | enum('N','Y')                     | NO   |     | N                      |       |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
45 rows in set (0.00 sec)

データベースレベルの権限 – mysql.dbテーブルで管理

mysql> desc mysel.db;

+-----------------------+---------------+------+-----+---------+-------+
| Field                 | Type          | Null | Key | Default | Extra |
+-----------------------+---------------+------+-----+---------+-------+
| Host                  | char(60)      | NO   | PRI |         |       |
| Db                    | char(64)      | NO   | PRI |         |       |
| User                  | char(32)      | NO   | PRI |         |       |
| Select_priv           | enum('N','Y') | NO   |     | N       |       |
| Insert_priv           | enum('N','Y') | NO   |     | N       |       |
| Update_priv           | enum('N','Y') | NO   |     | N       |       |
| Delete_priv           | enum('N','Y') | NO   |     | N       |       |
| Create_priv           | enum('N','Y') | NO   |     | N       |       |
| Drop_priv             | enum('N','Y') | NO   |     | N       |       |
| Grant_priv            | enum('N','Y') | NO   |     | N       |       |
| References_priv       | enum('N','Y') | NO   |     | N       |       |
| Index_priv            | enum('N','Y') | NO   |     | N       |       |
| Alter_priv            | enum('N','Y') | NO   |     | N       |       |
| Create_tmp_table_priv | enum('N','Y') | NO   |     | N       |       |
| Lock_tables_priv      | enum('N','Y') | NO   |     | N       |       |
| Create_view_priv      | enum('N','Y') | NO   |     | N       |       |
| Show_view_priv        | enum('N','Y') | NO   |     | N       |       |
| Create_routine_priv   | enum('N','Y') | NO   |     | N       |       |
| Alter_routine_priv    | enum('N','Y') | NO   |     | N       |       |
| Execute_priv          | enum('N','Y') | NO   |     | N       |       |
| Event_priv            | enum('N','Y') | NO   |     | N       |       |
| Trigger_priv          | enum('N','Y') | NO   |     | N       |       |
+-----------------------+---------------+------+-----+---------+-------+
22 rows in set (0.00 sec)

参考サイト

mysql – How to grant super privilege to the user? – Database Administrators Stack Exchange
https://dba.stackexchange.com/questions/63404/how-to-grant-super-privilege-to-the-user