mysql用戶創建與授權

在多用戶情形下,爲了避免用戶訪問或者操作不屬於自己的數據,需要使用用戶權限將不同用戶的數據隔離開來。mysql的用戶授權可以精確到表、操作和某個IP地址。

創建用戶

create user 'username'@'host' identified by 'password';

用法說明

username : 用戶名

host : ip地址,用戶在哪個主機登錄,如果是本機可以使用localhost,如果允許任意主機使用%代替

password : 用戶登錄密碼,如果沒有密碼留空

示例

create user 'guest'@'%' identified by '12345678';

create user 'guest'@'localhost' identified by '';

create user 'guest'@'%';

授權

grant [privileges] on [database.tablename] to 'username'@'host';

用法說明

privileges : 授予用戶的權限,包括select, insert, update, delete。如何授予所有權限,可以使用all代替

database : 數據名

tablename : 表名,如果是所有表格,可以使用*代替

示例

grant select on test.user to 'guest'@'localhost';

grant all on *.* to 'guest'@'%';

注意,上述授權的用戶不能給其他用戶授權,即不能執行grant命令,如果需要授予該權限,可以在命令後追加with grant option。

grant [privileges] on [database.tablename] to 'username'@'host' with grant option;

撤消授權

revoke [privileges] on [database].[tablename] from 'username'@'host';

用法說明

privileges, database和tablename用法和授權部分相同。

示例

revoke all on test.user from 'guest'@'localhost';

查看用戶權限

show grants for 'username'@'host';

示例

show grants for 'guest'@'localhost';

修改用戶密碼

set password for 'username'@'host' = password('newpassword');

示例

set password for 'guest'@'localhost' = password('12345678');

刪除用戶

drop user 'username'@'host';

示例

drop user 'guest'@'localhost';

參考

MySQL創建用戶與授權

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章