天天看點

mysql 建立使用者,指定資料庫,表的讀寫權限常用指令

作者:琴島藍

老是記不住mysql指令,然後網上找了點資料,然後記錄下來,友善查詢使用

1.遠端登入mysql

mysql -h ip -u root -p 密碼           

2.建立使用者

格式:grant 權限 on 資料庫.* to 使用者名@登入主機 identified by "密碼";

例1:增加一個test1使用者,密碼為123456,可以在任何主機上登入,并對所有資料庫有查詢,增加,修改和删除的功能。需要在mysql的root使用者下進行

mysql>grant select,insert,update,delete on *.* to test1@"%" identified by "123456";
mysql>flush privileges;           

例2:增加一個test2使用者,密碼為123456,隻能在192.168.2.12上登入,并對資料庫student有查詢,增加,修改和删除的功能。需要在mysql的root使用者下進行

mysql>grant select,insert,update,delete on student.* to [email protected] identified by “123456″;
mysql>flush privileges;           

添加使用者時可能會報 Your password does not satisfy the current policy requirements

可借鑒另一篇Mysql密碼政策問題 https://www.cnblogs.com/livedian/p/12743031.html

例3:授權使用者test3擁有資料庫student的所有權限

mysql>grant all privileges on student.* to test3@localhost identified by ’123456′;
mysql>flush privileges;           

3.修改使用者密碼

mysql>update mysql.user set password=password(’123456′) where User=’test1′ and Host=’localhost’;
mysql>flush privileges;           

4.删除使用者

mysql>delete from user where user=’test2′ and host=’localhost’;
mysql>flush privileges;           

5.删除資料庫和删除表

mysql>drop database 資料庫名;
mysql>drop table 表名;           

6.删除賬戶及權限

drop user 使用者名@’%’
drop user 使用者名@localhost           

**************************************************************************************

grant 詳細解析如下:

**************************************************************************************

MySQL 賦予使用者權限指令的簡單格式可概括為:

grant 權限 on 資料庫對象 to 使用者

一、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@’%’           

二、grant 資料庫開發人員,建立表、索引、視圖、存儲過程、函數。。。等權限。

mysql 建立使用者,指定資料庫,表的讀寫權限常用指令
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.%’;
grant 操作 MySQL 外鍵權限。
grant references on testdb.* to developer@’192.168.0.%’;
grant 操作 MySQL 臨時表權限。
grant create temporary tables on testdb.* to developer@’192.168.0.%’;
grant 操作 MySQL 索引權限。
grant index on testdb.* to developer@’192.168.0.%’;
grant 操作 MySQL 視圖、檢視視圖源代碼 權限。
grant create view on testdb.* to developer@’192.168.0.%’;
grant show view on testdb.* to developer@’192.168.0.%’;
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.%’;           
mysql 建立使用者,指定資料庫,表的讀寫權限常用指令

三、grant 普通 DBA 管理某個 MySQL 資料庫的權限。

grant all privileges on testdb to dba@’localhost’
其中,關鍵字 “privileges” 可以省略。           

四、grant 進階 DBA 管理 MySQL 中所有資料庫的權限。

grant all on *.* to dba@’localhost’           

五、MySQL grant 權限,分别可以作用在多個層次上。

1. grant 作用在整個 MySQL 伺服器上:           
grant select on *.* to dba@localhost; — dba 可以查詢 MySQL 中所有資料庫中的表。
grant all on *.* to dba@localhost; — dba 可以管理 MySQL 中的所有資料庫           

2. grant 作用在單個資料庫上:

grant select on testdb.* to dba@localhost; — dba 可以查詢 testdb 中的表。           

3. grant 作用在單個資料表上:

grant select, insert, update, delete on testdb.orders to dba@localhost;           

4. grant 作用在表中的列上:

grant select(id, se, rank) on testdb.apache_log to dba@localhost;           

5. grant 作用在存儲過程、函數上:

grant execute on procedure testdb.pr_add to ‘dba’@’localhost’
grant execute on function testdb.fn_add to ‘dba’@’localhost’           

六、檢視 MySQL 使用者權限

檢視目前使用者(自己)權限:
show grants;
檢視其他 MySQL 使用者權限:
show grants for dba@localhost;           

七、撤銷已經賦予給 MySQL 使用者權限的權限。

revoke 跟 grant 的文法差不多,隻需要把關鍵字 “to” 換成 “from” 即可:
grant all on *.* to dba@localhost;
revoke all on *.* from dba@localhost;           

八、MySQL grant、revoke 使用者權限注意事項

mysql 建立使用者,指定資料庫,表的讀寫權限常用指令
1. grant, revoke 使用者權限後,該使用者隻有重新連接配接 MySQL 資料庫,權限才能生效。
2. 如果想讓授權的使用者,也可以将這些權限 grant 給其他使用者,需要選項 “grant option“
grant select on testdb.* to dba@localhost with grant option;
這個特性一般用不到。實際中,資料庫權限最好由 DBA 來統一管理。
Category: Post
You can follow any responses to this entry via RSS.
Comments are currently closed, but you can trackback from your own site.           
mysql 建立使用者,指定資料庫,表的讀寫權限常用指令

=========================================================================

1.建立使用者并授權

grant語句的文法:
grant privileges (columns) on what to user identified by “password” with grant option           

要使用該句型,需确定字段有:

mysql 建立使用者,指定資料庫,表的讀寫權限常用指令
privileges 權限指定符權限允許的操作
alter 修改表和索引
create 建立資料庫和表
delete 删除表中已有的記錄
drop 抛棄(删除)資料庫和表
index 建立或抛棄索引
insert 向表中插入新行
reference 未用
select 檢索表中的記錄
update 修改現存表記錄
file 讀或寫伺服器上的檔案
process 檢視伺服器中執行的線程資訊或殺死線程
reload 重載授權表或清空日志、主機緩存或表緩存。
shutdown 關閉伺服器
all 所有;
all privileges同義詞
usage 特殊的“無權限”權限           
mysql 建立使用者,指定資料庫,表的讀寫權限常用指令

以上權限分三組:

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

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

第三組:權限特殊 all意味着“所有權限” uasge意味着無權限,即建立使用者,但不授予權限

mysql 建立使用者,指定資料庫,表的讀寫權限常用指令
columns
權限運用的列(可選)并且你隻能設定列特定的權限。如果指令有多于一個列,應該用逗号分開它們。
what
權限運用的級别。權限可以是全局,定資料庫或特定表.
user
權限授予的使用者,由一個使用者名和主機名組成,許兩個同名使用者從不同地方連接配接.預設:mysql使用者password           
mysql 建立使用者,指定資料庫,表的讀寫權限常用指令

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

用identified by時,密碼字元串用改用密碼的字面含義,grant将為你編碼密碼.

注:set password使用password()函數
with grant option           

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

執行個體講解:

mysql 建立使用者,指定資料庫,表的讀寫權限常用指令
grant all on db_book.* to [email protected] identified by “yeelion”   隻能在本地連接配接
grant all on db_book.* to [email protected] identified by “yeeliong”  允許從此域連接配接
grant all on db_book.* to huaying@% identified by “yeelion”   允許從任何主機連接配接 注:”%”字元起通配符作用,與like模式比對的含義相同。
grant all on db_book.* to huaying@%.koowo.com identified by “yeelion”;  允許huaying從koowo.com域的任何主機連接配接
grant all on db_book.* to [email protected] identified by “yeelion”
grant all on db_book.* to [email protected].% identified by “yeelion”
grant all on db_book.* to [email protected]/17 identified by “yeelion”           
mysql 建立使用者,指定資料庫,表的讀寫權限常用指令

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

注:有時 使用者@IP 需用引号 如”[email protected]/17″

grant all on *.* to huaying@localhost identified by “yeelion” with grant option           

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

mysql 建立使用者,指定資料庫,表的讀寫權限常用指令
grant reload on *.* to huaying@localhost identified by “yeelion” 隻賦予reload權限
grant all on db_book to [email protected] indetified by “yeelion” 所有權限
grant select on db_book to huaying@% indetified by “yeelion” 隻讀權限
grant select,insert,delete,update on db_book to [email protected] 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所有表的管理者授權.           
mysql 建立使用者,指定資料庫,表的讀寫權限常用指令

2.撤權并删除使用者

mysql 建立使用者,指定資料庫,表的讀寫權限常用指令
revoke的文法類似于grant語句
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 建立使用者,指定資料庫,表的讀寫權限常用指令

注:使用grant和revoke語句時,表自動重載,而你直接修改授權表時不是.

執行個體:

1.建立資料庫

CREATE DATABASE  `fypay` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;           

2.為建立的資料庫增加使用者fypay

grant create,select,insert,update,delete,drop,alter on fypay.* to fypay@”%” identified by “testfpay”;           

3.删除fypay使用者

delete from user where user=”fypay”
drop user fypay@localhost           

4.重新整理資料庫

flush privileges;            

下載下傳最新資源請通路分享吧:www.insharebbs.com

繼續閱讀