DCL
更多数据库MySQL系统内容就在以下专栏:
专栏链接:数据库MySQL
1. 基本介绍
DCL英文全称是Data Control Language(数据控制语言), 用来管理数据库用户、控制数据库的访问权限。
itcast 和 heima 是我们的用户,而DCL就是控制有哪些用户能够访问数据库,那么,每一个用户又有访问不同数据库的权限。
2. 用户管理
2.1 查询用户
基本语法:
USE mysql;
SELECT * FROM user;
- 在MySQL中,用户的信息,用户所具有的权限的信息,都是存放在系统数据库mysql的user表中的。
- 默认在MySQL数据库中有五个用户,表中的第一个字段host 是主机的意思。在这五个当中,我们只使用过root这一个用户,其他的并没有使用过。
- 我们在删除一个用户的时候,需要通过用户名和host 主机地址,同时定位。
- 主机地址,表示的当前用户只能在哪个主机上访问MySQL服务器,localhost 表示只能在本机访问,不能远程访问。
mysql> use mysql;
Database changed
mysql>
mysql> select * from user;
+-----------+------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+------------------------------------------------------------------------+------------------+-----------------------+-------------------+----------------+------------------+----------------+------------------------+---------------------+--------------------------+-----------------+
| Host | User | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Reload_priv | Shutdown_priv | Process_priv | File_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Show_db_priv | Super_priv | Create_tmp_table_priv | Lock_tables_priv | Execute_priv | Repl_slave_priv | Repl_client_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Create_user_priv | Event_priv | Trigger_priv | Create_tablespace_priv | ssl_type | ssl_cipher | x509_issuer | x509_subject | max_questions | max_updates | max_connections | max_user_connections | plugin | authentication_string | password_expired | password_last_changed | password_lifetime | account_locked | Create_role_priv | Drop_role_priv | Password_reuse_history | Password_reuse_time | Password_require_current | User_attributes |
+-----------+------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+------------------------------------------------------------------------+------------------+-----------------------+-------------------+----------------+------------------+----------------+------------------------+---------------------+--------------------------+-----------------+
| % | scott | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | | | | | 0 | 0 | 0 | 0 | caching_sha2_password | $A$005$"+4)*-%1 B4y/g_diDXo2r2UZxrw6JaXyvywXlP0IJfl5JA44SMArbOkaC | N | 2024-04-25 20:22:19 | NULL | N | N | N | NULL | NULL | NULL | NULL |
| localhost | mysql.infoschema | Y | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | | | | | 0 | 0 | 0 | 0 | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | N | 2024-04-11 20:35:58 | NULL | Y | N | N | NULL | NULL | NULL | NULL |
| localhost | mysql.session | N | N | N | N | N | N | N | Y | N | N | N | N | N | N | N | Y | N | N | N | N | N | N | N | N | N | N | N | N | N | | | | | 0 | 0 | 0 | 0 | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | N | 2024-04-11 20:35:58 | NULL | Y | N | N | NULL | NULL | NULL | NULL |
| localhost | mysql.sys | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | | | | | 0 | 0 | 0 | 0 | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | N | 2024-04-11 20:35:58 | NULL | Y | N | N | NULL | NULL | NULL | NULL |
| localhost | root | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | | | | | 0 | 0 | 0 | 0 | caching_sha2_password | $A$005$}|@dFq\)wgJ|>{)8MR9roBZWBb9ltDJxjKz5LPd7OL6YU7/tlbgMxFxTu65 | N | 2024-04-11 20:36:04 | NULL | N | Y | Y | NULL | NULL | NULL | NULL |
+-----------+------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+------------------------------------------------------------------------+------------------+-----------------------+-------------------+----------------+------------------+----------------+------------------------+---------------------+--------------------------+-----------------+
5 rows in set (0.06 sec)
mysql>
2.2 创建用户
基本语法:
CREATE USER '用户名' @ '主机名' IDENTIFIED BY '密码';
用户名:新创建的用户名。
主机名:在哪一个主机上用户可以访问当前MySQL。
密码:访问密码。
案例1:
创建用户 itcast ,只能在当前主机localhost 访问,密码:111111;
mysql>
mysql> create user 'itcast'@'localhost' identified by '111111';
Query OK, 0 rows affected (0.01 sec)
mysql>
- 查询一下,看是不是增加了一个用户;
mysql>
mysql> select * from user;
+-----------+------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+------------------------------------------------------------------------+------------------+-----------------------+-------------------+----------------+------------------+----------------+------------------------+---------------------+--------------------------+-----------------+
| Host | User | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Reload_priv | Shutdown_priv | Process_priv | File_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Show_db_priv | Super_priv | Create_tmp_table_priv | Lock_tables_priv | Execute_priv | Repl_slave_priv | Repl_client_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Create_user_priv | Event_priv | Trigger_priv | Create_tablespace_priv | ssl_type | ssl_cipher | x509_issuer | x509_subject | max_questions | max_updates | max_connections | max_user_connections | plugin | authentication_string | password_expired | password_last_changed | password_lifetime | account_locked | Create_role_priv | Drop_role_priv | Password_reuse_history | Password_reuse_time | Password_require_current | User_attributes |
+-----------+------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+------------------------------------------------------------------------+------------------+-----------------------+-------------------+----------------+------------------+----------------+------------------------+---------------------+--------------------------+-----------------+
| % | scott | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | | | | | 0 | 0 | 0 | 0 | caching_sha2_password | $A$005$"+4)*-%1 B4y/g_diDXo2r2UZxrw6JaXyvywXlP0IJfl5JA44SMArbOkaC | N | 2024-04-25 20:22:19 | NULL | N | N | N | NULL | NULL | NULL | NULL |
| localhost | itcast | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | | | | | 0 | 0 | 0 | 0 | caching_sha2_password | $A$005$O"+Vv4M6xe r0FD1DszyHBQABltzRNOfKdRvZG8bE2Lu5ROVpeuXWgZsx5 | N | 2024-06-05 19:53:17 | NULL | N | N | N | NULL | NULL | NULL | NULL |
| localhost | mysql.infoschema | Y | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | | | | | 0 | 0 | 0 | 0 | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | N | 2024-04-11 20:35:58 | NULL | Y | N | N | NULL | NULL | NULL | NULL |
| localhost | mysql.session | N | N | N | N | N | N | N | Y | N | N | N | N | N | N | N | Y | N | N | N | N | N | N | N | N | N | N | N | N | N | | | | | 0 | 0 | 0 | 0 | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | N | 2024-04-11 20:35:58 | NULL | Y | N | N | NULL | NULL | NULL | NULL |
| localhost | mysql.sys | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | | | | | 0 | 0 | 0 | 0 | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | N | 2024-04-11 20:35:58 | NULL | Y | N | N | NULL | NULL | NULL | NULL |
| localhost | root | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | | | | | 0 | 0 | 0 | 0 | caching_sha2_password | $A$005$}|@dFq\)wgJ|>{)8MR9roBZWBb9ltDJxjKz5LPd7OL6YU7/tlbgMxFxTu65 | N | 2024-04-11 20:36:04 | NULL | N | Y | Y | NULL | NULL | NULL | NULL |
+-----------+------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+------------------------------------------------------------------------+------------------+-----------------------+-------------------+----------------+------------------+----------------+------------------------+---------------------+--------------------------+-----------------+
6 rows in set (0.06 sec)
mysql>
- 使用itcast 用户访问mysql :
C:\Users\Lenovo>mysql -u itcast -p
Enter password: ******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.36 MySQL Community Server - GPL
Copyright (c) 2000, 2024, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| performance_schema |
+--------------------+
2 rows in set (0.01 sec)
mysql>
- 使用root 用户访问MySQL数据库:
Microsoft Windows [版本 10.0.22631.3672]
(c) Microsoft Corporation。保留所有权利。
C:\Users\Lenovo>mysql -u root -p
Enter password: ******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.36 MySQL Community Server - GPL
Copyright (c) 2000, 2024, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| company |
| demo |
| information_schema |
| itcast |
| itxiaobu |
| mysql |
| performance_schema |
| sys |
| text |
+--------------------+
9 rows in set (0.00 sec)
mysql>
mysql>
- 我们在使用不同的用户访问同一个mysql数据库,其能访问的数据库是不一样的:
itcast用户能访问的:
root用户能访问的:
这是因为我们仅仅只是创建了itcast 这个用户,它可以访问mysql数据库,但是它没有访问其他数据库的权限。
案例2:
创建用户 xiaobu 可以在任意主机访问该数据库,密码:123456;
mysql>
mysql> create user 'xiaobu'@'%' identified by '123456';
Query OK, 0 rows affected (0.03 sec)
mysql>
可以在任意主机访问,使用%
2.3 修改用户密码
基本语法:
ALTER USER '用户名' @ '主机名' IDENTIFIED WITH mysql_native_passward BY '新密码';
案例:
修改用户 xiaobu 的访问密码为 111111;
mysql> alter user 'xiaobu'@'%' identified with mysql_native_password by '111111';
Query OK, 0 rows affected (0.01 sec)
mysql>
2.4 删除用户
基本语法:
DROP USER '用户名' @ '主机名';
案例:
删除itcast@localhost用户;
mysql>
mysql> drop user 'itcast'@'localhost';
Query OK, 0 rows affected (0.01 sec)
mysql>
3. 权限控制
MySQL中定义了很多种权限,但是常用的就以下几种:
权限 | 说明 |
---|---|
ALL, ALL PRIVILEGES | 所有权限 |
SELECT | 查询数据 |
INSERT | 插入数据 |
UPDATE | 修改数据 |
DELECT | 删除数据 |
ALTER | 修改表 |
DROP | 删除数据库/表/视图 |
CREATE | 创建数据库/表 |
3.1 查询权限
基本语法:
SHOW GRANTS FOR '用户名'@'主机名';
案例:
查询用户xiaobu的权限
mysql>
mysql> show grants for 'xiaobu'@'%';
+------------------------------------+
| Grants for xiaobu@% |
+------------------------------------+
| GRANT USAGE ON *.* TO `xiaobu`@`%` |
+------------------------------------+
1 row in set (0.03 sec)
mysql>
3.2 授予权限
基本语法:
GRANT 权限列表 ON 数据库名.表名 TO '用户名'@'主机名';
案例:
赋予用户xiaobu 访问itcast数据库的所有表的权限
mysql>
mysql> grant all on itcast.* to 'xiaobu'@'%';
Query OK, 0 rows affected (0.01 sec)
mysql>
3.3 撤销权限
基本语法:
REVOKE 权限列表 ON 数据库名.表名 FROM '用户名'@'主机名';
案例:
撤销用户xiaobu 访问itcast数据库所有表的权限
mysql>
mysql> revoke all on itcast.* from 'xiaobu'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql>
注意:
- 多个权限之间,使用逗号分割。
- 授权时,数据库名和表名可以使用* 进行通配,代表所有。