mysql(二)-用戶管理與權限

用戶相關是存放在mysql.user表中,可使用desc查看錶結構mysql

MySQL大小寫詳情:sql

一、數據庫名嚴格區分大小寫
二、表名嚴格區分大小寫的
三、表的別名嚴格區分大小寫
四、變量名嚴格區分大小寫
五、列名在全部的狀況下均忽略大小寫
六、列的別名在全部的狀況下均忽略大小寫

用戶管理

用戶格式

用戶名@可登陸主機
user@host

host:host能夠爲主機名,也能夠爲IP地址,mysql裏主機名和IP地址屬於不一樣的主機;

host可使用通配符
通配符 表明含義
_ 任意單個字符
% 任意長度的任意字符
例子 表明
% 全部主機
192.168.%.% 全部以192.168.開頭的主機

建立用戶

格式:
方法一:此種方法不會授於用戶權限
create user '用戶名'@'可登陸主機' identified by '密碼'

CREATE USER 'hunk'@'localhost' IDENTIFIED BY '123456';

方法二:
grant 權限 on 數據庫.* to '用戶名'@'可登陸主機' identified by '密碼';

grant all on *.* to 'hunk2'@'%' identified by '123456';
受權格式:
grant 權限 on 數據庫對象 to 用戶
grant 權限 on 數據庫.* to 用戶名@登陸主機 identified by '密碼';
grant select on testdb.* to common_user@'%'';               受權指定用戶對指定數據查詢權限
grant select on testdb.* to user1@'localhost',user2@'localhost';               受權多個用戶對指定數據查詢權限
grant create,delete,insert,update on testdb.* to developer@'192.168.0.%';        受權指定用戶對指定數據建立,刪除,增長,更新權限
grant select (name,age) on dbname.tables to hunk@'localhost';                      受權指定用戶對指定表中的某些字段查詢權限
grant ALL on testdb.* to developer@'192.168.0.%' whit grant option;        受權指定用戶對指定數據建立,刪除,增長,更新權限,而且受權用戶能夠爲其餘用戶受權(謹慎)

grant usage on *.* to 'hunk'@'192.168.0.1' require ssl;     強制用戶使用ssl鏈接
grant usage on *.* to 'hunk'@'192.168.0.1' require none;     強制用戶使用ssl鏈接

EVOKE ALL ON *.* FROM 'username'@'localhost';    回收指定用戶所有權限
EVOKE update ON *.* FROM 'username'@'localhost';    回收指定用戶update權限
Show grants; 查看當前用戶(本身)權限
show grants for dba@localhost                               查看其餘用戶權限

設置用戶權限
grant select on legacy.* to mary@'localhost' identified by ‘mary_password’;
grant select,insert,update,delete on legacy.* to legacy@'localhost' identified by ‘legacy_password’;
grant select on legacy.* to report@'localhost" identified by ‘report_password’;

MySQL grant 權限,分別能夠做用在多個層次上。
1. grant 做用在整個 MySQL 服務器
2. grant 做用在單個數據庫
3. grant 做用在單個數據表
4. grant 做用在表中的列
5. grant 做用在存儲過程、函數
權限列表:
權限
權限說明
ALTER 修改表和索引
CREATE 刪除表中已有的記錄
DELETE 建立數據庫和表
DROP 拋棄(刪除)數據庫和表
INDEX 建立或拋棄索引
INSERT 向表中插入新行
SELECT 查詢表中的記錄
UPDATE 修改現存表記錄
EXECUTE 執行權
FILE 讀或寫服務器上的文件
PROCESS 查看服務器中執行的線程信息或殺死線程
RELOAD 重載受權表或清空日誌、主機緩存或表緩存
SHUTDOWN 關閉服務器
ALL 全部權限
USAGE 當一個用戶被建立時,mysql會自動授予其usage權限。usage權限只能用於登陸數據,不能執行其餘操做

重命名用戶

rename user 舊用戶名 to 新用戶名

修改用戶密碼

方法一:數據庫

/bin/mysqladmin -u root -p123456 password '新密碼'

方法二:緩存

登陸到mysql
set password for hunk2=password("hunk");
flush privileges;

方法三:服務器

登陸到mysql
update mysql.user set password=password("1234567") where user='hunk';
flush privileges;
必需要刷新表權限。

在mysql5.7中,mysql.user表的password字段已經被更改成authentication_string字段

查詢當前mysql進程列表,能夠查看登陸用戶ide

show processlist;
相關文章
相關標籤/搜索