转: MySQL 赋予用户权限(grant %-远程和localhost-本地区别)
2019獨角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
??相關(guān)參考資料:
MySQL 賦予用戶權(quán)限命令的簡單格式可概括為:
grant 權(quán)限 on 數(shù)據(jù)庫對象 to 用戶
一、grant 普通數(shù)據(jù)用戶,查詢、插入、更新、刪除 數(shù)據(jù)庫中所有表數(shù)據(jù)的權(quán)利。
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 數(shù)據(jù)庫開發(fā)人員,創(chuàng)建表、索引、視圖、存儲過程、函數(shù)。。。等權(quán)限。
grant 創(chuàng)建、修改、刪除 MySQL 數(shù)據(jù)表結(jié)構(gòu)權(quán)限。
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 外鍵權(quán)限。
grant references on testdb.* to developer@'192.168.0.%';
grant 操作 MySQL 臨時表權(quán)限。
grant create temporary tables on testdb.* to developer@'192.168.0.%';
grant 操作 MySQL 索引權(quán)限。
grant index on??testdb.* to developer@'192.168.0.%';
grant 操作 MySQL 視圖、查看視圖源代碼 權(quán)限。
grant create view on testdb.* to developer@'192.168.0.%';
grant show???view on testdb.* to developer@'192.168.0.%';
grant 操作 MySQL 存儲過程、函數(shù) 權(quán)限。
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.%';
三、grant 普通 DBA 管理某個 MySQL 數(shù)據(jù)庫的權(quán)限。
grant all privileges on testdb to dba@'localhost'
其中,關(guān)鍵字 “privileges” 可以省略。
四、grant 高級 DBA 管理 MySQL 中所有數(shù)據(jù)庫的權(quán)限。
grant all on *.* to dba@'localhost'
五、MySQL grant 權(quán)限,分別可以作用在多個層次上。
1. grant 作用在整個 MySQL 服務(wù)器上:
grant select on *.* to dba@localhost ; -- dba 可以查詢 MySQL 中所有數(shù)據(jù)庫中的表。
??grant all????on *.* to dba??@localhost ; -- dba 可以管理 MySQL 中的所有數(shù)據(jù)庫
2. grant 作用在單個數(shù)據(jù)庫上:
??grant select on testdb.* to dba@localhost ; -- dba 可以查詢 testdb 中的表。
3. grant 作用在單個數(shù)據(jù)表上:
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 作用在存儲過程、函數(shù)上:
grant execute on procedure testdb.pr_add to 'dba'@'localhost'
grant execute on function??testdb.fn_add to 'dba'@'localhost'
六、查看 MySQL 用戶權(quán)限
查看當(dāng)前用戶(自己)權(quán)限:
show grants;
查看其他 MySQL 用戶權(quán)限:
show grants for dba@localhost;
七、撤銷已經(jīng)賦予給 MySQL 用戶權(quán)限的權(quán)限。
revoke 跟 grant 的語法差不多,只需要把關(guān)鍵字 “to” 換成 “from” 即可:
grant??all on *.* to???dba@localhost;
revoke all on *.* from dba@localhost;
八、MySQL grant、revoke 用戶權(quán)限注意事項
1. grant, revoke 用戶權(quán)限后,該用戶只有重新連接 MySQL 數(shù)據(jù)庫,權(quán)限才能生效。
2. 如果想讓授權(quán)的用戶,也可以將這些權(quán)限 grant 給其他用戶,需要選項 “grant option“
grant select on testdb.* to dba@localhost with grant option;
這個特性一般用不到。實際中,數(shù)據(jù)庫權(quán)限最好由 DBA 來統(tǒng)一管理。
?
----------------------------------------------------------------------------------------------
授權(quán)命令GRANT 語句的語法如下:
GRANT privileges (columns)
??ON what
TO user IDENTIFIEDBY "password"??
WITH GRANT OPTION
對用戶授權(quán)
mysql>grant rights on database.* to user@host identified by "pass";
例1:
增加一個用戶test1密碼為abc,讓他可以在任何主機上登錄,并對所有數(shù)據(jù)庫有查詢、插入、修改、刪除的權(quán)限。
grant select,insert,update,delete on *.* to test1@"%" Identified by "abc";
ON 子句中*.* 說明符的意思是“所有數(shù)據(jù)庫,所有的表”
例2:
增加一個用戶test2密碼為abc, 讓他只可以在localhost上登錄,并可以對數(shù)據(jù)庫mydb進行查詢、插入、修改、刪除的操作。
grant select,insert,update,delete on mydb.* to test2@localhost identified by "abc";
例子3
增加一個用戶custom,他能從主機localhost、server.domain和whitehouse.gov連接。他只想要從 localhost存取bankaccount數(shù)據(jù)庫,從whitehouse.gov存取expenses數(shù)據(jù)庫和從所有3臺主機存取customer 數(shù)據(jù)庫。他想要從所有3臺主機上使用口令stupid。
為了使用GRANT語句設(shè)置個用戶的權(quán)限,運行這些命令:
shell> mysql --user=root mysql
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
ON bankaccount.* TO custom@localhost IDENTIFIED BY 'stupid';
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
ON expenses.* TO custom@whitehouse.gov IDENTIFIED BY 'stupid';
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
ON customer.* TO custom@'%' IDENTIFIED BY 'stupid';
==============================================
權(quán)限信息用user、db、host、tables_priv和columns_priv表被存儲在mysql數(shù)據(jù)庫中(即在名為mysql的數(shù)據(jù)庫中)。
權(quán)限 列 Context
select Select_priv 表
insert Insert_priv 表
update Update_priv 表
delete Delete_priv 表
index Index_priv 表
alter Alter_priv 表
create Create_priv 數(shù)據(jù)庫、表或索引
drop Drop_priv 數(shù)據(jù)庫或表
grant Grant_priv 數(shù)據(jù)庫或表
references References_priv 數(shù)據(jù)庫或表
reload Reload_priv 服務(wù)器管理
shutdown Shutdown_priv 服務(wù)器管理
process Process_priv 服務(wù)器管理
file File_priv 在服務(wù)器上的文件存取
1.select、insert、update和delete權(quán)限 允許你在一個數(shù)據(jù)庫現(xiàn)有的表上實施操作,是基本權(quán)限
2.alter權(quán)限允許你使用ALTER TABLE
3.create和drop權(quán)限允許你創(chuàng)建新的數(shù)據(jù)庫和表,或拋棄(刪除)現(xiàn)存的數(shù)據(jù)庫和表 如果你將mysql數(shù)據(jù)庫的drop權(quán)限授予一個用戶,該用戶能拋棄存儲了MySQL存取權(quán)限的數(shù)據(jù)庫!
4.grant權(quán)限允許你把你自己擁有的那些權(quán)限授給其他的用戶。
你不能明顯地指定一個給定用戶應(yīng)該被拒絕存取。即,你不能明顯地匹配一個用戶并且然后拒絕連接。你不能指定一個用戶有權(quán)創(chuàng)建立或拋棄一個數(shù)據(jù)庫中的表,也不能創(chuàng)建或拋棄數(shù)據(jù)庫本身。 可以同時列出許多被授予的單個權(quán)限。
例如,如果想讓用戶能讀取和修改已有表的內(nèi)容,但又不允許創(chuàng)建新表或刪除表,可按如下授權(quán):
GRANT SELECT,INSERT,DELETE,UPDATE ON samp_db.* TO 'user'@'%' IDENTIFIEDBY "pass"
以上是我從別的地方拷貝過來后稍作修改的文字,下面自己寫一些需要注意的東西。
為什么使用了Grant all on db.* to user identified by "pass"后,在主機上訪問數(shù)據(jù)庫還會出現(xiàn)ERROR 1045 (28000): Access denied for user 'user'@'localhost' (using password: YES) 的錯誤提示?
解答方法如下:運行命令 Grant all on db.* to 'user'@'localhost' identified by "pass"
原因是:當(dāng)不加@選項時,效果與加@'%'是一樣的,'%'從名義上包括任何主機,(%必須加上引號,不然與@放在一起可能不會被辨認出。)不過有些時候(有些版本)'%'不包括localhost,要單獨對@'localhost'進行賦值。????????
轉(zhuǎn)載于:https://my.oschina.net/u/2331760/blog/390407
總結(jié)
以上是生活随笔為你收集整理的转: MySQL 赋予用户权限(grant %-远程和localhost-本地区别)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【树莓派学习笔记】六、启用摄像头、实时视
- 下一篇: 怎么快速了解自己的MySQL服务器?