一、名词
1、数据库 —— 表的管理单元
2、表 —— 记录的管理单元
3、记录 —— 信息的载体,字段的管理单元
4、字段 —— 由字段名,字段类型,字段约束组成
5、约束 —— 不能为空,自增,自减,主键
6、类型 —— 数字,字符,日期
二、命令的简单说明
1. 数据库的查看及进入
show databases; 查看数据库名称
use mysql; 进入数据库(mysql为数据库名)
show tables; 查看数据库中的表
desc user ; 查看表结构
show database(); 查看自己当前所在库
show table(); 查看自己当前所在表
2. 创建
create database students; 创建名称为students的数据库
create table stus(name varchar(20), age int); 创建名为stus的表 括号内容为限定创建内容的条件(必须在对应数据库内才能创建表)
create table students.t1 (name varchar(20); age int); 这种方法不用进入数据库 已经声明在students. 的库中创建一个名为t1的表
alter table stus add (year date); 追加表列
3. 数据的插入与查询
insert into stus values( 张三 ,20); 向stus表中插入数据
insert into stus(year) values (20) ; 向stus的表中的year列插入数据
select * from stus 查看表中所有数据
select name,age where name = ‘张三’; 查看表中名字叫张三的数据对应的年龄及姓名 (name age 代表显示的数据可以跟多个参数 用,隔开即可)
where 代表条件查询
seclect name,age where age 20 or 25 查找年龄为20或25的人数据
select name,age from stus where age between 20 and 23 查找年龄在20-23岁之间的学生显示列为姓名和年龄
select name,age from stus where age not between 20 and 23 查找年龄不在20-23岁之间的学生显示列为姓名和年龄
select name,age from stus where age in (20,30,22)
查找年龄为20,30,22的学生显示列为姓名和年龄 in表示多个定义条件
select name,age from stus where age not in (20,30,22)
查找年龄为不是20,30,22的学生显示列为姓名和年龄 in表示多个定义条件
4. 数据库及数据的删除及更新
drop database students; 删除students的数据库
drop table stus 删除stus的表
alter table stus drop year 删除stus 的year列
delete from stus ; 删除表中的所有数据
delete from stus where age=20; 删除age=20的所有数据
update stus set age=30 where name=’zhangsan‘
将名为zhangsan的学生年龄调整为30
三、ROOT丢失自己的密码
当root用户,忘记了密码。可以使用破解的方式来登录系统。修改密码。
原理:使系统在启动时,不加载密码文件。
1. 修改mysql启动设置
# vim /etc/my.cnf
[mysqld]
skip-grant-tables
2. 重启mysql,无密码登录
重启mysql程序 systemctl restart mysqld
无密码登录 mysql -uroot
修改自己的密码 mysql>UPDATE mysql.user SET authentication_string=password('QianFeng@123456789') where user='root' and host='localhost';
刷新 mysql> FLUSH PRIVILEGES;
3. 修改mysql启动设置
注释掉跳过密码 # skip-grant-tables
四、mysql 权限管理
1、授权 —— grant
语法: grant 权限列表 on 库名.表名 to '用户名'@'客户端主机' [identified by '密码' with option参数];
示例 : grant SELECT (id), INSERT (name,age) ON mydb.mytbl to 'user8'@'localhost' identified by 'QianFeng@123';
grant 权限列表 on 库名.表名 to '用户名'@'客户端主机' identified by '密码' ;
2、查看权限
查看自己的权限 SHOW GRANTS\G
查看别人的权限 SHOW GRANTS FOR admin3@'%'\G
3、回收权限 —— REVOKE
语法: REVOKE 权限列表 ON 数据库名 FROM 用户名@‘客户端主机’
示例: REVOKE ALL PRIVILEGES ON bbs.* FROM admin3@’%’; //回收所有权限
4、删除用户的版本问题
5.6之前,先 revoke all privilege 再 drop user
5.7之后,直接 drop user