flush Privileges
權限存儲在mysql庫的user,db,tables_priv,columns_priv,procs_priv這幾個系統表中,待mysql實例啓動後就加載到內存中。node
查看mysql實例默認root用戶的權限(來自localhost)mysql
mysql> show grants for root@localhost\G; too many records ............ mysql> select * from db where user='root' and host='localhost'; Empty set (0.00 sec) mysql> select * from tables_priv where user='root' and host='localhost'; Empty set (0.00 sec) mysql> select * from columns_priv where user='root' and host='localhost'; Empty set (0.00 sec) mysql> select * from procs_pr
mysql> create user abc@localhost identified by '123456'; Query OK, 0 rows affected (0.07 sec) mysql> show grants for abc@localhost; +-----------------------------------------+ | Grants for abc@localhost | +-----------------------------------------+ | GRANT USAGE ON *.* TO `abc`@`localhost` | +-----------------------------------------+ 1 row in set (0.00 sec)
權限存儲在mysql庫的user,db,tables_priv,columns_priv, procs_priv這幾個系統表中,待mysql啓動後加載到內存中。linux
routine_type是表明存儲過程仍是函數的類型sql
create user ABC@localhost identified by '123456'; create user abc@localhost identified by '123456';
上面是建立了2個用戶。數據庫
create user abc@localhost identified by '123456'; create user abc@Localhost identified by '123456';
上面兩條命令仍是同一個用戶同一個主機。ide
mysql> show grants for abc@localhost; +-----------------------------------------+ | Grants for abc@localhost | +-----------------------------------------+ | GRANT USAGE ON *.* TO `abc`@`localhost` | +-----------------------------------------+ 1 row in set (0.00 sec) mysql> show create user root@localhost; '''''省略1W字
具體含義參考下表:函數
建立mysql用戶由兩種方式,ui
例一:this
mysql> create user 'abc1'@localhost identified by '123456' ; mysql> grant all privileges on *.* to 'abc1'@'localhost' with grant option;
例二:日誌
mysql> create user 'abc2'@'%' identified by '123456'; mysql> grant all privileges on *.* to 'abc2'@'%' with grant option;
例三:
mysql> create user 'abc3'@'localhost' identified by '123456'; mysql> grant reload,process on *.* to 'abc3'@'localhost'; mysql> grant select(id) on test.tmp to abc3@localhost;
例四:
mysql> create user 'custom'@'example1.node.com' identified by '123456'; Query OK, 0 rows affected (0.12 sec) mysql> grant select,insert,update,delete,create,drop on test.tmp to custom@'example1.node.com' ;
經過revoke來回收用戶權限,語法以下:
revoke 權限名 on 庫名.表名 from 用戶名@主機名
例子一:
mysql> show grants for custom@'example1.node.com'; +----------------------------------------------------------------------------------------------------+ | Grants for custom@example1.node.com | +----------------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO `custom`@`example1.node.com` | | GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP ON `test`.`tmp` TO `custom`@`example1.node.com` | +----------------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec) mysql> revoke delete on test.tmp from custom@'example1.node.com'; #剔除delete權限 Query OK, 0 rows affected (0.10 sec) mysql> show grants for custom@'example1.node.com'; +--------------------------------------------------------------------------------------------+ | Grants for custom@example1.node.com | +--------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO `custom`@`example1.node.com` | | GRANT SELECT, INSERT, UPDATE, CREATE, DROP ON `test`.`tmp` TO `custom`@`example1.node.com` | # delete權限消除啦! +--------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec)
經過drop來刪除用戶。語法以下
drop user 用戶名@主機名
例子1
mysql> drop user 'custom'@'example1.node.com'; Query OK, 0 rows affected (0.02 sec)
若是不想對某個用戶進行限制了,直接把對應的值改成0便可。
ps:
mysql> create user 'abc4'@'localhost' identified by '123456' with max_queries_per_hour 20 -> max_updates_per_hour 10 -> max_connections_per_hour 3 -> max_user_connections 2;
show create user
來查看詳細信息。mysql> alter user 'abc4'@'localhost' with MAX_QUERIES_PER_HOUR 3;
alter user 'abc4'@'localhost' identified by 'abc4';
設置系統參數default_password_lifetime做用於全部的用戶帳戶
若是爲每一個用戶設置了密碼過時策略,則會覆蓋上述系統參數。
alter user 'abc1'@'localhost' password expire interval 90 day;
90天過時時間alter user 'abc3'@'localhost' password expire default;
默認過時策略alter user 'abc1'@'localhost' password expire never;
密碼不過時手動強制某個用戶密碼過時
alter user 'abc1'@'localhost' password expire;
[root@linux-node2 ~]# mysqladmin -uabc3 -hlocalhost password '123456' -p mysql> select 1+2; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
修改密碼後看看效果
mysql> alter user user() identified by '123456'; Query OK, 0 rows affected (0.21 sec) # 切換到另外一個終端,使用剛纔abc3用戶的鏈接 mysql> select 1+2 -> ; +-----+ | 1+2 | +-----+ | 3 | +-----+ 1 row in set (0.08 sec)
經過執行create user/alter user 命令中帶account locak/unlock子句設置用戶的lock狀態。
create user abc5@localhost identified by '123456' account lock;
mysql> alter user 'abc2'@localhost account lock; ERROR 1819 (HY000): Your password does not satisfy the current policy requirements 請注意,若是報上面的錯誤(錯誤代碼爲1819),請檢查這個用戶是否存在。 mysql> alter user 'abc3'@localhost account lock; # 存在就會更改爲功 Query OK, 0 rows affected (0.09 sec)