MySQL使用者許可權管理

2021-06-03 15:24:28 字數 4243 閱讀 2783

(網際網路文件整理)

mysql的使用者管理,指的是哪個使用者可以連線伺服器,從**連線,連線後能做什麼.

mysql中grant語句建立mysql使用者並指定其許可權,而revoke語句刪除許可權。兩條語句實現了mysql資料庫的使用者管理,並提供與直接操作這些表的內容不同的另一種方法。

create和revoke語句影響4個表:授權表內容

user 能連線伺服器的使用者以及他們擁有的任何全域性許可權

db 資料庫級許可權

tables_priv 表級許可權

columns_priv 列級許可權

還有第5個授權表(host),但它不受grant和revoke的影響。

當你對乙個使用者發出一條grant語句時,在user表中為該使用者建立一條記錄。

如果語句指定任何全域性許可權(管理許可權或適用於所有資料庫的許可權),這些也記錄在user表中。如果你指定資料庫、表和列級許可權,他們被分別記錄在db、tables_priv和columns_priv表中。

本篇將以例項的形式講解建立刪除mysql使用者許可權...

1.建立使用者並授權

grant語句的語法:

grant privileges (columns) on what to 'user'@'%' identified by 'password' with grant option

要使用該句型,需確定欄位有:

privileges

許可權指定符許可權允許的操作 

alter 修改表和索引

create 建立資料庫和表

delete 刪除表中已有的記錄

drop 拋棄(刪除)資料庫和表

index 建立或拋棄索引

insert 向表中插入新行

reference 未用

select 檢索表中的記錄

update 修改現存表記錄

file 讀或寫伺服器上的檔案

process 檢視伺服器中執行的執行緒資訊或殺死執行緒

reload 過載授權表或清空日誌、主機快取或表快取。

shutdown 關閉伺服器

all 所有;all privileges同義詞

usage 特殊的「無許可權」許可權

以上許可權分三組:

第一組:適用於資料庫、表和列如:alter create delete drop index insert select update

第二組:數管理許可權 它們允許使用者影響伺服器的操作 需嚴格地授權 如:file process reload shut*

第三組:許可權特殊 all意味著「所有許可權」 uasge意味著無許可權,即建立使用者,但不授予許可權

columns

許可權運用的列(可選)並且你只能設定列特定的許可權。如果命令有多於乙個列,應該用逗號分開它們。

what

許可權運用的級別。許可權可以是全域性,定資料庫或特定表.

user

許可權授予的使用者,由乙個使用者名稱和主機名組成,許兩個同名使用者從不同地方連線.預設:mysql使用者

password

賦予使用者的口令(可選),如果你對使用者沒有指定identified by子句,該使用者口令不變.

用identified by時,口令字串用改用口令的字面含義,grant將為你編碼口令.

注:set password使用password()函式

with grant option

使用者可以授予許可權通過grant語句授權給其它使用者(可選)

例項講解:

注:以下連線區域,使用者名稱及密碼應加'',如 'huaying'@'koowo.com'

grant all on db_book.* to 'huaying'@'koowo.com

' identified by 'yeelion' 只能在本地連線

grant all on db_book.* to 'huaying'@'vpn.koowo.com'

identified by 'yeeliong' 允許從此域連線 

grant all on db_book.* to 'huaying'@'%

' identified by 'yeelion' 允許從任何主機連線

注:"%"字元起萬用字元作用,與like模式匹配的含義相同,yeelion為口令.

grant all on db_book.* to 'huaying'@%.koowo.com

identified by 'yeelion';

允許huaying從koowo.com域的任何主機連線 

grant all on db_book.* to 'huaying'@'192.168.1.189

' identified by 'yeelion';

grant all on db_book.* to 'huaying'@'192.168.1.%

' identified by 'yeelion'

grant all on db_book.* to 'huaying'@'192.168.1.0/17'

identified by 'yeelion'

允許從單ip 段ip或一子網ip登陸 

注:有時 使用者@ip 需用引號 如'huaying'@'192.168.1.0/17

' grant all on *.* to 'huaying'@'localhost

' identified by 'yeelion' with grant option

新增超級使用者huaying 可在本地登陸做任何操作.

grant reload on *.* to 'huaying'@'localhost'

identified by 'yeelion' 只賦予reload許可權

grant all on db_book to 'huaying'@'koowo.com'

indetified by 'yeelion' 所有許可權

grant select on db_book to 'huaying'@'%

' indetified by 'yeelion' 唯讀許可權

grant select,insert,delete,update on db_book to 'huaying'@'koowo.com

' indetified by 'yeelion'

只有select,insert,delete,update的許可權

grant select on db_book.storybook to 'huaying'@'localhost

' indetified by 'yeelion' 只對錶

grant update (name) on db_book.storybook to 'huaying'@'localhost

' 只對錶的name列 密碼不變

grant update (id,name,author) on db_book.storybook to 'huaying'@'localhost

'  只對錶的多列

grant all on book.* to ''@'koowo.com

' 允許koowo.com域中的所有使用者使用庫book

grant all on book.* to 'huaying'@'%.koowo.com'

indetified by 'yeelion' with grant option

允許huaying對庫book所有表的管理員授權.

flush privileges; 過載授權表 操作為更新,使之生效。

2.檢視撤權並刪除使用者許可權。

show grants; 檢視當前使用者(自己)許可權

show grants for db_user@localhost; 檢視其他 mysql 使用者許可權:

revoke 跟 grant 的語法差不多,只需要把關鍵字 「to」 換成 「from」 即可:

to用from取代,沒有indetifed by和with grant option子句. 如下:

revoke privileges (columns) on what from user

user:必須匹配原來grant語句的你想撤權的使用者的user部分。

privileges:不需匹配,可以用grant語句授權,然後用revoke語句只撤銷部分許可權。

revoke語句只刪許可權不刪使用者,撤銷了所有許可權後user表中使用者記錄保留,使用者仍然可以連線伺服器.

要完全刪除乙個使用者必須用一條delete語句明確從user表中刪除使用者記錄:

delete from user where user='huaying'

flush privileges; 過載授權表

注:使

mysql 使用者許可權管理

mysql 中顯示所有使用者 select distinct concat user user,host,as query from mysql.user 檢視乙個使用者的許可權 show grants for company 為使用者授權 grant select on b2b search.to...

MySQL使用者許可權管理

使用者許可權管理主要有以下作用 1.可以限制使用者訪問哪些庫 哪些表 2.可以限制使用者對哪些表執行select create delete delete alter等操作 3.可以限制使用者登入的ip或網域名稱 4.可以限制使用者自己的許可權是否可以授權給別的使用者 mysql grant all...

mysql 使用者許可權管理

root使用者登入 mysql uroot p x 切換到mysql use mysql 建立資料庫 create database db 建立使用者 create user username host identified by password host預設值為 修改密碼 update user...