Skip to content

用户和权限管理

基于mariadb测试

创建用户,用户没有创建数据库和数据表等权限,只有查看information_schema数据库权限。参考 https://mariadb.com/kb/en/create-user/

sql
create user test1@localhost identified by 'test';

创建或者替换用户,如果用户已经存在不会报告错误。

sql
create or replace user test1@localhost identified by 'test';

如果用户存在就提示警告

sql
create user if not exists test1@localhost identified by 'test';

重命名用户,如果不指定host部分则默认使用%。

sql
# test1@'localhost'用户被修改为test11@'%'
rename user test1@'localhost' to test11;
# test11@'%'用户被修改为test1@'localhost'
rename user test11@'%' to test1@'localhost';

删除用户

sql
drop user test1@'localhost';
# 如果用户不存在只会提示警告
drop user if exists test1@'localhost';

修改用户密码,参考 https://mariadb.com/kb/en/set-password/

sql
set password for test1@localhost = password("12345678");

使用alter user修改用户密码,参考 https://mariadb.com/kb/en/alter-user/

sql
alter user test1@localhost identified by 'test';

权限管理

注意: 使用mariadb:11.3.2测试,使用mariadb:10.4.19有些权限不存在导致grant语法错误。

显示指定用户当前已经分配的权限

参考

sql
show grants for test1@localhost;

回收指定用户的指定权限

参考

sql
# 创建用户
create user if not exists user1@'%' identified by '123456';

# 授予所有权限
grant all privileges on *.* to user1@'%';

# 回收其中的super权限
revoke super on *.* from user1@'%';

全局权限

binlog admin权限

启用二进制日志的管理,包括 PURGE BINARY LOGS 语句和设置相关binlog系统变量

参考 https://mariadb.com/kb/en/grant/#binlog-admin

sql
grant binlog admin on *.* to test1@localhost;
binlog monitor权限

MariaDB 10.5.2 中 REPLICATION CLIENT 的新名称(出于兼容性目的,仍支持将 REPLICATION CLIENT 作为别名)。 允许运行与二进制日志相关的 SHOW 命令,特别是 SHOW BINLOG STATUS 和 SHOW BINARY LOGS 语句。 与 MariaDB 10.5 之前的 REPLICATION CLIENT 不同,此权限不包含 SHOW REPLICA STATUS,并且需要 REPLICA MONITOR。

参考 https://mariadb.com/kb/en/grant/#binlog-monitor

sql
grant binlog monitor on *.* to test1@localhost;
binlog replay权限

允许使用 BINLOG 语句(由 mariadb-binlog 生成)重放二进制日志,在 secure_timestamp 设置为复制时执行 SET 时间戳,并设置通常包含在 BINLOG 输出中的系统变量的会话值。

参考 https://mariadb.com/kb/en/grant/#binlog-replay

sql
grant binlog replay on *.* to test1@localhost;
shutdown权限

使用 SHUTDOWN 或 mariadb-admin shutdown 命令关闭服务器。

参考 https://mariadb.com/kb/en/grant/#shutdown

sql
grant shutdown on *.* to test1@localhost;