MariaDB [(none)]> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A
Database changed MariaDB [mysql]> select host,user,authentication_string from user; +-----------+-------------+-----------------------+ | Host | User | authentication_string | +-----------+-------------+-----------------------+ | localhost | mariadb.sys | | | localhost | root | invalid | | localhost | mysql | invalid | +-----------+-------------+-----------------------+ 3 rows in set (0.001 sec)
❯ mysql -umu -p123456 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 35 Server version: 10.6.16-MariaDB-0ubuntu0.22.04.1 Ubuntu 22.04
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> select user(); +--------------+ | user() | +--------------+ | mu@localhost | +--------------+ 1 row in set (0.000 sec)
2.2 删除用户
只有高权限用户或者root用户才能删除其他用户。
1
dropuser'用户名'@'主机名';
如果在传入用户名时不提供主机名,则主机名默认为%;
1 2 3
dropuser'用户名'; -- 等价于 dropuser'用户名'@'%';
示例如下
1 2
MariaDB [mysql]> drop user 'mu'@'192.168.1.30'; Query OK, 0 rows affected (0.006 sec)
MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | +--------------------+ 1 row in set (0.000 sec)
MariaDB [(none)]> select user(); +--------------+ | user() | +--------------+ | mu@localhost | +--------------+ 1 row in set (0.000 sec)
使用如下命令给用户授权
1
grantselecton stu_test.student to'mu'@'%';
授权之后,mu用户就能看到这个表了
1 2 3 4 5 6 7 8
MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | stu_test | +--------------------+ 2 rows in set (0.001 sec)
mu用户可以进行查询操作,但是不能删改student表,操作会被数据库拒绝。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
MariaDB [(none)]> use stu_test; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A
Database changed MariaDB [stu_test]> select * from student; +------+--------+ | id | name | +------+--------+ | 1 | 张三 | | 2 | 李四 | | 3 | 王五 | | 4 | 赵六 | | 5 | 刘七 | +------+--------+ 5 rows in set (0.000 sec)
MariaDB [stu_test]> insert into student values (6,'王璐'); ERROR 1142 (42000): INSERT command denied to user 'mu'@'localhost' for table `stu_test`.`student`
MariaDB [stu_test]> delete from student where id = 5; Query OK, 1 row affected (0.007 sec)
刷新权限
如果发现赋予权限后用户还是不能执行某些操作,可以用如下命令刷新权限
1
flush privileges;
3.3 回收权限
语法
1
revoke 权限列表 on 数据库.对象名 from'用户名'@'主机';
比如回收mu用户对于student表的插入权限
1
revokeinserton stu_test.student from'mu'@'%';
此时mu用户又不能进行插入操作了
1 2
MariaDB [stu_test]> insert into student values (7,'你好'); ERROR 1142 (42000): INSERT command denied to user 'mu'@'localhost' for table `stu_test`.`student`