Mysql——SQL语言及基本操作一(DDL、DML、DQL)在此链接:http://t.csdnimg.cn/7Z5IX
mysql 版本 8.0
一、DCL数据控制语言
简介:DCL为数据控制语言,用来定义访问权限和安全级别
1. 权限级别
Global level --所有库,所有表的权限
Database level --某个数据库中的所有表权限
Table level --库中的某个表的权限
Column level --表中的某个字段,的权限
2. Mysql用户管理
2.1 创建用户
create user zhangsan@'localhost' identified by 'Xielei@123';
// 用户名 登陆点 身份认证 新密码
### 如果报错1290,刷新下权限再重新创建即可
2.2 删除用户
drop user zhangsan@'localhost';
2.3 修改用户密码
- root修改自己的密码
mysqladmin -uroot -p'Xielei@123' password 'Zhonghui@123'
// 原密码 新密码
- 如果丢失root用户密码
前言:当root用户,忘记了密码,可以使用破解的方式来登录系统,来修改密码
原理:使系统在启动时,不加载密码文件。
操作如下:①修改MySQL启动设置
vim /etc/my.cnf ——>进入后找到[mysqld]一栏,在下一行输入:skip-grant-tables
②重启MySQL,无密码登录,进入数据库后修改root密码,最后退出
systemctl restart mysqld mysql -uroot mysql> flush privileges; Query OK, 0 rows affected (0.02 sec) mysql> create user 'root'@'%' identified by 'Zhonghui@123'; Query OK, 0 rows affected (0.01 sec) mysql> grant all on *.* to 'root'@'%'; Query OK, 0 rows affected (0.00 sec) mysql> alter user 'root'@'%' identified with mysql_native_password by 'Zhonghui@123'; Query OK, 0 rows affected (0.01 sec) mysql> exit
③进入 /etc/my.cnf 文件,把添加的 skip-grant-tables 一行注释掉,然后保存退出
④重启MySQL,输入新密码重新登陆
[root@harbor ~]# systemctl restart mysqld [root@harbor ~]# mysql -uroot -p'Zhonghui@123' mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 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>
2.4 登录MySQL
mysql -P 3306 -uroot -p'Xielei@123' mysql -e'show databases'
// -P MySQL服务端口,默认3306。大写P
// -u 指定用户名
// -p 指定登录密码
// -e 接SQL语句,可以不进入数据库直接在外面操作
// 后面的mysql指:此处的mysql为指定登录的数据库
2.5 MySQL权限语法
- 语法格式
grant #auth on #databaseName.#table to '#userName'@'#host';
—> #auth 代表权限,如下:
- all privileges 全部权限
- select 查询权限
- select,insert,update,delete 增删改查权限
- select,[...]增...等权限
——> #databaseName 代表数据库名
——> #table 代表具体表,如下:
- * 代表全部表
- A,B 代表具体A,B表
——> #userName 代表用户名
——> #host 代表访问权限,如下:
- %代表通配所有host地址权限(可远程访问)
- localhost为本地权限(不可远程访问)
- 指定特殊Ip访问权限 如10.138.106.102
2.6 MySQL权限示例
2.6.1 赋予权限
授予目标:授予admin1用户对bbs库所有的表,具有所有权限(不包括授权)
赋予授权:
create user admin1@'localhost' identified by 'Xielei@123'; grant all on bbs.* to admin1@'localhost';
查看授予的权限
mysql> grant all on bbs.* to admin1@'localhost'; Query OK, 0 rows affected (0.01 sec) mysql> show grants for admin1@'localhost'; +---------------------------------------------------------+ | Grants for admin1@localhost | +---------------------------------------------------------+ | GRANT USAGE ON *.* TO `admin1`@`localhost` | | GRANT ALL PRIVILEGES ON `bbs`.* TO `admin1`@`localhost` | +---------------------------------------------------------+ 2 rows in set (0.00 sec)
2.6.2 撤销权限
撤销admin1用户对bbs库所有的表,具有的所有权限
revoke all on bbs.* from 'admin1'@'localhost';
查看admin1的权限是否回收
mysql> revoke all on bbs.* from 'admin1'@'localhost'; Query OK, 0 rows affected (0.00 sec) mysql> show grants for admin1@'localhost'; +--------------------------------------------+ | Grants for admin1@localhost | +--------------------------------------------+ | GRANT USAGE ON *.* TO `admin1`@`localhost` | +--------------------------------------------+ 1 row in set (0.00 sec) mysql>
2.6.3 查看权限
查看自己(root)的权限
show grants;
查看别人的权限,比如查看admin3用户的权限
show grants for admin3@'localhost';