数据库概述、部署MySQL服务、必备命令、密码管理、安装图形软件、SELECT语法 、筛选条件

Top

NSD DBA DAY01

  1. 案例1:构建MySQL服务器
  2. 案例2:密码管理
  3. 案例3:安装图形软件
  4. 案例4:筛选条件

1 案例1:构建MySQL服务器

1.1 问题

  1. 在IP地址192.168.88.50主机和192.168.88.51主机上部署mysql服务
  2. 练习必备命令的使用

1.2 方案

准备2台虚拟机,要求如下:

表-1

配置yum源、关闭selinux和防火墙,如果忘记了请自行补习前边课程的知识或查看今天讲课的PPT,谢谢!!!

1.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:安装软件

命令操作如下所示:

mysql-server 提供服务软件

mysql 提供命令软件

复制代码
  1. root@mysql50 \~\]# yum -y install mysql-server mysql //安装提供服务和命令软件

  2. root@mysql50 \~\]# rpm -q mysql-server mysql

  3. mysql-8.0.26-1.module+el8.4.0+652+6de068a7.x86_64
  4. root@mysql50 \~\]#

  5. root@mysql50 \~\]# rpm -qi mysql-server //查看软件信息

  6. Version : 8.0.26
  7. Release : 1.module+el8.4.0+652+6de068a7
  8. Architecture: x86_64
  9. Install Date: 2023年03月13日 星期一 12时09分38秒
  10. Group : Unspecified
  11. Size : 126674945
  12. License : GPLv2 with exceptions and LGPLv2 and BSD
  13. Signature : RSA/SHA256, 2021年09月22日 星期三 07时27分14秒, Key ID 15af5dac6d745a60
  14. Source RPM : mysql-8.0.26-1.module+el8.4.0+652+6de068a7.src.rpm
  15. Build Date : 2021年09月22日 星期三 07时06分32秒
  16. Build Host : ord1-prod-x86build005.svc.aws.rockylinux.org
  17. Relocations : (not relocatable)
  18. Packager : infrastructure@rockylinux.org
  19. Vendor : Rocky
  20. URL : http://www.mysql.com
  21. Summary : The MySQL server and related files
  22. Description :
  23. MySQL is a multi-user, multi-threaded SQL database server. MySQL is a
  24. client/server implementation consisting of a server daemon (mysqld)
  25. and many different client programs and libraries. This package contains
  26. the MySQL server and some accompanying files and directories.
  27. root@mysql50 \~\]# systemctl start mysqld //启动服务

  28. root@mysql50 \~\]# systemctl enable mysqld //设置服务开机运行

步骤二:查看端口号和进程名

命令操作如下所示:

复制代码
  1. root@mysql50 \~\]# ps -C mysqld //查看进程

  2. 21912 ? 00:00:00 mysqld
  3. root@mysql50 \~\]#

  4. tcp LISTEN 0 70 *:33060 *:* users:(("mysqld",pid=21912,fd=22))
  5. tcp LISTEN 0 128 *:3306 *:* users:(("mysqld",pid=21912,fd=25))
  6. root@mysql50 \~\]#

  7. root@mysql50 \~\]# netstat -utnlp \| grep mysqld //仅查看mysqld进程

  8. tcp6 0 0 :::3306 :::* LISTEN 21912/mysqld
  9. root@mysql50 \~\]#

MySQL 8中的3306端口是MySQL服务默认使用的端口,主要用于建立客户端与MySQL服务器之间的连接。

MySQL 8中的33060端口是MySQL Shell默认使用的管理端口,主要用于执行各种数据库管理任务。远程管理MySQL服务器:使用MySQL Shell连接到MySQL服务,并在远程管理控制台上执行各种数据库管理操作,例如创建、删除、备份和恢复数据库等。

步骤三:连接服务。

说明: 数据库管理员本机登陆默认没有密码

命令操作如下所示:

复制代码
  1. root@mysql50 \~\]# mysql //连接服务

  2. Your MySQL connection id is 8
  3. Server version: 8.0.26 Source distribution
  4. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  5. Oracle is a registered trademark of Oracle Corporation and/or its
  6. affiliates. Other names may be trademarks of their respective
  7. owners.
  8. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  9. mysql> 登陆后的提示符
  10. mysql> exit //断开连接
  11. Bye
  12. root@mysql50 \~\]#

命令操作如下所示:

复制代码
  1. root@mysql51 \~\]# yum -y install mysql-server mysql

  2. root@mysql51 \~\]# systemctl enable mysqld

  3. mysql> exit
  4. Bye
  5. root@mysql51 \~\]#

命令操作如下所示:

复制代码
  1. mysql> select version() ; //查看数据库软件版本
  2. +-----------+
  3. | version() |
  4. +-----------+
  5. | 8.0.26 |
  6. +-----------+
  7. 1 row in set (0.00 sec)
  8. mysql> select user() ; //查看登陆的用户和客户端地址
  9. +----------------+
  10. | user() |
  11. +----------------+
  12. | root@localhost | 管理员root本机登陆
  13. +----------------+
  14. 1 row in set (0.00 sec)
  15. mysql> show databases; //查看已有的库
  16. +--------------------+
  17. | Database |
  18. +--------------------+
  19. | information_schema |
  20. | mysql |
  21. | performance_schema |
  22. | sys |
  23. +--------------------+
  24. 4 rows in set (0.00 sec)
  25. mysql>

说明:

默认4个库 不可以删除,存储的是 服务运行时加载的不同功能的程序和数据。

information_schema:是MySQL数据库提供的一个虚拟的数据库,存储了MySQL数据库中的相关信息,比如数据库、表、列、索引、权限、角色等信息。它并不存储实际的数据,而是提供了一些视图和存储过程,用于查询和管理数据库的元数据信息。

mysql:存储了MySQL服务器的系统配置、用户、账号和权限信息等。它是MySQL数据库最基本的库,存储了MySQL服务器的核心信息。

performance_schema:存储了MySQL数据库的性能指标、事件和统计信息等数据,可以用于性能分析和优化。

sys:是MySQL 8.0引入的一个新库,它基于information_schema和performance_schema视图,提供了更方便、更直观的方式来查询和管理MySQL数据库的元数据和性能数据。

复制代码
  1. mysql> select database(); //查看当前在那个库里 null 表示没有在任何库里
  2. +------------+
  3. | database() |
  4. +------------+
  5. | NULL |
  6. +------------+
  7. 1 row in set (0.00 sec)
  8. mysql> use mysql ; //切换到mysql库
  9. mysql> select database(); // 再次显示所在的库
  10. +------------+
  11. | database() |
  12. +------------+
  13. | mysql |
  14. +------------+
  15. 1 row in set (0.00 sec)
  16. mysql> show tables; //显示库里已有的表
  17. +------------------------------------------------------+
  18. | Tables_in_mysql |
  19. +------------------------------------------------------+
  20. | columns_priv |
  21. | component |
  22. | db |
  23. | default_roles |
  24. | engine_cost |
  25. | func |
  26. | general_log |
  27. | global_grants |
  28. | gtid_executed |
  29. | help_category |
  30. | help_keyword |
  31. | help_relation |
  32. | help_topic |
  33. | innodb_index_stats |
  34. | innodb_table_stats |
  35. | password_history |
  36. | plugin |
  37. | procs_priv |
  38. | proxies_priv |
  39. | replication_asynchronous_connection_failover |
  40. | replication_asynchronous_connection_failover_managed |
  41. | replication_group_configuration_version |
  42. | replication_group_member_actions |
  43. | role_edges |
  44. | server_cost |
  45. | servers |
  46. | slave_master_info |
  47. | slave_relay_log_info |
  48. | slave_worker_info |
  49. | slow_log |
  50. | tables_priv |
  51. | time_zone |
  52. | time_zone_leap_second |
  53. | time_zone_name |
  54. | time_zone_transition |
  55. | time_zone_transition_type |
  56. | user |
  57. +------------------------------------------------------+
  58. 37 rows in set (0.00 sec)
  59. mysql> exit ; 断开连接
  60. Bye
  61. root@mysql50 \~\]#

2.1 问题

1) 在192.168.88.50主机做如下练习:

  1. 设置root密码为tarena
  2. 修改root密码为123qqq...A
  3. 破解root密码为NSD2023...a

2.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:设置root密码为tarena

命令操作如下所示:

2行输出是警告而已不用关心

复制代码
  1. root@mysql50 \~\]# mysqladmin -uroot -p password "tarena"

  2. mysqladmin: [Warning] Using a password on the command line interface can be insecure.
  3. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
  4. root@mysql50 \~\]# mysql //无密码连接被拒绝

  5. root@mysql50 \~\]#

  6. mysql: [Warning] Using a password on the command line interface can be insecure.
  7. Welcome to the MySQL monitor. Commands end with ; or \g.
  8. Your MySQL connection id is 14
  9. Server version: 8.0.26 Source distribution
  10. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  11. Oracle is a registered trademark of Oracle Corporation and/or its
  12. affiliates. Other names may be trademarks of their respective
  13. owners.
  14. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  15. mysql> 登陆成功

步骤二:修改root密码为123qqq...A

命令操作如下所示:

复制代码
  1. root@mysql50 \~\]# mysqladmin -uroot -ptarena password "123qqq...A" //修改密码

  2. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
  3. root@mysql50 \~\]# mysql -uroot --ptarena //旧密码无法登陆

  4. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
  5. root@mysql50 \~\]# mysql -uroot -p123qqq...A //新密码登陆

  6. Welcome to the MySQL monitor. Commands end with ; or \g.
  7. Your MySQL connection id is 18
  8. Server version: 8.0.26 Source distribution
  9. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  10. Oracle is a registered trademark of Oracle Corporation and/or its
  11. affiliates. Other names may be trademarks of their respective
  12. owners.
  13. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  14. mysql> 登陆成功

步骤三:破解root密码为NSD2023...a

说明:在mysql50主机做此练习

命令操作如下所示:

复制代码
  1. root@mysql50 \~\]# mysql -uroot -pNSD2023...a //破解前登陆失败

  2. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
  3. root@mysql50 \~\]# vim /etc/my.cnf.d/mysql-server.cnf //修改主配置文件

  4. skip-grant-tables //手动添加此行 作用登陆时不验证密码
  5. :wq
  6. root@mysql50 \~\]# systemctl restart mysqld //重启服务 作用让服务以新配置运行

  7. Welcome to the MySQL monitor. Commands end with ; or \g.
  8. Your MySQL connection id is 7
  9. Server version: 8.0.26 Source distribution
  10. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  11. Oracle is a registered trademark of Oracle Corporation and/or its
  12. affiliates. Other names may be trademarks of their respective
  13. owners.
  14. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  15. //把mysql库下user表中 用户root的密码设置为无;
  16. mysql> update mysql.user set authentication_string="" where user="root";
  17. Query OK, 1 row affected (0.05 sec)
  18. Rows matched: 1 Changed: 1 Warnings: 0
  19. mysql> exit; 断开连接
  20. Bye
  21. root@mysql50 \~\]# vim /etc/my.cnf.d/mysql-server.cnf 编辑配置文件

  22. #skip-grant-tables //注释添加的行
  23. :wq
  24. root@mysql50 \~\]# systemctl restart mysqld //重启服务 作用让注释生效

  25. Welcome to the MySQL monitor. Commands end with ; or \g.
  26. Your MySQL connection id is 8
  27. Server version: 8.0.26 Source distribution
  28. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  29. Oracle is a registered trademark of Oracle Corporation and/or its
  30. affiliates. Other names may be trademarks of their respective
  31. owners.
  32. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  33. //设置root用户本机登陆密码
  34. mysql> alter user root@"localhost" identified by "NSD2023...a";
  35. Query OK, 0 rows affected (0.00 sec)
  36. mysql> exit 断开连接
  37. Bye
  38. root@localhost \~\]# mysql 不输密码无法登陆

  39. root@localhost \~\]# mysql -uroot -pNSD2023...a 使用破解的密码登陆

  40. Welcome to the MySQL monitor. Commands end with ; or \g.
  41. Your MySQL connection id is 10
  42. Server version: 8.0.26 Source distribution
  43. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  44. Oracle is a registered trademark of Oracle Corporation and/or its
  45. affiliates. Other names may be trademarks of their respective
  46. owners.
  47. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  48. mysql>
  49. mysql> 登陆成功
  50. mysql> show databases; 查看已有的库
  51. +--------------------+
  52. | Database |
  53. +--------------------+
  54. | information_schema |
  55. | mysql |
  56. | performance_schema |
  57. | sys |
  58. +--------------------+
  59. 4 rows in set (0.01 sec)

3 案例3:安装图形软件

3.1 问题

  • 在IP地址192.168.88.50主机安装phpmyadmin软件
  • 客户端通过访问phpmyadmin软件管理数据库

3.2 方案

把用到的软件拷贝的虚拟机mysql50里

在mysql50主机,首先配置运行环境LNP,然后安装phpmyadmin软件,最后打开真机的浏览器输入phpmyadmin的网址访问。

3.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:部署运行环境LNP

命令操作如下所示:

复制代码
  1. gcc 源码包编译工具
  2. unzip 提供解压.zip 压缩包命令
  3. make 源码软件编译命令
  4. pcre-devel 支持正则表达式
  5. zlib-devel 提供数据压缩命令
  6. root@mysql50 \~\]# yum -y install gcc unzip make pcre-devel zlib-devel //安装依赖

  7. root@mysql50 \~\]# cd nginx-1.22.1 //进源码目录

  8. root@mysql50 nginx-1.22.1\]# make \&\& make install //编译并安装

  9. conf html logs sbin
  10. root@mysql50 nginx-1.22.1\]# vim /usr/local/nginx/conf/nginx.conf //修改主配置文件

  11. 44 root html;
  12. 45 index index.php index.html index.htm; //添加php首页名
  13. 46 }
  14. 65 location ~ \.php$ { //访问.php的请求转给本机的9000端口
  15. 66 root html;
  16. 67 fastcgi_pass 127.0.0.1:9000;
  17. 68 fastcgi_index index.php;
  18. 69 #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  19. 70 include fastcgi.conf; //保存nginx变量文件
  20. 71 }
  21. :wq
  22. root@mysql50 nginx-1.22.1\]# /usr/local/nginx/sbin/nginx //启动服务

  23. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 42182/nginx: master
  24. root@mysql50 nginx-1.22.1\]#

  25. php 解释php代码
  26. php-devel php扩展包
  27. php-mysqlnd 连接mysql命令包
  28. php-json 支持json代码
  29. php-fpm 提供fpm服务
  30. root@mysql50 \~\]# yum -y install php php-devel php-mysqlnd php-json php-fpm //安装php软件

  31. 38 ;listen = /run/php-fpm/www.sock
  32. 39 listen = 127.0.0.1:9000 //非socket方式运行,不是必须的
  33. :wq
  34. root@mysql50 \~\]# systemctl start php-fpm //启动服务

  35. tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 67251/php-fpm: mast
  36. root@mysql50 \~\]#

  37. <?php
  38. $name = "plj" ;
  39. echo $name ;
  40. echo "\n" ;
  41. ?>
  42. :wq
  43. root@mysql50 \~\]# curl http://localhost/test.php //访问脚本

  44. root@mysql50 \~\]#

命令操作如下所示:

复制代码
  1. root@mysql50 \~\]# unzip phpMyAdmin-5.2.1-all-languages.zip //解压

  2. root@mysql50 \~\]# cd /usr/local/nginx/html/phpmyadmin/ //进软件目录

  3. root@mysql50 phpmyadmin\]# vim config.inc.php //修改主配置文件

  4. 16 $cfg['blowfish_secret'] = 'plj123'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
  5. //管理本机的数据库服务
  6. 30 cfg\['Servers'\]\[i]['host'] = 'localhost';
  7. :wq
  8. root@mysql50 phpmyadmin\]# setenforce 0 //关闭selinux

步骤三:客户端访问

命令操作如下所示:

复制代码
  1. http://192.168.88.50/phpmyadmin 打开浏览器输入此网址 效果如图-1所示

图-1

说明:输入数据库管理员root 和 密码 成功后如图-2所示

4 案例4:筛选条件

4.1 问题

  1. 准备练习环境
  2. 练习数值比较
  3. 练习范围匹配
  4. 练习模糊匹配
  5. 练习正则匹配
  6. 练习逻辑比较
  7. 练习字符比较/空/非空
  8. 练习别名/去重/合并

4.2 方案

拷贝tarena.sql文件到mysql50主机里,然后使用tarena.sql创建练习使用的数据。

4.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:准备练习环境

复制代码
  1. //拷贝tarena.sql 拷贝到 mysql50主机的/root 下
  2. openeuler@server1 \~\]$ scp /linux-soft/s3/tarena.sql root@192.168.88.50:/root/

  3. tarena.sql 100% 284KB 171.9MB/s 00:00
  4. //连接mysql50主机
  5. openeuler@server1 \~\]$ ssh root@192.168.88.50

  6. Last login: Tue May 23 10:59:57 2023 from 192.168.88.254
  7. //恢复数据
  8. root@mysql50 \~\]# mysql -uroot -pNSD2023...a \< /root/tarena.sql

  9. //连接服务
  10. root@mysql50 \~\]# mysql -uroot -pNSD2023...a

  11. +--------------------+
  12. | Database |
  13. +--------------------+
  14. | information_schema |
  15. | mysql |
  16. | performance_schema |
  17. | sys |
  18. | tarena | 恢复的库
  19. +--------------------+
  20. 5 rows in set (0.00 sec)
  21. mysql> use tarena; //进入库
  22. Reading table information for completion of table and column names
  23. You can turn off this feature to get a quicker startup with -A
  24. Database changed
  25. mysql> show tables; //查看表
  26. +------------------+
  27. | Tables_in_tarena |
  28. +------------------+
  29. | departments | 部门表
  30. | employees | 员工表
  31. | salary | 工资表
  32. | user | 用户表
  33. +------------------+
  34. 4 rows in set (0.00 sec)

使用user 表做查询练习

user表里存储的是 系统用户信息 就是 /etc/passwd 文件的内容

复制代码
  1. mysql> desc tarena.user; //查看表头
  2. +----------+-------------+------+-----+---------+----------------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +----------+-------------+------+-----+---------+----------------+
  5. | id | int(11) | NO | PRI | NULL | auto_increment |行号
  6. | name | char(20) | YES | | NULL | |用户名
  7. | password | char(1) | YES | | NULL | |密码占位符
  8. | uid | int(11) | YES | | NULL | | uid号
  9. | gid | int(11) | YES | | NULL | | gid号
  10. | comment | varchar(50) | YES | | NULL | | 描述信息
  11. | homedir | varchar(80) | YES | | NULL | | 家目录
  12. | shell | char(30) | YES | | NULL | | 解释器
  13. +----------+-------------+------+-----+---------+----------------+
  14. 8 rows in set (0.00 sec)

select命令格式演示

语法格式1 SELECT 字段列表 FROM 库名.表名;

语法格式2 SELECT 字段列表 FROM 库名.表名 where 筛选条件;

复制代码
  1. mysql> select name from tarena.user; //查看一个表头
  2. mysql> select name ,uid from tarena.user; //查看多个表头
  3. mysql> select * from tarena.user; //查看所有表头

加筛选条件

复制代码
  1. mysql> select * from tarena.user where name = "root"; //查找root用户信息
  2. +----+------+----------+------+------+---------+---------+-----------+
  3. | id | name | password | uid | gid | comment | homedir | shell |
  4. +----+------+----------+------+------+---------+---------+-----------+
  5. | 1 | root | x | 0 | 0 | root | /root | /bin/bash |
  6. +----+------+----------+------+------+---------+---------+-----------+
  7. 1 row in set (0.00 sec)
  8. mysql>
  9. mysql> select * from tarena.user where id = 2 ; //查找第2行用户信息
  10. +----+------+----------+------+------+---------+---------+--------------+
  11. | id | name | password | uid | gid | comment | homedir | shell |
  12. +----+------+----------+------+------+---------+---------+--------------+
  13. | 2 | bin | x | 1 | 1 | bin | /bin | /sbin/nologin |
  14. +----+------+----------+------+------+---------+---------+--------------+
  15. 1 row in set (0.00 sec)

步骤二:练习数值比较

比较符号:

= != > >= < <=

相等 不相等 大于 大于等于 小于 小于等于

符号两边要是数字或数值类型的表头 符号左边与符号右边做比较

复制代码
  1. //查看第3行的行号、用户名、uid、gid 四个表头的值
  2. mysql> select id,name,uid,gid from tarena.user where id = 3;
  3. +----+--------+------+------+
  4. | id | name | uid | gid |
  5. +----+--------+------+------+
  6. | 3 | daemon | 2 | 2 |
  7. +----+--------+------+------+
  8. 1 row in set (0.00 sec)
  9. //查看前2行的行号用户名、uid、gid 四个表头的值
  10. mysql> select id,name,uid,gid from tarena.user where id < 3;
  11. +----+------+------+------+
  12. | id | name | uid | gid |
  13. +----+------+------+------+
  14. | 1 | root | 0 | 0 |
  15. | 2 | bin | 1 | 1 |
  16. +----+------+------+------+
  17. 2 rows in set (0.00 sec)
  18. //查看前3行的行号、用户名、uid、gid 四个表头的值
  19. mysql> select id,name,uid,gid from tarena.user where id <= 3;
  20. +----+--------+------+------+
  21. | id | name | uid | gid |
  22. +----+--------+------+------+
  23. | 1 | root | 0 | 0 |
  24. | 2 | bin | 1 | 1 |
  25. | 3 | daemon | 2 | 2 |
  26. +----+--------+------+------+
  27. 3 rows in set (0.00 sec)
  28. //查看前uid号大于6000的行号、用户名、uid、gid 四个表头的值
  29. mysql> select id,name,uid,gid from tarena.user where uid > 6000;
  30. +----+-----------+-------+-------+
  31. | id | name | uid | gid |
  32. +----+-----------+-------+-------+
  33. | 22 | nfsnobody | 65534 | 65534 |
  34. +----+-----------+-------+-------+
  35. 1 row in set (0.00 sec)
  36. //查看前uid号大于等于1000的行号、用户名、uid、gid 四个表头的值
  37. mysql> select id,name,uid,gid from tarena.user where uid >= 1000;
  38. +----+-----------+-------+-------+
  39. | id | name | uid | gid |
  40. +----+-----------+-------+-------+
  41. | 22 | nfsnobody | 65534 | 65534 |
  42. | 24 | plj | 1000 | 1000 |
  43. +----+-----------+-------+-------+
  44. 2 rows in set (0.00 sec)
  45. //查看uid号和gid号相同的行 仅显示行号、用户名、uid、gid 四个表头的值
  46. mysql> select id,name,uid,gid from tarena.user where uid = gid;
  47. +----+-----------------+-------+-------+
  48. | id | name | uid | gid |
  49. +----+-----------------+-------+-------+
  50. | 1 | root | 0 | 0 |
  51. | 2 | bin | 1 | 1 |
  52. | 3 | daemon | 2 | 2 |
  53. | 13 | nobody | 99 | 99 |
  54. | 14 | systemd-network | 192 | 192 |
  55. | 15 | dbus | 81 | 81 |
  56. | 17 | sshd | 74 | 74 |
  57. | 18 | postfix | 89 | 89 |
  58. | 20 | rpc | 32 | 32 |
  59. | 21 | rpcuser | 29 | 29 |
  60. | 22 | nfsnobody | 65534 | 65534 |
  61. | 23 | haproxy | 188 | 188 |
  62. | 24 | plj | 1000 | 1000 |
  63. | 25 | apache | 48 | 48 |
  64. | 26 | mysql | 27 | 27 |
  65. +----+-----------------+-------+-------+
  66. 15 rows in set (0.00 sec)
  67. //查看uid号和gid号不一样的行 仅显示行号、用户名、uid、gid 四个表头的值
  68. mysql> select id,name,uid,gid from tarena.user where uid != gid;
  69. +----+----------+------+------+
  70. | id | name | uid | gid |
  71. +----+----------+------+------+
  72. | 4 | adm | 3 | 4 |
  73. | 5 | lp | 4 | 7 |
  74. | 6 | sync | 5 | 0 |
  75. | 7 | shutdown | 6 | 0 |
  76. | 8 | halt | 7 | 0 |
  77. | 9 | mail | 8 | 12 |
  78. | 10 | operator | 11 | 0 |
  79. | 11 | games | 12 | 100 |
  80. | 12 | ftp | 14 | 50 |
  81. | 16 | polkitd | 999 | 998 |
  82. | 19 | chrony | 998 | 996 |
  83. +----+----------+------+------+
  84. 11 rows in set (0.00 sec)
  85. mysql>

步骤三:练习范围匹配

in (值列表) //在...里

not in (值列表) //不在...里

between 数字1 and 数字2 //在...之间

命令操作如下所示:

复制代码
  1. //uid号表头的值 是 (1 , 3 , 5 , 7) 中的任意一个即可
  2. mysql> select name , uid from tarena.user where uid in (1 , 3 , 5 , 7);
  3. +------+------+
  4. | name | uid |
  5. +------+------+
  6. | bin | 1 |
  7. | adm | 3 |
  8. | sync | 5 |
  9. | halt | 7 |
  10. +------+------+
  11. //shell 表头的的值 不是 "/bin/bash"或"/sbin/nologin" 即可
  12. mysql> select name , shell from tarena.user where shell not in ("/bin/bash","/sbin/nologin");
  13. +----------+----------------+
  14. | name | shell |
  15. +----------+----------------+
  16. | sync | /bin/sync |
  17. | shutdown | /sbin/shutdown |
  18. | halt | /sbin/halt |
  19. | mysql | /bin/false |
  20. +----------+----------------+
  21. //id表头的值 在 10 到 20 之间即可 包括 10 和 20 本身
  22. mysql> select id , name , uid from tarena.user where id between 10 and 20 ;
  23. +----+-----------------+------+
  24. | id | name | uid |
  25. +----+-----------------+------+
  26. | 10 | operator | 11 |
  27. | 11 | games | 12 |
  28. | 12 | ftp | 14 |
  29. | 13 | nobody | 99 |
  30. | 14 | systemd-network | 192 |
  31. | 15 | dbus | 81 |
  32. | 16 | polkitd | 999 |
  33. | 17 | sshd | 74 |
  34. | 18 | postfix | 89 |
  35. | 19 | chrony | 998 |
  36. | 20 | rpc | 32 |
  37. +----+-----------------+------+
  38. 11 rows in set (0.00 sec)mysql>

步骤四:练习模糊匹配

where 字段名 like "表达式";

通配符

_ 表示 1个字符

% 表示零个或多个字符

命令操作如下所示:

复制代码
  1. //找名字必须是3个字符的 (没有空格挨着敲)
  2. mysql> select name from tarena.user where name like "___";
  3. +------+
  4. | name |
  5. +------+
  6. | bin |
  7. | adm |
  8. | ftp |
  9. | rpc |
  10. | plj |
  11. | bob |
  12. +------+
  13. 6 rows in set (0.00 sec)
  14. //找名字必须是4个字符的(没有空格挨着敲)
  15. mysql> select name from tarena.user where name like "_ _ _ _";
  16. +------+
  17. | name |
  18. +------+
  19. | root |
  20. | sync |
  21. | halt |
  22. | mail |
  23. | dbus |
  24. | sshd |
  25. | null |
  26. +------+
  27. 7 rows in set (0.00 sec)
  28. //找名字以字母a开头的(没有空格挨着敲)
  29. mysql> select name from tarena.user where name like "a%";
  30. //查找名字至少是4个字符的表达式
  31. mysql> select name from tarena.user where name like "%_ _ _ _%";(没有空格挨着敲)
  32. mysql> select name from tarena.user where name like "_ % _";(没有空格挨着敲)
  33. mysql> select name from tarena.user where name like "_ _ _ _%";(没有空格挨着敲)

步骤五:练习正则匹配

格式:select 字段名列表 from 库名.表名 where字段名 regexp '正则表达式';

回顾shell课程学过的元字符(正则符号)

^ 匹配行首

$ 匹配行尾

\] 匹配范围内任意一个 \* 前边的表达式出现零次或多次 \| 或者 . 任意一个字符 命令操作如下所示: ``` ``` 1. //添加有数字的名字 2. insert into tarena.user(name)values("yaya9"); 3. insert into tarena.user(name)values("6yaya"); 4. insert into tarena.user(name)values("ya7ya"); 5. insert into tarena.user(name)values("yay8a"); 6. //查看名字里有数字的 7. mysql\> select name from tarena.user where name regexp "\[0-9\]"; 8. +-------+ 9. \| name \| 10. +-------+ 11. \| yaya9 \| 12. \| 6yaya \| 13. \| ya7ya \| 14. \| yay8a \| 15. +-------+ 16. 4 rows in set (0.00 sec) 17. //查看名字以数字开头 18. mysql\> select name from tarena.user where name regexp "\^\[0-9\]"; 19. +-------+ 20. \| name \| 21. +-------+ 22. \| 6yaya \| 23. +-------+ 24. 1 row in set (0.00 sec) 25. //查看名字以数字结尾 26. mysql\> select name from tarena.user where name regexp "\[0-9\]$"; 27. +-------+ 28. \| name \| 29. +-------+ 30. \| yaya9 \| 31. +-------+ 32. 1 row in set (0.00 sec) 33. mysql\> 34. //查看名字以r开头 35. mysql\> select name from tarena.user where name regexp "\^r"; 36. +---------+ 37. \| name \| 38. +---------+ 39. \| root \| 40. \| rpc \| 41. \| rpcuser \| 42. +---------+ 43. 3 rows in set (0.00 sec) 44. //查看名字以t结尾 45. mysql\> select name from tarena.user where name regexp "t$"; 46. +------+ 47. \| name \| 48. +------+ 49. \| root \| 50. \| halt \| 51. +------+ 52. 2 rows in set (0.00 sec) 53. mysql\> 54. //查看名字以r开头或t结尾 55. mysql\> select name from tarena.user where name regexp "\^r\|t$"; 56. +---------+ 57. \| name \| 58. +---------+ 59. \| root \| 60. \| halt \| 61. \| rpc \| 62. \| rpcuser \| 63. +---------+ 64. 4 rows in set (0.00 sec) 65. //名字r开头t结尾 66. mysql\> select name from tarena.user where name regexp "\^r.\*t$"; 67. +------+ 68. \| name \| 69. +------+ 70. \| root \| 71. +------+ 72. 1 row in set (0.00 sec) 73. mysql\> 步骤六:练习逻辑比较 多个判断条件 逻辑与 and (\&\&) 多个判断条件必须同时成立 逻辑或 or (\|\|) 多个判断条件其中某个条件成立即可 逻辑非 not (!) 取反 命令操作如下所示: ``` ``` 1. //逻辑非例子,查看解释器不是/bin/bash 的 2. mysql\> select name,shell from tarena.user where shell != "/bin/bash"; 3. //not 也是取反 要放在表达式的前边 4. mysql\> select name,shell from tarena.user where not shell = "/bin/bash"; 5. //id值不在 10 到 20 之间 6. mysql\> select id , name from tarena.user where not id between 10 and 20 ; 7. 8. //逻辑与 例子 9. mysql\> select name , uid from tarena.user where name="root" and uid = 1; 10. Empty set (0.00 sec) 11. mysql\> select name , uid from tarena.user where name="root" and uid = 0; 12. +------+------+ 13. \| name \| uid \| 14. +------+------+ 15. \| root \| 0 \| 16. +------+------+ 17. 1 row in set (0.00 sec) 18. //逻辑或 例子 19. mysql\> select name , uid from tarena.user where name = "root" or name = "bin" or uid = 1; 20. +------+------+ 21. \| name \| uid \| 22. +------+------+ 23. \| root \| 0 \| 24. \| bin \| 1 \| 25. +------+------+ 26. mysql\> () 提高优先级 ``` ``` 1. mysql\> select 2 + 3 \* 5 ; //使用默认计算顺序 先乘除后加减 2. +------------+ 3. \| 2 + 3 \* 5 \| 4. +------------+ 5. \| 17 \| 6. +------------+ 7. 1 row in set (0.00 sec) 8. 9. mysql\> select (2 + 3 ) \* 5 ; //先加法再乘法 10. +---------------+ 11. \| (2 + 3 ) \* 5 \| 12. +---------------+ 13. \| 25 \| 14. +---------------+ 15. 1 row in set (0.00 sec) 16. 17. mysql\> 逻辑匹配什么时候需要加() 逻辑与and 优先级高于逻辑或 or 如果在筛选条件里既有and 又有 or 默认先判断and 再判断or ``` ``` 1. //没加() 的查询结果 2. select name , uid from tarena.user 3. where name = "root" or name = "bin" and uid = 1 ; 4. +------+------+ 5. \| name \| uid \| 6. +------+------+ 7. \| root \| 0 \| 8. \| bin \| 1 \| 9. +------+------+ 10. 2 rows in set (0.00 sec) 11. //加()的查询结果 12. select name , uid from tarena.user 13. where (name = "root" or name = "bin") and uid = 1 ; 14. +------+------+ 15. \| name \| uid \| 16. +------+------+ 17. \| bin \| 1 \| 18. +------+------+ 19. 1 row in set (0.00 sec) 20. mysql\> 步骤七:练习字符比较/空/非空 符号两边必须是字符 或字符类型的表头 = 相等比较 != 不相等比较。 命令操作如下所示: ``` ``` 1. //查看表里是否有名字叫apache的用户 2. mysql\> select name from tarena.user where name="apache" ; 3. +--------+ 4. \| name \| 5. +--------+ 6. \| apache \| 7. +--------+ 8. 1 row in set (0.00 sec) 9. //输出解释器不是/bin/bash的用户名 及使用的解释器 10. mysql\> select name , shell from tarena.user where shell != "/bin/bash"; 11. +-----------------+----------------+ 12. \| name \| shell \| 13. +-----------------+----------------+ 14. \| bin \| /sbin/nologin \| 15. \| daemon \| /sbin/nologin \| 16. \| adm \| /sbin/nologin \| 17. \| lp \| /sbin/nologin \| 18. \| sync \| /bin/sync \| 19. \| shutdown \| /sbin/shutdown \| 20. \| halt \| /sbin/halt \| 21. \| mail \| /sbin/nologin \| 22. \| operator \| /sbin/nologin \| 23. \| games \| /sbin/nologin \| 24. \| ftp \| /sbin/nologin \| 25. \| nobody \| /sbin/nologin \| 26. \| systemd-network \| /sbin/nologin \| 27. \| dbus \| /sbin/nologin \| 28. \| polkitd \| /sbin/nologin \| 29. \| sshd \| /sbin/nologin \| 30. \| postfix \| /sbin/nologin \| 31. \| chrony \| /sbin/nologin \| 32. \| rpc \| /sbin/nologin \| 33. \| rpcuser \| /sbin/nologin \| 34. \| nfsnobody \| /sbin/nologin \| 35. \| haproxy \| /sbin/nologin \| 36. \| apache \| /sbin/nologin \| 37. \| mysql \| /bin/false \| 38. +-----------------+----------------+ 39. 24 rows in set (0.00 sec) 40. mysql\> 空 is null 表头下没有数据 非空 is not null 表头下有数据 mysql服务 使用关键字 null 或 NULL 表示表头没有数据 ``` ``` 1. //添加新行 仅给行中的id 表头和name表头赋值 2. mysql\> insert into tarena.user(id,name) values(71,""); //零个字符 3. mysql\> insert into tarena.user(id,name) values(72,"null");//普通字母 4. mysql\> insert into tarena.user(id,name) values(73,NULL); //表示空 5. mysql\> insert into tarena.user(id,name) values(74,null); //表示空 6. //查看id表头值大于等于70 的行 仅显示行中 id表头 和 name 表头的值 7. mysql\> select id , name from tarena.user where id \>= 71; 8. +----+------+ 9. \| id \| name \| 10. +----+------+ 11. \| 71 \| \| 12. \| 72 \| null \| 13. \| 73 \| NULL \| 14. \| 74 \| NULL \| 15. +----+------+ 16. //查看name 表头没有数据的行 仅显示行中id表头 和 naeme 表头的值 17. mysql\> select id , name from tarena.user where name is null; 18. +----+------+ 19. \| id \| name \| 20. +----+------+ 21. \| 28 \| NULL \| 22. \| 29 \| NULL \| 23. \| 73 \| NULL \| 24. \| 74 \| NULL \| 25. +----+------+ 26. //查看name 表头是0个字符的行, 仅显示行中id表头 和 naeme 表头的值 27. mysql\> select id , name from tarena.user where name=""; 28. +----+------+ 29. \| id \| name \| 30. +----+------+ 31. \| 71 \| \| 32. +----+------+ 33. 1 row in set (0.00 sec) 34. //查看name 表头值是null的行, 仅显示行中id表头 和 naeme 表头的值 35. mysql\> select id , name from tarena.user where name="null"; 36. +----+------+ 37. \| id \| name \| 38. +----+------+ 39. \| 72 \| null \| 40. +----+------+ 41. 1 row in set (0.00 sec) 42. //查看name 表头有数据的行, 仅显示行中id表头 和 name 表头的值 43. mysql\> select id , name from tarena.user where name is not null; 44. +----+-----------------+ 45. \| id \| name \| 46. +----+-----------------+ 47. \| 1 \| root \| 48. \| 2 \| bin \| 49. \| 3 \| daemon \| 50. \| 4 \| adm \| 51. \| 5 \| lp \| 52. .... 53. .... 54. \| 27 \| bob \| 55. \| 71 \| \| 56. \| 72 \| null \| 57. +----+-----------------+ 步骤八:练习别名/去重/合并 命令操作如下所示: ``` ``` 1. //定义别名使用 as 或 空格 2. mysql\> select name , homedir from tarena.user; 3. mysql\> select name as 用户名 , homedir 家目录 from tarena.user; 4. //拼接 concat() 5. mysql\> select concat(name,"-",uid) as 用户信息 from tarena.user where uid \<= 5; 6. +--------------+ 7. \| 用户信息 \| 8. +--------------+ 9. \| root-0 \| 10. \| bin-1 \| 11. \| daemon-2 \| 12. \| adm-3 \| 13. \| lp-4 \| 14. \| sync-5 \| 15. +--------------+ 16. 6 rows in set (0.00 sec) 17. //2列拼接 18. mysql\> select concat(name , "-" , uid) as 用户信息 from tarena.user where uid \<= 5; 19. //多列拼接 20. mysql\> select concat(name , "-" , uid , "-" , gid) as 用户信息 from tarena.user where uid \<= 5; 21. +--------------+ 22. \| 用户信息 \| 23. +--------------+ 24. \| root-0-0 \| 25. \| bin-1-1 \| 26. \| daemon-2-2 \| 27. \| adm-3-4 \| 28. \| lp-4-7 \| 29. \| sync-5-0 \| 30. +--------------+ 去重显示 distinct 字段名列表 ``` ``` 1. //去重前输出 2. mysql\> select shell from tarena.user where shell in ("/bin/bash","/sbin/nologin") ; 3. +---------------+ 4. \| shell \| 5. +---------------+ 6. \| /bin/bash \| 7. \| /sbin/nologin \| 8. \| /sbin/nologin \| 9. \| /sbin/nologin \| 10. \| /sbin/nologin \| 11. \| /sbin/nologin \| 12. \| /sbin/nologin \| 13. \| /sbin/nologin \| 14. \| /sbin/nologin \| 15. \| /sbin/nologin \| 16. \| /sbin/nologin \| 17. \| /sbin/nologin \| 18. \| /sbin/nologin \| 19. \| /sbin/nologin \| 20. \| /sbin/nologin \| 21. \| /sbin/nologin \| 22. \| /sbin/nologin \| 23. \| /sbin/nologin \| 24. \| /sbin/nologin \| 25. \| /sbin/nologin \| 26. \| /bin/bash \| 27. \| /sbin/nologin \| 28. +---------------+ 29. 22 rows in set (0.00 sec) 30. //去重后查看 31. mysql\> select distinct shell from tarena.user where shell in ("/bin/bash","/sbin/nologin") ; 32. +---------------+ 33. \| shell \| 34. +---------------+ 35. \| /bin/bash \| 36. \| /sbin/nologin \| 37. +---------------+ 38. 2 rows in set (0.01 sec) 39. mysql\>

相关推荐
whysqwhw10 分钟前
OkHttp深度架构缺陷分析与演进规划
android
用户70937225385112 分钟前
Android14 SystemUI NotificationShadeWindowView 加载显示过程
android
木叶丸1 小时前
跨平台方案该如何选择?
android·前端·ios
顾林海2 小时前
Android ClassLoader加载机制详解
android·面试·源码
用户2018792831672 小时前
🎨 童话:Android画布王国的奇妙冒险
android
whysqwhw2 小时前
OkHttp框架的全面深入架构分析
android
你过来啊你2 小时前
Android App冷启动流程详解
android
泓博3 小时前
KMP(Kotlin Multiplatform)改造(Android/iOS)老项目
android·ios·kotlin
移动开发者1号3 小时前
使用Baseline Profile提升Android应用启动速度的终极指南
android·kotlin
移动开发者1号3 小时前
解析 Android Doze 模式与唤醒对齐
android·kotlin