【lesson32】MySQL用户管理

用户管理介绍

在这里插入图片描述

用户

用户信息

MySQL中的用户,都存储在系统数据库mysql的user表中

//操作语法
mysql> use mysql;
Database changed
mysql> select host,user,authentication_string from user;

在这里插入图片描述
字段解释:

  • host: 表示这个用户可以从哪个主机登陆,如果是localhost,表示只能从本机登陆
  • user: 用户名
  • authentication_string: 用户密码通过password函数加密后的
  • *_priv: 用户拥有的权限

创建用户

//操作语法
create user '用户名'@'登陆主机/ip' identified by '密码';

案例;
在这里插入图片描述
我们查看user表
在这里插入图片描述
user表中确实多了一个用户;
我们尝试用该用户登入
在这里插入图片描述
发现确实也能登入。
但是只能在本地登入,不能远程登入。

想要在任意主机登入,得用下列语句创建一个用户。

create user 'lzf' @'%' identified by '123456';

这里就不演示了。

删除用户

drop user '用户名'@'主机名'

在这里插入图片描述

修改用户密码

自己改自己密码

set password=password('新的密码');

root用户修改指定用户的密码

set password for '用户名'@'主机名'=password('新的密码')

用update修改密码

update user set authentication_string = password('11111') where user = 'lzf';

因为用户的信息是被管理在user表中的,所以我们可以直接该user表中的密码。
同样我们也可以直接对user表进行用户的增删查改。

数据库的权限

在这里插入图片描述
在这里插入图片描述

给用户授权

语法:

grant 权限列表 on 库.对象名 to '用户名'@'登陆位置' [identified by '密码']

权限列表,多个权限用逗号分开

grant select on ...
grant select, delete, create on ....
grant all [privileges] on ... -- 表示赋予该用户在该对象上的所有权限
  • *.**: 代表本系统中的所有数据库的所有对象(表,视图,存储过程等)
  • 库.* : 表示某个数据库中的所有数据对象(表,视图,存储过程等)
  • identified by可选。 如果用户存在,赋予权限的同时修改密码,如果该用户不存在,就是创建用户

案例:

--使用root账号
--终端A
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| 57test |
| bit_index |
| ccdata_pro |
| innodb_test |
| musicserver |
| myisam_test |
| mysql |
| order_sys |
| performance_schema |
| scott |
| sys |
| test |
| vod_system |
+--------------------+
14 rows in set (0.00 sec)
mysql> use test;
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| account |
| student |
| user |
+----------------+
3 rows in set (0.01 sec)

--给用户lzf赋予test数据库下所有文件的select权限
mysql> grant select on test.* to 'lzf'@'localhost';
Query OK, 0 rows affected (0.01 sec)
--使用lzf账号
--终端B
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)
--暂停等root用户给whb赋完权之后,在查看
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test | --赋完权之后,就能看到新的表
+--------------------+
2 rows in set (0.01 sec)
mysql> use 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
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| account |
| student |
| user |
+----------------+
3 rows in set (0.00 sec)
mysql> select * from account;
+----+--------+---------+
| id | name | blance |
+----+--------+---------+
| 2 | 李四 | 321.00 |
| 3 | 王五 | 5432.00 |
| 4 | 赵六 | 543.90 |
| 5 | 赵六 | 543.90 |
+----+--------+---------+
4 rows in set (0.00 sec)
--没有删除权限
mysql> delete from account;
ERROR 1142 (42000): DELETE command denied to user 'whb'@'localhost' for table 'account'
备注:特定用户现有查看权限
mysql> show grants for 'lzf'@'%';
+-----------------------------------------------+
| Grants for lzf@% |
+-----------------------------------------------+
| GRANT USAGE ON *.* TO 'lzf'@'%' |
| GRANT ALL PRIVILEGES ON `test`.* TO 'lzf'@'%' |
+-----------------------------------------------+
2 rows in set (0.00 sec)
mysql> show grants for 'root'@'%';
+-------------------------------------------------------------+
| Grants for root@% |
+-------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION |
+-------------------------------------------------------------+
1 row in set (0.00 sec)

注意:如果发现赋权限后,没有生效,执行如下指令:

flush privileges;

回收权限

语法:

revoke 权限列表 on 库.对象名 from '用户名'@'登陆位置'

案例

-- 回收lzf对test数据库的所有权限
--root身份,终端A
mysql> revoke all on test.* from 'lzf'@'localhost';
Query OK, 0 rows affected (0.00 sec)
--lzf身份,终端B
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test |
+--------------------+
2 rows in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)

相关推荐

  1. lesson33MySQL使用C/C++连接

    2024-02-02 21:40:01       45 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-02 21:40:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-02 21:40:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-02 21:40:01       82 阅读
  4. Python语言-面向对象

    2024-02-02 21:40:01       91 阅读

热门阅读

  1. mysql8 允许外网连接mysql

    2024-02-02 21:40:01       47 阅读
  2. 总是提示安装不了tensorflow

    2024-02-02 21:40:01       55 阅读
  3. Yii2之类自动加载

    2024-02-02 21:40:01       50 阅读
  4. 2024/2/1 备战蓝桥杯 3-3 二叉树

    2024-02-02 21:40:01       55 阅读
  5. 拓扑空间论学习与Transformer的联系

    2024-02-02 21:40:01       55 阅读
  6. C语言宏的进化

    2024-02-02 21:40:01       55 阅读
  7. Python程序设计 数据容器(List、Tuple、Dict、Set)

    2024-02-02 21:40:01       45 阅读
  8. 学生寝室管理系统

    2024-02-02 21:40:01       48 阅读