【Mysql】用户授权(GRANT)语法介绍和示例
-- 授权用户'user_name'@'host_name'使用密码'password'对数据库'db_name'有SELECT, INSERT, UPDATE权限
GRANT SELECT, INSERT, UPDATE ON db_name.* TO 'user_name'@'host_name' IDENTIFIED BY 'password';
-- 授权用户'user_name'@'host_name'对数据库'db_name'的表'table_name'有所有权限
GRANT ALL ON db_name.table_name TO 'user_name'@'host_name';
-- 授权用户'user_name'@'host_name'执行存储过程'procedure_name'的权限
GRANT EXECUTE ON PROCEDURE db_name.procedure_name TO 'user_name'@'host_name';
-- 授权用户'user_name'@'host_name'执行任何函数的权限
GRANT EXECUTE ON FUNCTION * TO 'user_name'@'host_name';
-- 授权用户'user_name'@'host_name'对所有数据库的所有表执行SELECT操作的权限
GRANT SELECT ON *.* TO 'user_name'@'host_name';
-- 授权用户'user_name'@'host_name'同时具有SELECT和INSERT权限,并设置密码
GRANT SELECT, INSERT ON db_name.* TO 'user_name'@'host_name' IDENTIFIED BY 'password';
-- 撤销用户'user_name'@'host_name'对表'table_name'的SELECT权限
REVOKE SELECT ON db_name.table_name FROM 'user_name'@'host_name';
-- 删除用户'user_name'@'host_name'
DELETE FROM mysql.user WHERE User='user_name' AND Host='host_name';
FLUSH PRIVILEGES; -- 刷新权限使更改生效
这些例子展示了如何使用GRANT和REVOKE语句来授予和撤销用户权限,以及如何删除用户。在实际操作中,应根据实际需求和环境来调整权限和用户信息。
评论已关闭