day38-MySql破解密码步骤、基本SQL语句、表的详细操作(含上节课复习)

上节课复习


    1、多路复用I/O
        select模型
        poll模型
        epoll模型

2、异步I/O

3、数据库
    本质就是一个C/S架构的套接字软件
    Server的套接字软件专门管理本地硬盘上的数据文件

相关观念:
    数据库服务器:运行有数据库管理软件的计算机
    数据库管理软件:套接字软件MySql
    库:文件夹
    表:文件
    记录:就相当于文件中的一行内容


01 MySql破解密码

linux平台下,破解密码的两种方式:

方法一:删除授权库mysql,重新初始化
[root@egon ~]# rm -rf /var/lib/mysql/mysql #所有授权信息全部丢失!!!
[root@egon ~]# systemctl restart mariadb
[root@egon ~]# mysql
方法二:启动时,路过授权库
[root@egon ~]# vim /etc/my.cnf    #mysql主配置文件
[mysqld]
skip-grant-table
[root@egon ~]# systemctl restart mariadb
[root@egon ~]# mysql
MariaDB [(none)]> update mysql.user set password=password("123") where user="root" and host="localhost";
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> \q
[root@egon ~]# #打开/etc/my.cnf去掉skip-grant-table,然后重启
[root@egon ~]# systemctl restart mariadb
[root@egon ~]# mysql -u root -p123 #以新密码登录

windows平台下,5.7版本,破解密码的两种方式:

方式一:
#1 关闭mysql
#2 在cmd中执行:mysqld --skip-grant-tables #无需密码即可登陆
#3 在cmd中执行:mysql
#4 执行如下sql:
update mysql.user set password=password('egon123') where user = "root" and host="localhost";    #直接修改管理员密码
flush privileges;    #刷新权限信息

#5 tskill mysqld #或taskkill -f /PID 7832
#6 重新启动mysql
方式二:
#1. 关闭mysql,可以用tskill mysqld将其杀死
#2. 在解压目录下,新建mysql配置文件my.ini
#3. my.ini内容,指定
[mysqld]
skip-grant-tables

#4.启动mysqld
#5.在cmd里直接输入mysql登录,然后操作
update mysql.user set authentication_string=password('') where user='root and host='localhost';

flush privileges;

#6.注释my.ini中的skip-grant-tables,然后启动myqsld,然后就可以以新密码登录了

破解步骤总结:

#1、关闭mysql
net stop mysql

#2、重新启动
mysqld --skip-grant-tables


#3
mysql -uroot -p

update mysql.user set password=password("egon123") where user="root" and host="localhost";
flush privileges;

#4、关闭mysql,正常启动
net start mysql

补充:
在windows下,为mysql服务指定配置文件
强调:配置文件中的注释可以是中文,但配置项中不能存在中文

#在mysql的解压目录下,新建my.ini,然后配置
#1. 在执行mysqld命令时,下列配置会生效,即mysql服务启动时生效
[mysqld]
;skip-grant-tables
port=3306
character_set_server=utf8
default-storage-engine=innodb
innodb_file_per_table=1


#解压的目录
basedir=E:\mysql-5.7.19-winx64
#data目录
datadir=E:\my_data #在mysqld --initialize时,就会将初始数据存入此处指定的目录,在初始化之后,启动mysql时,就会去这个目录里找数据



#2. 针对客户端命令的全局配置,当mysql客户端命令执行时,下列配置生效
[client]
port=3306
default-character-set=utf8
user=root
password=123

#3. 只针对mysql这个客户端的配置,2中的是全局配置,而此处的则是只针对mysql这个命令的局部配置
[mysql]
;port=3306
;default-character-set=utf8
user=egon
password=4573


#!!!如果没有[mysql],则用户在执行mysql命令时的配置以[client]为准

my.ini

02 基本的Sql语句

有了mysql这个数据库软件,就可以将程序员从对数据的管理中解脱出来,专注于对程序逻辑的编写

mysql服务端软件即mysqld帮我们管理好文件夹以及文件,前提是作为使用者的我们,需要下载mysql的客户端,或者其他模块来连接到mysqld,然后使用mysql软件规定的语法格式去提交自己命令,实现对文件夹或文件的管理。该语法即sql(Structured Query Language 即结构化查询语言)

SQL语言主要用于存取数据、查询数据、更新数据和管理关系数据库系统,SQL语言由IBM开发。SQL语言分为3种类型:
#1、DDL语句    数据库定义语言: 数据库、表、视图、索引、存储过程,例如CREATE DROP ALTER
#2、DML语句    数据库操纵语言: 插入数据INSERT、删除数据DELETE、更新数据UPDATE、查询数据SELECT
#3、DCL语句    数据库控制语言: 例如控制用户的访问权限GRANT、REVOKE
文件夹:库
    增
        create database db1 charset utf8;

    删
        drop database db1;
    改
        alter database db1 charset gbk;
    查
        show databases;
        show create database db1;

文件:表
    切换文件夹
        use db1;
        select database(); 查看当前所在的库
    增
        create table t1(id int,name char);  #id int为整型的id,name char为定长(默认为1,针对显示效果)的name
        create table db1.t1(id int,name char);
    删
        drop table t1;
    改
        alter table t1 add age int;
        alter table t1 modify name char(15);    #修改name的定长,默认为1(只能显示1位)
        alter table t1 change name NAME char(15);
        alter table t1 drop age;

    查
        show tables;
        show create table t1;   #查看指定的某个文件(t1)
        desc t1;

文件内的一行行内容:记录
    增
        insert into t2(id,name) values
        (1,'egon'),
        (2,'lxx'),
        (3,'alex');

    删
        delete from db1.t1 where id >= 2;

        create table t2(id int primary key auto_incremnt,name char(15));
        insert into t2(name) values
        ('egon'),
        ('lxx'),
        ('wxx'),
        ('axx');

        清空表应该使用:
                truncate t2;
    改
        update db1.t1 set name='lxx_dsb' where id=2;
    查
        select id from db1.t1;
        select id,name from t1;
        select name,id from t1;
        select * from t1;
        select * from t1 where id >= 2;
MariaDB [db1]> describe t1; #查看表结构,可简写为desc 表名
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int(11)               | YES  |     | NULL    |       |
| name  | varchar(50)           | YES  |     | NULL    |       |
| sex   | enum('male','female') | YES  |     | NULL    |       |
| age   | int(3)                | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+


MariaDB [db1]> show create table t1\G; #查看表详细结构,可加\G

03 表详细操作

一、创建表的完整语法

#语法:
create table 库名.表名(
    字段名1 类型[(宽度) 约束条件],
    字段名2 类型[(宽度) 约束条件],
    字段名3 类型[(宽度) 约束条件]
);

约束条件:是在数据类型之外对字段附加的额外的限制

注意:
1、最后一个字段之后不能加逗号
2、在同一张表中,字段名是不能相同
3、宽度和约束条件可选,字段名和类型是必须的

二、数据类型
1、整型:默认是有符号的

create table t3(x tinyint);

PS: 修改sql_mode为严格模式,必须重启客户端才能生效

set global sql_mode="strict_trans_tables";
select @@sql_mode;

create table t4 (x tinyint unsigned);    #tinyint:SQL Server数据库的一种数据类型,范围从0到255之间的整数

强调:整型类型后面的宽度限制的根本不是存储宽度,限制的是显示宽度

create table t5(id int(1));
create table t6(id int(5));

2、浮点型:

float(255,30)
double(255,30)
decimal(65,30)

create table t8(x float(255,30));    #255:总长度,30:小数点后位数,不难看出点数前有225位
create table t9(x double(255,30));
create table t10(x decimal(65,30));


insert into t8 vlues(1.111111111111111111111);
结果:1.111111164093017600000000000000
insert into t9 vlues(1.111111111111111111111);
结果:1.111111111111111200000000000000
insert into t10 vlues(1.111111111111111111111);
结果:1.111111111111111111111

float---->double---->decimal 依次往右,精度越高

日常使用float最多

3、 日期类型

year 1999
date 1999-11-11
time 08:30:00
datetime/timestamp 1999-11-11 08:30:00

create table student(    #创建学生列表的基本信息
    id int primary key auto_increment,
    name char(16),
    born_year year,
    birth date,
    class_time time,
    reg_time datetime
);

insert into student(name,born_year,birth,class_time,reg_time) values
('egon1',now(),now(),now(),now());    #now是截取当前时间

insert into student(name,born_year,birth,class_time,reg_time) values
('egon1',2000,20001111,now(),now());    #指定年月日

insert into student(name,born_year,birth,class_time,reg_time) values
('egon1',2000,'2000-11-11',083000,now());    #自定义日期格式,指定时间

insert into student(name,born_year,birth,class_time,reg_time) values
('egon1',2000,'2000-11-11',"08:30:00",20171111111111);    #纯自定义格式1

insert into student(name,born_year,birth,class_time,reg_time) values
('egon1',2000,'2000-11-11',"08:30:00","2017-11-11 11:11:11");    #纯自定义格式2

create table t11(x timestamp);    #
create table t12(x datetime not null default now());



日期格式:
YEAR
            YYYY(1901/2155)

        DATE
            YYYY-MM-DD(1000-01-01/9999-12-31)

        TIME
            HH:MM:SS('-838:59:59'/'838:59:59')

        DATETIME

            YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59    Y)

        TIMESTAMP

            YYYYMMDD HHMMSS(1970-01-01 00:00:00/2037 年某时)

在DATETIME和TIMESTAMP中推荐使用TIMESTAMP:
在实际应用的很多场景中,MySQL的这两种日期类型都能够满足我们的需要,存储精度都为秒,但在某些情况下,会展现出他们各自的优劣。下面就来总结一下两种日期类型的区别。

1.DATETIME的日期范围是1001——9999年,TIMESTAMP的时间范围是1970——2038年。

2.DATETIME存储时间与时区无关,TIMESTAMP存储时间与时区有关,显示的值也依赖于时区。在mysql服务器,操作系统以及客户端连接都有时区的设置。

3.DATETIME使用8字节的存储空间,TIMESTAMP的存储空间为4字节。因此,TIMESTAMP比DATETIME的空间利用率更高。

4.DATETIME的默认值为null;TIMESTAMP的字段默认不为空(not null),默认值为当前时间(CURRENT_TIMESTAMP),如果不做特殊处理,并且update语句中没有指定该列的更新值,则默认更新为当前时间。

datetime与timestamp的区别

4、字符类型

注意:宽度指限制的是字符个数
char:定长
    char(5)

varchar:变长
    varchar(5)

相同点:宽度指的都是最大存储的字符个数,超过了都无法正常存储
不同点:

char(5):
        'm'--->'m    '5个字符

    varchar(5)
        'm'--->'m'1个字符

set global sql_mode="strict_trans_tables,PAD_CHAR_TO_FULL_LENGTH";

注意:mysql在查询时针对where 字段="值    "会忽略掉右面的空格,即where 字段="值"
如果是like模糊匹配就不会忽略右面的空格了

char(5)
egon |axx  |lxx  |fm   |

varchar(5)
1bytes+egon|1bytes+axx|1bytes+lxx|1bytes+fm|
Value CHAR(4) Storage Required VARCHAR(4) Storage Required
''     '    ' 4 bytes '' 1 byte
'ab' 'ab  ' 4 bytes 'ab' 3 bytes
'abcd' 'abcd' 4 bytes 'abcd' 5 bytes
'abcdefgh' 'abcd' 4 bytes 'abcd' 5 bytes

5、枚举与集合类型
枚举enum('a','b','c'):多选一
集合set('a','b','c'):多选多

create table emp(
    name varchar(15),
    sex enum('male','female','unkown'),
    hobbies set('read','music','yinshi','play')
);

insert into emp values('alex','male','music,play');


mysql> select * from emp;
+------+------+------------+
| name | sex  | hobbies    |
+------+------+------------+
| alex | male | music,play |
+------+------+------------+
1 row in set (0.00 sec)

猜你喜欢

转载自blog.csdn.net/qq_17513503/article/details/81121763
今日推荐