• 【网络安全 --- MySQL数据库】网络安全MySQL数据库应该掌握的知识,还不收藏开始学习。


    四,MySQL

    4.1 mysql安装

    #centos7默认安装的是MariaDB-5.5.68或者65,
    #查看版本的指令:[root@web01 bbs]# rpm -qa| grep mariadb
    #安装mariadb的最新版,只是更新了软件版本,不会删除之前原有的数据。
    #修改yum源的配置文件
    vim /etc/yum.repos.d/mariadb.repo
    i[mariadb]
    name=mariadb laster version
    baseurl=http://mirrors.tuna.tsinghua.edu.cn/mariadb/yum/10.6/centos7-amd64/
    gpgcheck=0
    #yum安装mariadb
    yum install mariadb-server -y
    #重新启动mariadb并设置开机自启
    systemctl start mariadb
    systemctl enable mariadb
    # 安装完之后建议运行一下安全初始化的动作:
    mysql_secure_installation
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    4-2 授权

    #授权 ,默认情况下mysql和mariadb都是不允许root用户远程连接登录的。
    # grant 操作(增删改查,all) on 库名.表名 to 用户名@'%' identified by '密码';
    
    grant all on *.* to root@'192.168.31.%' identified by 'aini';
    grant all on wordpress.* to wordpress@'192.168.61.%' identified by '123456';
    grant all on wordpress.t1 to jaden@'192.168.61.%' identified by '123';  #jaden用户只能对menu表进行操作
    
    # 网站代码中连接数据库的时候使用的是哪个用户,那个用户有什么权限,那么网站代码就能对数据库做什么操作。
    
    ## 查看某个用户有哪些权限
    show grants for root@'%';
    
    # 单独创建用户
    create user wang@'%' identified by '123';
    
    # 单独创建的用户是没有任何权限的,只能登录,需要授权
    grant all on wordpress.* to wang@'%';
    
    # 上面两条就等于我们前面授权加创建用户的一条指令。
    # 删除用户
    drop user wang@'%';
    
    # 查看用户的权限
    show grants for jaden@'192.168.61.%';
    
    # 回收权限: 注意:只有在本机登录的root用户才有这个能力
    revoke select on wordpress.t1 from jaden@'192.168.61.%'; 
    show grants for jaden@'192.168.61.%';
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    4-3 登录修改密码

    #使用普通用户登录
    mysql -u wordpress -p123456 -h 10.0.0.7
    #默认的数据文件存储位置是
    /var/lib/mysql/
    #/root/.mysql_history 记录了我们做的历史sql指令
    # 修改用户密码
    #安全初始化,可以修改root用户的密码:mysql_secure_installation
    格式:mysql> set password for 用户名@localhost = password('新密码'); 
    例子:mysql> set password for root@localhost = password('123'); 
    # 查询当前是在哪个库里面
    MariaDB [mysql]> select database();
    #查看表结构
    desc songs;
    +---------+--------------+------+----
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    4-4 MySQL数据类型

    int   整形 数字   适合存储:年龄, 加减运算
    float 浮点型     适合存储:余额 加减运算
    char   字符串     适合存储:不做加减运算 身份号码 密码,单行信息
    text   文本       适合存储: 适合多行信息,小说,商品描述
    enum   枚举       适合存储: 固定选项,多选一
    date   日期类型   适合存储:时间,一般存储的是unix时间戳,从1970.1.1 0:0:0到现在过了多少秒,这个时间戳是可以转化为具体的时间				日期的。
    boolean 布尔类型   true/false 对应数字就是0/0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    4-5 所有的整型int

    image-20230718204445735

    4-6 字符串类型

    image-20230718204510252

    4-7 text类型

    image-20231017194357601

    4-8 MySQL完整性约束

    not null # 不能为空,默认是可以为空的
    default   # default 100,意思是默认值为100
    unique   # 唯一
    auto_increment # 自增
    primary key #主键:not null+unique,还自带auto_increment自增属性,但是每个表里面只能有一列能为primary key主键列
    unsigned #只能存正整数,默认是可以存正数和负数的
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    4-9 MySQL数据表操作

    #切换库
    use linux;
    #创建表 #每个web项目其实都会创建很多个表来存储不同的数据
    create table 表名(
    字段名1 类型[(宽度) 约束条件],
    字段名2 类型[(宽度) 约束条件],
        字段名3 类型[(宽度) 约束条件]
    );
    示例:
    mysql> create table jaden(  
        -> id int, 
        -> name varchar(50),
        -> age int(3)
        -> );
    #查看一下mysql帮我们创建表的时候的详细指令
    	show create table jaden;
    	
    #创建库和创建表的时候还可以指定字符集编码,默认字符集是Latin。
    	DEFAULT CHARACTER SET utf8mb4
    	create table jaden(id int, name varchar(50)) ENGINE=MyISAM DEFAULT CHARSET=utf8;
    # ENGINE=MyISAM这是指定存储引擎,这个后面说。
    
    #往表里面插入数据
        insert into jaden(id,name,age) value(1,'xx',18); # 插入单条数据
        insert into jaden(id,name,age) values(2,'xx2',15),(3,'xx3',19); #插入多条数据
        
    #创建只有name列的表t1;
    	create table t1(name char(6));
    
    #查看表结构
    	desc t1;
    
    #往表t1插入数据
        insert t1 value('zhang');
        insert t1 value('li');
    
    #查询t1表中所有数据
    	select * from t1;
    
    #指定字符集的创表语句
    	create table t2(name char(6),age int(3)) default charset=utf8;
    
    #往表t2插入数据
        insert t2 value('张三',20);
        insert t2 value('李四',60);
    
    #创建表t4
    	create table t4(name char(6),age int(3) default 0 ) default charset=utf8;
    
    #指定列插入数据
    	insert t4(name) values('张三'),('李四');
    
    #查询结果
        mysql> select * from t4;
        +--------+------+
        | name   | age |
        +--------+------+
        | 张三   |    0 |
        | 李四   |    0 |
        +--------+------+
        2 rows in set (0.00 sec)
    
    ##修改表
    #修改字段的长度
    	alter table s2 modify name char(10);
    
    #查看创表语句
    	show create table s2;
    
    #增加字段
    	alter table s2 add age int(3);
    
    #删除字段
    	alter table s2 drop age;
    
    #ALTER TABLE 表名 ADD 字段名 数据类型 [完整性约束条件…] FIRST; #添加这个字段的时候,把它放到第一个字段位置去。
    #ALTER TABLE 表名 ADD 字段名 数据类型 [完整性约束条件…] AFTER 字段名;#after是放到后的这个字段的后面去了,我们通过一个first和一个after就可以将新添加的字段放到表的任意字段位置了。
    
    # 修改表的字符集
    	alter table 表名 charset=utf8mb4;
    
    #使用where条件删除单条数据
    	delete from t5 where name='zhangsan';
    
    #删除所有数据
    	delete from t5;
    
    #单条件修改:
    	update t5 set password='123' where name='wangwu';
    
    #单条件修改多列:
    	update t5 set password='123',name='xxx' where name='wangwu';
    
    #多条件修改
        update t5 set password='123' where name='wangwu' and id=1;
        update t5 set password='123' where name='wangwu' or id=1;
    
    #修改所有数据
    	update t5 set password='123456';
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99

    4-10 MySQL查询数据

    #sql查询
    ## city有多少个中国的城市?
    select * from city where CountryCode='CHN';
    
    ## 查询city表中,山西省的城市名?
    select * from city where district='shanxi';
    
    ## 查询city表中,山西省和河北省的城市名?
    select * from city where district='shanxi' or district='hebei' ;
    
    ## 查询city表中,山西省和河北省的城市中人口大于100w?
    select * from city where (district='shanxi' or district='hebei') and Population >1000000 ;
    
    ## 查询city表中,要求只显示城市名和人口数量,山西省和河北省的城市名按人口数量排序,升序?
    select Name,Population from city where district='shanxi' or district='hebei'order by Population ;
    
    ## 查询city表中,要求只显示城市名和人口数量,山西省和河北省的城市名按人口数量排序,降序?
    select Name,Population from city where district='shanxi' or district='hebei'order by Population desc ;
    
    ## 查询city表中,要求只显示城市名和人口数量,山西省和河北省的城市名按人口数量前5名;
    select Name,Population from city where district='shanxi' or district='hebei'order by Population desc limit 5;
    
    ## 查询city表中,要求只显示城市名和人口数量,山西省和河北省的城市名按人口数量第2名和第3名;
    select Name,Population from city where district='shanxi' or district='hebei'order by Population desc limit 1,2;
    
    ## 查询city表中,所有中国省份中带an的城市
    select * from city where countrycode='chn' and district like '%an%' ;
    
    ## 查询city表中,所有中国的城市人口在89000和89999之间的城市
    select * from city where countrycode='chn' and Population  between 89000 and 89999 ;
    
    ## 查询city表中,要求只显示城市名和人口数量,查询CHN人口最多的前5个城市?
    ## 查询city表中,要求只显示城市名和人口数量,查询CHN人口最少的前5个城市?
    ## 查询中国的城市数量?
    select count(name) as 中国城市总数 from city where countrycode='CHN';
    
    ## 查询世界的国家数量?
    select count(name) from country;
    
    ## 查询中国的总人口?
    select sum(population) from city where countrycode='chn';
    
    ## 把多行合并成一行
    select group_concat(name) from city where countrycode='chn' and district='hebei';
    
    ## 把多列合并成一列
    select concat(Name,"#",CountryCode,"#",District) from city where countrycode='chn' and district='hebei' ;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    4-11 MySQL 索引

    #增加主键索引(要求结果唯一)
    	alter table t100w add PRIMARY KEY(id);
    
    #创建普通索引
    	alter table t100w add index num(num);
    	
    #创建联合索引
    	alter table t100w add index lianhe(k1,k2);
    
    #查看索引
    	show index from t100w;
    
    #删除普通索引
    	alter table t100w  drop index lianhe;
    
    #删除主键索引
    	alter table t100w  drop  PRIMARY key;
    
    #创建表的时候,指定索引
    	create table zhu2(id int(8) primary key AUTO_INCREMENT ,name char(10),passwd char(10));
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    4-12 MySQL Union

    #合并两个select查询结果
    CREATE TABLE `c1` (
      `ID` int NOT NULL AUTO_INCREMENT,
      `Name` char(35) NOT NULL DEFAULT '',
      `District` char(20) NOT NULL DEFAULT '',
      `Population` int NOT NULL DEFAULT '0',
     PRIMARY KEY (`ID`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
    CREATE TABLE `c2` (
      `ID` int NOT NULL AUTO_INCREMENT,
      `Name` char(35) NOT NULL DEFAULT '',
      `District` char(20) NOT NULL DEFAULT '',
      `Population` int NOT NULL DEFAULT '0',
     PRIMARY KEY (`ID`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
    
    insert into c1(ID,Name,District,Population) select ID,Name,District,Population 
    from city where CountryCode='CHN' and District='Hebei';
    
    insert into c2(ID,Name,District,Population)   select ID,Name,District,Population 
    from city where CountryCode='CHN' and District='Henan';
    
    select * from c1 union select * from c2 order by Population;
    
    #sql注入中经常会使用的
    select * from c1 union select 1,2,3,user();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    4-13 mysql存储引擎

    MyISAM: ## 读性能好,写的性能差     表级锁 每张表,三个文件
    innodb## 读性能微弱,写的性能好   行级锁 每张表,两个文件
    
    • 1
    • 2

    4-14 MySQL找回root密码

    #b适用于mariadb 10.6
    1.修改配置文件
        vim /etc/my.cnf.d/server.cnf
        [mysqld]
        skip-grant-tables
        
    2.启动mariadb
    	systemctl start mariadb
    	
    3.空密码 登录数据库并执行修改密码
        use mysql;
        update user set password=password('123') where user='root' and host='localhost';
        flush privileges;
        
    4.删除配置文件中前面增加的skip-grant-tables
    5.重启启动mariadb
    	systemctl restart mariadb
    6.使用新密码验证
    	mysql -uroot -p123
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
  • 相关阅读:
    使用 Apache Camel 和 Quarkus 的微服务(五)
    C语言 extern “C“的作用
    建立线上思维,创客匠人教你打造线上教学服务生态圈
    初入自媒体短视频行业,有什么需要注意的?怎么快速入行?
    python绘制混淆矩阵
    关于在3dsmax中制作的模型导入UE后尺寸大小不对的问题
    RTK-RTD-SBAS-WAAS-PPP-PPK-WADGPS
    同步协程的必备工具: WaitGroup
    死锁的发生与避免
    实用又好用,4款高质量办公软件,助你工作效率,节节攀升
  • 原文地址:https://blog.csdn.net/m0_67844671/article/details/133892635