1. 程式人生 > 資料庫 >MySQL資料庫高危許可權回收參考

MySQL資料庫高危許可權回收參考

1. 基本操作指令

1、檢視當前系統資料庫

# mysql -uroot -p
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| erp              |
+--------------------+
3 rows in set (0.00 sec)

2、切換資料庫

mysql> use erp;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed

2. 許可權回收準備工作

1、統計當前環境所有的使用者(%表示所有機器可訪問;127.0.0.1、localhost表示本機可訪問)

mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
+-----------------------------------------+
| query                                   |
+-----------------------------------------+
| User: 'root'@'%';                       |
| User: 'root'@'127.0.0.1';               |
| User: 'root'@'::1';                     |
| User: ''@'localhost';                   |
| User: 'root'@'localhost';               |
| User: 'erp'@'%';                       |
+-----------------------------------------+
5 rows in set (0.00 sec)

2、統計業務使用者具有哪些許可權以使用者名稱erp舉例如下

mysql> show grants for 'erp'@'%';
+------------------------------------------------------------------------------------+
| Grants for erp@%                                                         |
+------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'erp'@'%' IDENTIFIED BY PASSWORD '*******'      |
| GRANT ALL PRIVILEGES ON `ump`.* TO 'ump'@'%'                          |
+------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

3. 操作回收高危許可權

1、回收所有許可權或回收某一許可權,比如drop許可權

mysql> revoke all privileges on erp.* from 'erp'@'%';
mysql> revoke drop on erp.* from 'erp'@'%';
mysql> flush privileges;

備註:
(1)列舉部分特殊的伺服器許可權及其功能說明:

super:擁有此許可權允許使用者終止任何查詢;修改全域性變數的SET語句;使用CHANGE MASTER,PURGE MASTER LOGS
shutdown:關閉資料庫
show databases:檢視資料庫
replication client:查詢master server、slave server狀態
replication slave:檢視從伺服器
reload:擁有此許可權才可執行flush [tables | logs | privileges]
process:擁有此許可權才可以執行SHOW PROCESSLIST和KILL命令
file:擁有file許可權才可以執行 select ..into outfile和load data infile…操作

(2)普通使用者許可權及其功能說明:

all:允許任何操作(usage許可權不能被回收)
usage:只允許登入
alter:修改資料庫的表
alter routine:修改/刪除儲存過程
create:建立表
create routine:建立儲存過程
create temporary tables:建立臨時表
create:建立新的資料庫或表
create view:建立檢視
delete:刪除表資料
drop:刪除資料庫/表
event:建立/更改/刪除/檢視事件
execute:執行許可權
grant option:將自身所擁有的許可權授予其他使用者
index:建立/刪除索引
insert:新增表資料
lock tables:鎖表
references:將其它表的一個欄位作為某一個表的外來鍵約束
select:查詢表資料
show view:檢視檢視
trigger:建立觸發器
update:更新表資料

2、重新授權必要許可權

mysql> grant select,insert,alter,update,delete,create,execute on erp.* to 'erp'@'%' ;
mysql> flush privileges;

3、確認許可權

mysql> show grants for 'erp'@'%';

4. 注意事項

1)、file, process, super為危險許可權,切勿許可權授予管理員以外的賬號;

mysql> revoke file,process,super on erp.* from 'erp'@'%';

2)、檢視某個或所有使用者的伺服器許可權,確認普通賬號沒有授權上述三種危險許可權

mysql> select * from mysql.user where user='erp'\G;
*************************** 1. row ***************************
                  Host: %
                  User: erp
              Password: *33F471D4D8A84CD6C0
           Select_priv: N
           Insert_priv: N
           Update_priv: N
           Delete_priv: N
           Create_priv: N
             Drop_priv: N
           Reload_priv: N
         Shutdown_priv: N
          Process_priv: N
             File_priv: N
            Grant_priv: N
       References_priv: N
            Index_priv: N
            Alter_priv: N
          Show_db_priv: N
            Super_priv: N
 Create_tmp_table_priv: N
      Lock_tables_priv: N
          Execute_priv: N
       Repl_slave_priv: N
      Repl_client_priv: N
      Create_view_priv: N
        Show_view_priv: N
   Create_routine_priv: N
    Alter_routine_priv: N
      Create_user_priv: N
            Event_priv: N
          Trigger_priv: N
Create_tablespace_priv: N
              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: 
      password_expired: N
1 row in set (0.00 sec)
mysql> select * from mysql.user \G;

3)、授予某張表許可權,許可權資訊儲存在mysql.tables_priv表中

mysql> grant select on dbname.tablename to 'username'@'%' with grant option;
mysql> select * from mysql.tables_priv;
select * from mysql.tables_priv;
+-----------+-----+-------+------------+----------------+---------------------+-------+
| Host | Db  | User  | Table_name  | Grantor | Timestamp | Table_priv | Column_priv |
+-----------+-----+-------+------------+----------------+---------------------+-------+
| % | dbname | username | tablename | root@localhost | 0000-00-00 00:00:00 | Select,Grant |             |
+-----------+-----+-------+------------+----------------+---------------------+-------+

4)、授予某個欄位許可權,許可權資訊儲存在mysql.columns_priv表中

mysql> grant select(Column_name) on dbname.tablename to 'username'@'%' with grant option;
mysql> select * from mysql.columns_priv;
select * from mysql.columns_priv;
+-----------+-----+-------+------------+-------------+---------------------+----------+
| Host   | Db  | User  | Table_name | Column_name | Timestamp   | Column_priv |
+-----------+-----+-------+------------+-------------+---------------------+----------+
| % | dbname | username | tablename | Column_name | 0000-00-00 00:00:00 | Select|
+-----------+-----+-------+------------+-------------+---------------------+----------+

5)、當使用如下命令回收許可權時,它回收的只是全域性的許可權,username使用者其他的許可權,比如對dbname資料庫的許可權,對tablename表的許可權,對某個Column_name欄位的許可權仍然持有。

mysql> revoke all privileges on *.* from 'username'@'localhost';

所以為了回收使用者的所有許可權,要使用如下命令

mysql> revoke all privileges,grant option from 'username'@'%';

5. 結束