MySQL 安全控制
DCL(Data Control Language 數(shù)據(jù)庫(kù)控制語(yǔ)言)
用于數(shù)據(jù)庫(kù)授權(quán)双藕、角色控制等操作
GRANT 授權(quán),為用戶賦予訪問(wèn)權(quán)限
REVOKE 取消授權(quán),撤回授權(quán)權(quán)限
用戶管理
創(chuàng)建用戶
create user '用戶名'@'IP地址' identified by '密碼';
刪除用戶
drop user '用戶名'@'IP地址';
修改用戶
rename user '用戶名'@'IP地址' to '新用戶名'@'IP地址' ;
修改密碼
// 第一種方法:
set password for '用戶名'@'IP地址'=Password('新密碼')
// 第二種方法:
alter user '用戶名'@'IP地址' identified by '新密碼';
// 第三種方法(忘記密碼時(shí)珊拼,必須使用此方法修改密碼):
UPDATE mysql.user SET authentication_string='' WHERE user='root' and host='localhost';
PS:用戶權(quán)限相關(guān)數(shù)據(jù)保存在mysql數(shù)據(jù)庫(kù)的user表中浆洗,所以也可以直接對(duì)其進(jìn)行操作(不建議)
權(quán)限管理
grant 權(quán)限 on 數(shù)據(jù)庫(kù).表 to '用戶'@'IP地址' identified by '密碼'; -- 授權(quán)并設(shè)置密碼
revoke 權(quán)限 on 數(shù)據(jù)庫(kù).表 from '用戶'@'IP地址' -- 取消權(quán)限
查看授權(quán)信息
查看授權(quán)語(yǔ)句
show grants for '用戶'@'IP地址';
查看生效的授權(quán)信息
針對(duì)所有庫(kù)和表的權(quán)限,比如 . 嘶摊。 去 mysql.user 中查看
select * from mysql.user where user='shark'\G
針對(duì)具體到庫(kù)的權(quán)限延蟹,比如db_name.* 叶堆。 去 mysql.db 中查看
select * from mysql.db where user='shark'\G
針對(duì)具體表的授權(quán),在 mysql.tables_priv 中查看
select * from mysql.tables_priv where user='shark'\G
假如是 MySQL8.x
CREATE USER '你的用戶名'@'localhost' IDENTIFIED BY '你的密碼';
#創(chuàng)建新的用戶
GRANT ALL PRIVILEGES ON 你的數(shù)據(jù)庫(kù)名.* TO '你的用戶名'@'localhost';
#把剛剛創(chuàng)建的數(shù)據(jù)庫(kù)的管理權(quán)限給予剛剛創(chuàng)建的MySQL用戶
FLUSH PRIVILEGES;
#刷新權(quán)限,使用設(shè)置生效
關(guān)于權(quán)限
all privileges 除grant外的所有權(quán)限
select 僅查權(quán)限
select,insert 查和插入權(quán)限
...
usage 無(wú)訪問(wèn)權(quán)限
alter 使用alter table
alter routine 使用alter procedure和drop procedure
create 使用create table
create routine 使用create procedure
create temporary tables 使用create temporary tables
create user 使用create user蔗喂、drop user、rename user和revoke all privileges
create view 使用create view
delete 使用delete
drop 使用drop table
execute 使用call和存儲(chǔ)過(guò)程
file 使用select into outfile 和 load data infile
grant option 使用grant 和 revoke
index 使用index
insert 使用insert
lock tables 使用lock table
process 使用show full processlist
show databases 使用show databases
show view 使用show view
update 使用update
reload 使用flush
shutdown 使用mysqladmin shutdown(關(guān)閉MySQL)
super 使用change master高帖、kill缰儿、logs、purge乖阵、master和set global。還允許mysqladmin調(diào)試登陸
replication client 服務(wù)器位置的訪問(wèn)
replication slave 由復(fù)制從屬使用
關(guān)于數(shù)據(jù)庫(kù)和表
對(duì)于目標(biāo)數(shù)據(jù)庫(kù)以及內(nèi)部其他:
數(shù)據(jù)庫(kù)名.* 數(shù)據(jù)庫(kù)中的所有
數(shù)據(jù)庫(kù)名.表 指定數(shù)據(jù)庫(kù)中的某張表
數(shù)據(jù)庫(kù)名.存儲(chǔ)過(guò)程 指定數(shù)據(jù)庫(kù)中的存儲(chǔ)過(guò)程
*.* 所有數(shù)據(jù)庫(kù)
關(guān)于用戶和 IP
用戶名@IP地址 用戶只能在改IP下才能訪問(wèn)
用戶名@192.168.1.% 用戶只能在改IP段下才能訪問(wèn)(通配符%表示任意)
用戶名@%.shark.com
用戶名@% 用戶可以再任意IP下訪問(wèn)(默認(rèn)IP地址為%)
Example
create user 'shark'@'';
grant all privileges on *.* to 'shark'@'%' identified by '123';
立刻生效
/*將數(shù)據(jù)讀取到內(nèi)存中瞪浸,從而立即生效。*/
flush privileges
也可以在創(chuàng)建用戶的同時(shí)直接授權(quán)(mysql8.x 不可以)
grant select on *.* /*設(shè)置查詢數(shù)據(jù)的權(quán)限在所有的庫(kù)和表*/
to 'shark_2'@"%" /*指定用戶名和來(lái)源 ip*/
identified by '123'; /*設(shè)置密碼*/
免費(fèi)的資源师崎,我們往往不知道去珍惜默终!點(diǎn)個(gè)關(guān)注和喜歡吧
作者:xiguatian
鏈接:http://www.reibang.com/p/394ded3e923c
來(lái)源:簡(jiǎn)書(shū)
簡(jiǎn)書(shū)著作權(quán)歸作者所有,任何形式的轉(zhuǎn)載都請(qǐng)聯(lián)系作者獲得授權(quán)并注明出處犁罩。