Open9

mysql ユーザー

syysyy

GRANT文でmysqlのユーザーを作成できる。

grant 権限 [フィールド名] [, 権限 [(フィールド名)]]
on データベース名.テーブル
TO ユーザー名[@ホスト] [identified by パスワード], [ユーザー...];
syysyy

ユーザー一覧

mysql.userテーブルでユーザー一覧を確認できる。

mysql> select user, host from mysql.user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| root          | %         |
| mysql.session | localhost |
| mysql.sys     | localhost |
| root          | localhost |
+---------------+-----------+
4 rows in set (0.00 sec)

syysyy

ユーザーaにテーブルaのselect権限のみを付与する。
ユーザー名とホスト,パスワードは''で囲む必要がある。

mysql> grant select on hoge.a to 'a'@'localhost' identified by 'pass';
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> select user, host from mysql.user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| root          | %         |
| a             | localhost |
| mysql.session | localhost |
| mysql.sys     | localhost |
| root          | localhost |
+---------------+-----------+
5 rows in set (0.00 sec)

aユーザーでmysqlに接続する。

root@7dc8b299c42c:/# mysql -u a -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 30
Server version: 5.7.31 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

aユーザーはaテーブルのみselectできる。insertやupdateはエラーになる。
show tablesにはaテーブルしか表示されない。

mysql> show tables;
+----------------+
| Tables_in_hoge |
+----------------+
| a              |
+----------------+
1 row in set (0.00 sec)

mysql> select * from a; 
+----+------+
| id | a1   |
+----+------+
|  1 |    1 |
+----+------+
1 row in set (0.00 sec)

mysql> insert into a values (2,2);
ERROR 1142 (42000): INSERT command denied to user 'a'@'localhost' for table 'a'
mysql> update a set a1 = 2;
ERROR 1142 (42000): UPDATE command denied to user 'a'@'localhost' for table 'a'
syysyy

drop user ユーザー名@ホスト
でユーザーを削除できる。

mysql> drop user a@localhost;
Query OK, 0 rows affected (0.00 sec)
syysyy

create userでユーザー作成

パスワードなし (identified by でパスワード指定可能)

mysql> create user a@localhost ;
Query OK, 0 rows affected (0.00 sec)

aユーザーで接続。作成しただけだと何も権限がないのでデータベースに接続もできない。

root@7dc8b299c42c:/# mysql -u a ;
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 31
Server version: 5.7.31 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use hoge;
ERROR 1044 (42000): Access denied for user 'a'@'localhost' to database 'hoge'
mysql> 
syysyy

show grants for ユーザーでユーザーの権限を表示する。
grantではなくgrantsなのに注意。

USAGEは権限なしを表す

mysql> show grants for a@localhost;
+---------------------------------------+
| Grants for a@localhost                |
+---------------------------------------+
| GRANT USAGE ON *.* TO 'a'@'localhost' |
+---------------------------------------+
1 row in set (0.00 sec)

show grantsだけなら今ログインしているユーザーの全部の権限を表示する

mysql> SHOW GRANTS;
+---------------------------------------------------------------------+
| Grants for root@localhost                                           |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION        |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)


syysyy

flush privilegesで権限をcommitする

syysyy

権限一覧

権限 内容
alter alterが使える
insert insert可能
update update可能
select select可能
delete delete 可能
create データベースとテーブルを作成可能
drop データベースとテーブルを削除可能
index indexの作成と削除を可能
show databases show databasesが可能
file load data文などが可能
lock tables lock tablesが可能
create view ビュー作成が可能
show view ビューの閲覧が可能
ALL 全権限を持つ
grant option 他のユーザーの権限変更が可能
create user ユーザー作成が可能
replication client ?
replication slave スレーブになれる権限がある
syysyy

権限削除

revoke 権限 from ユーザー
で権限を削除できる。
権限付与のときは grant 権限 to ユーザーでto とfromが違うので注意。
ユーザーに存在しない権限を削除してもエラーにはならないので注意。
削除対象となる権限やdb,テーブルは、持っている権限に完全一致させる必要あり。

mysql> show grants for c;
+---------------------------------------------------------------+
| Grants for c@%                                                |
+---------------------------------------------------------------+
| GRANT SELECT, INSERT, UPDATE, DELETE, ALTER ON *.* TO 'c'@'%' |
+---------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> revoke select on *.* from c;
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for c;
+-------------------------------------------------------+
| Grants for c@%                                        |
+-------------------------------------------------------+
| GRANT INSERT, UPDATE, DELETE, ALTER ON *.* TO 'c'@'%' |
+-------------------------------------------------------+
1 row in set (0.00 sec)

# ない権限を削除してもエラーにならない
mysql> revoke select on *.* from c;
Query OK, 0 rows affected (0.00 sec)