權限管理
一 mysql庫下的授權表
linux系統的用戶作用是:
- 1、登陸系統
- 2、管理系統文件
一樣的道理,mysql數據庫管理軟件用戶的則作用是:
- 1、登陸MySQL數據庫
- 2、管理庫與表等數據庫對象
mysql數據庫管理軟件將權限信息都存放於mysql庫下,該庫下有一系列授權表,權限信息都存放於這一系列表中,我們挑幾個重點介紹一下
mysql庫下的授權表及其放行權限的范圍
-
1、mysql.user
針對所有數據、所有庫下所有表、以及表下的所有字段
-
2、msyql.db
只針對某一數據庫下的所有表,以及表下的所有字段
-
3、tables_priv
只針對某一張表、以及該表下的所有字段
-
4、columns_priv
只針對某一個字段
例如:如下圖,權限表放行權限的范圍
mysql.user:針對庫db1,db2及其包含的所有
db:只針對庫例如db1,及其db1包含的所有
tables_priv:只針對db1.table1,及其該表包含的所有
columns_prive:只針對db1.table1.column1,只放行該字段
二 權限相關操作
2.1 創建\查詢\刪除用戶
創建用戶語法
create user '用戶名'@'主機' identified by '密碼';
例
create user 'jason'@'1.1.1.1' identified by '123';
create user 'jason'@'192.168.1.%' identified by '123';
create user 'jason'@'%' identified by '123';
查詢用戶
mysql> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| | db04 |
| root | db04 |
| | localhost |
| root | localhost |
+------+-----------+
刪除用戶
mysql> drop user root@'::1';
Query OK, 0 rows affected (0.01 sec)
mysql> drop user ''@db04;
Query OK, 0 rows affected (0.00 sec)
mysql> drop user 'root'@db04;
Query OK, 0 rows affected (0.00 sec)
mysql> drop user ''@localhost;
Query OK, 0 rows affected (0.00 sec)
修改密碼
# 方式1
mysqladmin -uroot -p123 password '1'
# 方式2
update mysql.user set password=password('123') where user='root' and host='localhost';
# 方式3
set password=password('1'); 修改當前用戶的密碼
# 方式4
grant all on *.* to 'root'@'localhost' identified by '123';
2.2 授權
MySQL 賦予用戶權限命令的簡單格式可概括為:
grant 權限 on 數據庫對象 to 用戶
注意:grant, revoke 用戶權限后,該用戶只有重新連接 MySQL 數據庫,權限才能生效。
查看幫助
mysql> help grant
權限:對文件夾,對文件,對文件某一字段的權限
常用權限有:select, insert, update, delete
all可以代表除了grant之外的所有權限
MySQL 的 權限,分別可以作用在多個層次上
-
1、所有庫的所有表
-
2、單庫下的所有表(最常用的授權級別)
-
3、單表下的所有列
-
4、單列權限:企業業里稱單列授權為 脫敏,即脫離敏感信息,涉及到敏感信息一定要脫敏
例如 授予vip賬號對某一表下所有列的查詢權限 而授予非vip賬號對某一表下的某一列的查詢權限
-
5、針對存儲過程的權限
-
6、針對函數的權限
舉例如下
#(1)針對所有庫的所有表:*.*
grant select on *.* to 'jason1'@'localhost' identified by '123';
只在user表中可以查到jason1用戶的select權限被設置為Y
#(2)針對某一數據庫:db1.*
grant select on db1.* to 'jason2'@'%' identified by '123';
只在db表中可以查到jason2用戶的select權限被設置為Y
#(3)針對某一個表:db1.t1
grant select on db1.t1 to 'jason3'@'%' identified by '123';
只在tables_priv表中可以查到jason3用戶的select權限
#(4)針對某一個字段:
mysql> select * from t3;
+------+-------+------+
| id | name | age |
+------+-------+------+
| 1 | jason1 | 18 |
| 2 | jason2 | 19 |
| 3 | jason3 | 29 |
+------+-------+------+
grant select (id,name),update (age) on db1.t3 to 'jason4'@'localhost' identified by '123';
#可以在tables_priv和columns_priv中看到相應的權限
mysql> select * from tables_priv where user='jason4'\G
*************************** 1. row ***************************
Host: localhost
Db: db1
User: jason4
Table_name: t3
Grantor: root@localhost
Timestamp: 0000-00-00 00:00:00
Table_priv:
Column_priv: Select,Update
row in set (0.00 sec)
mysql> select * from columns_priv where user='jason4'\G
*************************** 1. row ***************************
Host: localhost
Db: db1
User: jason4
Table_name: t3
Column_name: id
Timestamp: 0000-00-00 00:00:00
Column_priv: Select
*************************** 2. row ***************************
Host: localhost
Db: db1
User: jason4
Table_name: t3
Column_name: name
Timestamp: 0000-00-00 00:00:00
Column_priv: Select
*************************** 3. row ***************************
Host: localhost
Db: db1
User: jason4
Table_name: t3
Column_name: age
Timestamp: 0000-00-00 00:00:00
Column_priv: Update
rows in set (0.00 sec)
#(5)作用在存儲過程上:
use db1;
delimiter //
create procedure p1()
BEGIN
select * from blog;
INSERT into blog(name,sub_time) values("xxx",now());
END //
delimiter ;
show procedure status; -- 查看到db1下有一個名為p1的存儲過程
grant execute on procedure db1.p1 to 'dba'@'localhost';
#(6)作用在函數上:
delimiter //
create function f1(
i1 int,
i2 int)
returns int
BEGIN
declare num int;
set num = i1 + i2;
return(num);
END //
delimiter ;
show function status; -- 查看到db1下有一個名為f1的函數
grant execute on function db1.f1 to 'dba'@'localhost';
測試:
[root@localhost ~]# mysql -udba -p
mysql> select db1.f1(1,2);
all可以代表除了grant之外的所有權限,可以用with帶上grant,授權一個超級管理員
grant all on *.* to jason@'%' identified by '123' with grant option;
查看mysql.user表中可以查到jason用戶的grant權限也被設置為Y
2.3 查看授權
#1.授權:
mysql> grant all on *.* to root@'%' identified by '123';
#2.查看用戶權限
查看當前用戶(自己)權限:
mysql> show grants;
查看其他 MySQL 用戶權限:
mysql> show grants for root@'%';
2.4 撤銷權限。
revoke 跟 grant 的語法差不多,只需要把關鍵字 “to” 換成 “from” 即可:
grant all on *.* to dba@localhost;
revoke all on *.* from dba@localhost;
revoke select on db1.* from 'jason'@'%';
2.5 擴展授權
max_queries_per_hour:一個用戶每小時可發出的查詢數量
max_updates_per_hour:一個用戶每小時可發出的更新數量
max_connections_per_hour:一個用戶每小時可連接到服務器的次數
max_user_connections:允許同時連接數量
mysql> grant all on *.* to test@'%' identified by '123' with max_user_connections 1;
# 測試:
mysql -utest -p123 -h192.168.15.100
不要用-hlocalhost,它代表通過本地套接字鏈接
2.6 針對不同角色權限分配
2.6.1 針對普通用戶
grant 普通數據用戶,查詢、插入、更新、刪除 數據庫中所有表數據的權利。
grant select on testdb.* to common_user@'%'
grant insert on testdb.* to common_user@'%'
grant update on testdb.* to common_user@'%'
grant delete on testdb.* to common_user@'%'
或者,用一條 MySQL 命令來替代:
grant select, insert, update, delete on testdb.* to common_user@'%'
2.6.2 針對開發人員
grant 數據庫開發人員,創建表、索引、視圖、存儲過程、函數。。。等權限,具體操作如下
1、grant 創建、修改、刪除 MySQL 數據表結構權限。
grant create on testdb.* to developer@'192.168.0.%';
grant alter on testdb.* to developer@'192.168.0.%';
grant drop on testdb.* to developer@'192.168.0.%';
2、grant 操作 MySQL 外鍵權限。
grant references on testdb.* to developer@'192.168.0.%';
3、grant 操作 MySQL 臨時表權限。
grant create temporary tables on testdb.* to developer@'192.168.0.%';
4、grant 操作 MySQL 索引權限。
grant index on testdb.* to developer@'192.168.0.%';
5、grant 操作 MySQL 視圖、查看視圖源代碼 權限。
grant create view on testdb.* to developer@'192.168.0.%';
grant show view on testdb.* to developer@'192.168.0.%';
6、grant 操作 MySQL 存儲過程、函數 權限。
grant create routine on testdb.* to developer@'192.168.0.%'; -- now, can show procedure status
grant alter routine on testdb.* to developer@'192.168.0.%'; -- now, you can drop a procedure
grant execute on testdb.* to developer@'192.168.0.%';
2.6.3 針對普通DBA
grant 普通 DBA 管理某個 MySQL 數據庫的權限。
grant all privileges on testdb.* to dba@'localhost'
其中,關鍵字 “privileges” 可以省略。
2.6.4 針對高級DBA
grant 高級 DBA 管理 MySQL 中所有數據庫的權限。
grant all on *.* to dba@'localhost'
2.7 在企業中權限的設定
#開發人員說:請給我開一個用戶
#1.首先進行溝通
1.你需要對哪些庫、表進行操作
2.你從哪里連接過來
3.用戶名有沒有要求
4.密碼要求
5.你要使用多長時間
6.發郵件
#2.一般給開發創建用戶權限,建議不給delete權限
grant select,update,delete,insert on *.* to jason@'10.0.0.%'' identified by '123';
#3.注意:
開發圖方便想要root用戶,這玩意,誰給誰背鍋,自己斟酌,可以考慮讓部門老大來給
四 破解密碼
4.1 linux平台
方法一:不推薦
[root@jason ~]# rm -rf /var/lib/mysql/mysql # 所有授權信息全部丟失!!!
[root@jason ~]# systemctl restart mariadb
[root@jason ~]# mysql
方法二:啟動時,跳過授權庫
[root@jason ~]# vim /etc/my.cnf #mysql主配置文件
[mysqld]
skip-grant-table
[root@jason ~]# systemctl restart mariadb
[root@jason ~]# mysql
MariaDB [(none)]> update mysql.user set password=password("123") where user="root" and host="localhost";
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> \q
[root@jason ~]# #打開/etc/my.cnf去掉skip-grant-table,然后重啟
[root@jason ~]# systemctl restart mariadb
[root@jason ~]# mysql -u root -p123 # 以新密碼登錄
方法三:
# 1、先關閉數據庫服務
方式一
systemctl stop mysql
方式二
mysqladmin -uroot -p123 -S /tmp/mysql.sock shutdown
# 2、跳過授權表啟動mysql
mysqld_safe --skip-grant-tables --skip-networking &
ps:還需要跳過網絡,否則在操作過程中很不安全
# 3、登錄並修改密碼
[root@jason ~]# mysql
MariaDB [(none)]> update mysql.user set password=password("123") where user="root" and host="localhost";
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> \q
# 4、重啟服務
systemctl start mysql
# 5、用修改后的密碼登錄即可
4.2 windows平台下
5.7版本mysql
方式一
#1 關閉mysql
#2 在cmd中執行:mysqld --skip-grant-tables
#3 在cmd中執行:mysql
#4 執行如下sql:
update mysql.user set authentication_string=password('') where user = 'root';
flush privileges;
#5 tskill mysqld #或taskkill -f /PID 7832
#6 重新啟動mysql
方式二
#1. 關閉mysql,可以用tskill mysqld將其殺死
#2. 在解壓目錄下,新建mysql配置文件my.ini
#3. my.ini內容,指定
[mysqld]
skip-grant-tables
#4.啟動mysqld
#5.在cmd里直接輸入mysql登錄,然后操作
update mysql.user set authentication_string=password('') where user='root and host='localhost';
flush privileges;
#6.注釋my.ini中的skip-grant-tables,然后啟動myqsld,然后就可以以新密碼登錄了