為mysql使用者設定許可權 mysql的使用者許可權設定

2021-10-17 16:11:47 字數 1277 閱讀 4675

一、首先我們需要來看一下mysql預設資料庫裡面的四張表(user,db,tables_priv,columns_priv)。

1、user表(使用者層許可權)

因為欄位太多,只擷取了一部分。首先登陸的時候驗證host,user,password也就是ip,使用者名稱,密碼是否匹配,匹配登陸成功將會為登陸者分配許可權,分配許可權的順序也是按照上面四張表的排列順序進行的,舉個例子,如果user表的select_priv為y說明他擁有所有表的查詢許可權,如果為n就需要到下一級db表中進行許可權分配了。其中的%是萬用字元,代表任意的意思。

2、db表(資料庫層許可權)

來到db表之後會匹配host,user然後會根據db欄位對應的表進行許可權分配,像select_priv這些字段對應的許可權大家應該都能看出來是對應著什麼許可權了吧,這裡不細說了(不偷懶,舉個例子select_priv,insert_priv,update_priv,delete_priv,create_priv,drop_priv分別代表著查詢,增加,更新,刪除,建立,銷毀)。其中y代表這擁有此項許可權,n則代表沒有此項許可權。

3、tables_priv表(表層許可權)

與上面一樣,這是通過host,db,user,table來進行定位到表層的乙個許可權分配。不過它只有table_priv和column_priv兩個欄位來記錄許可權。

4、columns_priv表(欄位層許可權)

顧名思義,欄位層許可權,通過host,db,user,table,column來進行定位到字段層的乙個許可權分配,只有column_priv來記錄許可權。

二、使用grant命令來分配許可權

大致格式:grant 許可權 on 資料庫物件 to 使用者 identified by 密碼

我們通過兩條命令來解讀下:

grant all on test.* to 'root'@'%' identified by '123456' with grant option;

上面這條命令的意思是給root@%這個使用者對應的test資料庫分配所有操作許可權,identified by是需要進行的乙個密碼認證。把all換成usage就表示這個使用者除了登陸啥都幹不了。

grant select, insert, update, delete on test.* to 'root'@'%' identified by '123456' with grant option;

上面這條命令則是給root@%這個使用者對應的test資料庫分配了增刪改查的操作許可權。

檢視某個使用者的許可權:show grants for 'root'@'%';

檢視當前使用者: select current_user();

Mysql 使用者許可權設定

關於mysql的使用者管理,筆記 1 建立新使用者 通過root使用者登入之後建立 grant all privileges on to testuser localhost identified by 123456 建立新使用者,使用者名為testuser,密碼為123456 grant all ...

mysql許可權使用者設定

drop user employa localhost 刪除乙個使用者 新建三個使用者 create user employeea localhost identified by 1234 create user employeeb localhost identified by 1234 crea...

mysql使用者許可權設定

1 建立新使用者 通過root使用者登入之後建立 grant all privileges on to testuser localhost identified by 123456 建立新使用者,使用者名為testuser,密碼為123456 grant all privileges on to ...