MySQLログインには、ユーザー名、パスワード、IPアドレスの3つの条件が必要です。
**当局関連
- すべてのライブラリのパーミッション:ビューmysql.user
- ライブラリのパーミッションの指定: view mysql.db
- テーブルのパーミッションの指定: mysql.table_privを参照してください。
- カラムのパーミッションの指定: mysql.columns_priv を参照してください。
**共通の権威
- SQLステートメント: SELECT、INSERT、UPDATAE、INDEX
- ストアド・プロシージャ: CREATE ROUTINE、ALTER ROUTINE、EXECUTE、TRIGGER
- 管理者権限:SUPER、RELOAD、SHOW DATABASE、SHUTDOWN、GRANT OPTION
**ユーザー関連業務
ユーザー winston を作成 パスワード 12345678 どの IP からでもアクセス可能
(root@localhost)[(none)]> create user 'winston'@'%' identified by '12345678';
Query OK, 0 rows affected (0.07 sec)
# サーバーを変更する
[root@localhost ~]# mysql -h192.168.153.139 -uwinston -p
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.25 MySQL Community Server (GPL)
 (c) 2000, 2019, 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.
(winston@192.168.153.139)[(none)]>
ユーザーを削除して別のサーバーでログイン
(root@localhost)[(none)]> drop user 'winston'@'%';
Query OK, 0 rows affected (0.01 sec)
[root@localhost ~]# mysql -h192.168.153.139 -uwinston -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'winston'@'192.168.153.139' (using password: YES)
閲覧許可
# 現在のユーザーの権限を表示する
(root@localhost)[(none)]> 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)
# USAGE 接続権限のみ
[root@localhost ~]# mysql -h192.168.153.139 -ushaco -p
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.25 MySQL Community Server (GPL)
 (c) 2000, 2019, 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.
(shaco@192.168.153.139)[(none)]> show grants;
+-----------------------------------+
| Grants for shaco@% |
+-----------------------------------+
| GRANT USAGE ON *.* TO 'shaco'@'%' |
+-----------------------------------+
1 row in set (0.00 sec)
SELECT、INSERT、UPDATE、DELETEなどの権限を付与します。
ユーザーにスコープのアクセス許可を与えます。
(root@localhost)[(none)]> grant select,delete,update,insert on *.* to 'shaco';
Query OK, 0 rows affected (0.00 sec)
(shaco@192.168.153.139)[(none)]> show grants;
+------------------------------------------------------------+
| Grants for shaco@% |
+------------------------------------------------------------+
| GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'shaco'@'%' |
+------------------------------------------------------------+
1 row in set (0.00 sec)
作成と認可のマージについては、1つのステートメントで実行することは可能ですが、警告を伴って実行することは主流ではありません。したがって、最善の方法は、最初に作成し、次に認可することです。
(root@localhost)[(none)]> grant select,delete,update,insert on test.* to 'tom'@'192.168.153.%' identified by '12345678';
Query OK, 0 rows affected, 1 warning (0.05 sec)
(root@localhost)[(none)]> show warnings\G
*************************** 1\. row ***************************
 Level: Warning
 Code: 1287
Message: Using GRANT for creating new user is deprecated and will be removed in future release. Create new user with CREATE USER statement.
1 row in set (0.00 sec)
パスワードを変更するには、alter文を使用します。
(root@localhost)[(none)]> alter user 'shacoK'@'192.168.153.%' identified by '1234';
Query OK, 0 rows affected (0.00 sec)
パーミッションの変更と追加は、grant
(root@localhost)[(none)]> grant select on *.* to 'shacoK'@'192.168.153.%';
Query OK, 0 rows affected (0.00 sec)
(root@localhost)[(none)]> show grants for 'shacoK'@'192.168.153.%';
+-------------------------------------------------+
| Grants for shacoK@192.168.153.% |
+-------------------------------------------------+
| GRANT SELECT ON *.* TO 'shacoK'@'192.168.153.%' |
+-------------------------------------------------+
1 row in set (0.00 sec)
......から......許可を取り消します。
注意:全権限を奪うことは、ユーザーを削除することではありません。
(root@localhost)[(none)]> revoke select on *.* from 'shacoK'@'192.168.153.%';
Query OK, 0 rows affected (0.00 sec)
(root@localhost)[(none)]> show grants for 'shacoK'@'192.168.153.%';
+------------------------------------------------+
| Grants for shacoK@192.168.153.% |
+------------------------------------------------+
| GRANT USAGE ON *.* TO 'shacoK'@'192.168.153.%' |
+------------------------------------------------+
1 row in set (0.00 sec)
他のユーザーに権限を付与する権限 .........................に付与する権限(grant オプション付き
(root@localhost)[(none)]> grant select on *.* to 'shacoK'@'192.168.153.%' with grant option;
Query OK, 0 rows affected (0.00 sec)
(root@localhost)[(none)]> show grants for 'shacoK'@'192.168.153.%';
+-------------------------------------------------------------------+
| Grants for shacoK@192.168.153.% |
+-------------------------------------------------------------------+
| GRANT SELECT ON *.* TO 'shacoK'@'192.168.153.%' WITH GRANT OPTION |
+-------------------------------------------------------------------+
1 row in set (0.00 sec)
**パーミッション**に関する表
MySQLで作成されたユーザはmysql.userにあり、authentication_stringフィールドはパスワードを記録するために使用されます。authentication_stringフィールドは、5.7でのみ使用可能なパスワードを記録するために使用されます。
(root@localhost)[mysql]> select host,user,authentication_string from user ;
+-----------------------+---------------+-------------------------------------------+
| host | user | authentication_string |
+-----------------------+---------------+-------------------------------------------+
| localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| localhost.localdomain | root | |
| .1 | root | |
| ::1 | root | |
| localhost | | NULL |
| localhost.localdomain | | NULL |
| localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| % | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| % | shaco | *84AAC12F54AB666ECFC2A83C676908C8BBC381B1 |
| 192.168.153.% | shacoK | *A4B6157319038724E3560894F7F932C8886EBFCF |
+-----------------------+---------------+-------------------------------------------+
11 rows in set (0.02 sec)
ユーザーテーブル
ここで、各列は権限を表し、Userテーブルの各列はグローバル権限を表します。
(root@localhost)[mysql]> select * from user limit 1\G
*************************** 1\. row ***************************
 Host: localhost
 User: root
 Select_priv: Y
 Insert_priv: Y
 Update_priv: Y
 Delete_priv: Y
 Create_priv: Y
 Drop_priv: Y
 Reload_priv: Y
 Shutdown_priv: Y
 Process_priv: Y
 File_priv: Y
 Grant_priv: Y
 References_priv: Y
 Index_priv: Y
 Alter_priv: Y
 Show_db_priv: Y
 Super_priv: Y
 Create_tmp_table_priv: Y
 Lock_tables_priv: Y
 Execute_priv: Y
 Repl_slave_priv: Y
 Repl_client_priv: Y
 Create_view_priv: Y
 Show_view_priv: Y
 Create_routine_priv: Y
 Alter_routine_priv: Y
 Create_user_priv: Y
 Event_priv: Y
 Trigger_priv: Y
Create_tablespace_priv: Y
 ssl_type: 
 ssl_cipher: 
 x509_issuer: 
 x509_subject: 
 max_questions: 0
 max_updates: 0
 max_connections: 0
 max_user_connections: 0
 plugin: mysql_native_password
 authentication_string: *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B
 password_expired: N
 password_last_changed: 2020-07-27 18:37:28
 password_lifetime: NULL
 account_locked: N
1 row in set (0.00 sec)
DBテーブル
特定のデータベースを操作する権限のみを持つユーザーの記録
(root@localhost)[mysql]> select * from db limit 1\G
*************************** 1\. row ***************************
 Host: %
 Db: test
 User: 
 Select_priv: Y
 Insert_priv: Y
 Update_priv: Y
 Delete_priv: Y
 Create_priv: Y
 Drop_priv: Y
 Grant_priv: N
 References_priv: Y
 Index_priv: Y
 Alter_priv: Y
Create_tmp_table_priv: Y
 Lock_tables_priv: Y
 Create_view_priv: Y
 Show_view_priv: Y
 Create_routine_priv: Y
 Alter_routine_priv: N
 Execute_priv: N
 Event_priv: Y
 Trigger_priv: Y
1 row in set (0.00 sec)
table_privテーブル 特定のテーブルの完全な制限を示します。DBテーブルと同じ効果で、table_nameカラムが追加されています。
columns_privテーブル。
**注:認可を得るためにこれら4つの表を変更しないでください。**
**ログイン制限
最大同時接続ユーザー数が1の場合
(root@localhost)[mysql]> alter user 'shacoK'@'192.168.153.%' with max_user_connections 1;
Query OK, 0 rows affected (0.00 sec)
[root@localhost ~]# mysql -h192.168.153.139 -ushacoK -p
Enter password: 
ERROR 1226 (42000): User 'shacoK' has exceeded the 'max_user_connections' resource (current value: 1)
単位時間当たりのユーザーログイン数
(root@localhost)[(none)]> alter user 'shacoK'@'192.168.153.%' with max_connections_per_hour 3;
Query OK, 0 rows affected (0.02 sec)
[root@localhost ~]# mysql -h192.168.153.139 -ushacoK -p
Enter password: 
ERROR 1226 (42000): User 'shacoK' has exceeded the 'max_connections_per_hour' resource (current value: 3)




