# Mysql数据库 **Repository Path**: xiaoxinbupa/mysql-database ## Basic Information - **Project Name**: Mysql数据库 - **Description**: 数据是一切源头,而mysql是一门很深奥的技术 - **Primary Language**: Unknown - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-08-17 - **Last Updated**: 2024-09-06 ## Categories & Tags **Categories**: Uncategorized **Tags**: 学习笔记 ## README # Mysql数据库 #### 介绍 数据是一切源头,而mysql是一门很深奥的技术 ## Mysql数据库 ### 构建MySQL服务器 #### 1.1 目标 1. 在IP地址192.168.88.50主机和192.168.88.51主机上部署mysql服务 2. 必备命令的使用 #### 1.2 解析 **数据库定义**:数仓 储存数据的仓库 数据:文本文件、视频文件、音频文件、图片文件 库: 仓库,有很大磁盘储存空间的服务器 总:储存数据的一种服务 **数据库服务软件**:商业软件、开源软件 //开源不等于免费软件 ![image-20240708093019926](https://gitee.com/xiaoxinbupa/linux-note/raw/master/linux_base_picture/image-20240708093019926.png) -------前四个是硬盘储存数据库,后三个是内存储存数据库 - **数据库类型** ![image-20240708093311838](https://gitee.com/xiaoxinbupa/linux-note/raw/master/linux_base_picture/image-20240708093311838.png) ![image-20240708093328673](https://gitee.com/xiaoxinbupa/linux-note/raw/master/linux_base_picture/image-20240708093328673.png) - **Mysql数据库version** ![image-20240708093503086](https://gitee.com/xiaoxinbupa/linux-note/raw/master/linux_base_picture/image-20240708093503086.png) - **mysql特点与应用** ![image-20240708093825340](https://gitee.com/xiaoxinbupa/linux-note/raw/master/linux_base_picture/image-20240708093825340.png) - **数据库参数** ![image-20240708095230007](https://gitee.com/xiaoxinbupa/linux-note/raw/master/linux_base_picture/image-20240708095230007.png) **主配置文件信息** ![image-20240708102458249](https://gitee.com/xiaoxinbupa/linux-note/raw/master/linux_base_picture/image-20240708102458249.png) ![image-20240708102332956](https://gitee.com/xiaoxinbupa/linux-note/raw/master/linux_base_picture/image-20240708102332956.png) #### 1.3 步骤 准备2台虚拟机,要求如下:(配置yum源、关闭selinux和防火墙) 表-1 ![image-20240708084251514](https://gitee.com/xiaoxinbupa/linux-note/raw/master/linux_base_picture/image-20240708084251514.png) 准备虚拟机(在真机里执行如下命令) ```bash vm clone mysql50 mysql51 vm setip mysql50 192.168.88.50 vm setip mysql51 192.168.88.51 ``` **步骤一:安装软件** 命令操作如下所示: **mysql-server 提供服务软件** **mysql 提供命令软件** ```bash //安装提供服务和命令软件 [root@mysql50 ~]# yum -y install mysql-server mysql 软件已安装 [root@mysql50 ~]# rpm -q mysql-server mysql mysql-server-8.0.26-1.module+el8.4.0+652+6de068a7.x86_64 mysql-8.0.26-1.module+el8.4.0+652+6de068a7.x86_64 [root@mysql50 ~]# //查看软件信息 [root@mysql50 ~]# rpm -qi mysql-server Name : mysql-server Version : 8.0.26 Release : 1.module+el8.4.0+652+6de068a7 Architecture: x86_64 Install Date: 2023年03月13日 星期一 12时09分38秒 Group : Unspecified Size : 126674945 License : GPLv2 with exceptions and LGPLv2 and BSD Signature : RSA/SHA256, 2021年09月22日 星期三 07时27分14秒, Key ID 15af5dac6d745a60 Source RPM : mysql-8.0.26-1.module+el8.4.0+652+6de068a7.src.rpm Build Date : 2021年09月22日 星期三 07时06分32秒 Build Host : ord1-prod-x86build005.svc.aws.rockylinux.org Relocations : (not relocatable) Packager : infrastructure@rockylinux.org Vendor : Rocky URL : http://www.mysql.com Summary : The MySQL server and related files Description : MySQL is a multi-user, multi-threaded SQL database server. MySQL is a client/server implementation consisting of a server daemon (mysqld) and many different client programs and libraries. This package contains the MySQL server and some accompanying files and directories. [root@mysql50 ~]# systemctl start mysqld //启动服务 [root@mysql50 ~]# systemctl enable mysqld //开机运行 ``` **步骤二:查看端口号和进程名** 命令操作如下所示: ps -C 【进程名】 //查看单一进程的状态 ss/netstat -anutlp //查看所有进程的信息 ```bash [root@mysql50 ~]# ps -C mysqld //仅查看mysqld进程 PID TTY TIME CMD 21912 ? 00:00:00 mysqld [root@mysql50 ~]# [root@mysql50 ~]# ss -utnlp | grep 3306 查看端口 tcp LISTEN 0 70 *:33060 *:* users:(("mysqld",pid=21912,fd=22)) tcp LISTEN 0 128 *:3306 *:* users:(("mysqld",pid=21912,fd=25)) [root@mysql50 ~]# 或 [root@mysql50 ~]# netstat -utnlp | grep mysqld //仅查看mysqld进程 tcp6 0 0 :::33060 :::* LISTEN 21912/mysqld tcp6 0 0 :::3306 :::* LISTEN 21912/mysq ``` ---- 【说明】 MySQL 8中的3306端口是MySQL服务默认使用的端口,主要用于建立客户端与MySQL服务器之间的连接。 MySQL 8中的33060端口是MySQL Shell默认使用的管理端口,主要用于执行各种数据库管理任务。 远程管理MySQL服务器:使用MySQL Shell连接到MySQL服务,并在远程管理控制台上执行各种数据库管理操作,例如创建、删除、备份和恢复数据库等。 ---- **步骤三:连接服务。** 【说明: 数据库管理员本机登陆默认没有密码 所以直接简写为mysql】 ```bash [root@mysql50 ~]# mysql //连接服务 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.26 Source distribution Copyright (c) 2000, 2021, 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> 登陆后的提示符 mysql> exit //断开连接 Bye [root@mysql50 ~]# ``` **步骤四:配置第2台数据库服务器MySQL51。** ```bash [root@mysql51 ~]# yum -y install mysql-server mysql [root@mysql51 ~]# systemctl start mysqld [root@mysql51 ~]# systemctl enable mysqld [root@mysql51 ~]# mysql mysql> exit Bye [root@mysql51 ~]# ``` **步骤五:练习必备命令的使用(在mysql50主机完成练习)** **省流:** - select version() ; //查看数据库软件版本 - select user() ; //查看登陆的用户和客户端地址 - select database(); // 再次显示所在的库 - use mysql ; //切换到mysql库 - show databases; //查看已有的库 - show tables; //显示库里已有的表 ```bash [root@mysql50 ~]# mysql //连接服务 mysql> select version() ; //查看数据库软件版本 +-----------+ | version() | +-----------+ | 8.0.26 | +-----------+ 1 row in set (0.00 sec) mysql> select user() ; //查看登陆的用户和客户端地址 +----------------+ | user() | +----------------+ | root@localhost | 管理员root本机登陆 +----------------+ 1 row in set (0.00 sec) mysql> show databases; //查看已有的库 +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) mysql> ``` 【说明】 默认4个库 不可以删除,存储的是 服务运行时加载的不同功能的程序和数据。 **information_schema**:是MySQL数据库提供的一个虚拟的数据库,存储了MySQL数据库中的相关信息,比如数据库、表、列、索引、权限、角色等信息。它并不存储实际的数据,而是提供了一些视图和存储过程,用于查询和管理数据库的元数据信息。 **mysql**:存储了MySQL服务器的系统配置、用户、账号和权限信息等。它是MySQL数据库最基本的库,存储了MySQL服务器的核心信息。 **performance_schema**:存储了MySQL数据库的性能指标、事件和统计信息等数据,可以用于性能分析和优化。 **sys**:是MySQL 8.0引入的一个新库,它基于information_schema和performance_schema视图,提供了更方便、更直观的方式来查询和管理MySQL数据库的元数据和性能数据。 ```bash mysql> select database(); //查看当前在那个库里 null 表示没有在任何库里 +------------+ | database() | +------------+ | NULL | +------------+ 1 row in set (0.00 sec) mysql> use mysql ; //切换到mysql库 mysql> select database(); // 再次显示所在的库 +------------+ | database() | +------------+ | mysql | +------------+ 1 row in set (0.00 sec) mysql> show tables; //显示库里已有的表 +------------------------------------------------------+ | Tables_in_mysql | +------------------------------------------------------+ | columns_priv | | component | | db | | default_roles | | engine_cost | | func | | general_log | | global_grants | | gtid_executed | | help_category | | help_keyword | | help_relation | | help_topic | | innodb_index_stats | | innodb_table_stats | | password_history | | plugin | | procs_priv | | proxies_priv | | replication_asynchronous_connection_failover | | replication_asynchronous_connection_failover_managed | | replication_group_configuration_version | | replication_group_member_actions | | role_edges | | server_cost | | servers | | slave_master_info | | slave_relay_log_info | | slave_worker_info | | slow_log | | tables_priv | | time_zone | | time_zone_leap_second | | time_zone_name | | time_zone_transition | | time_zone_transition_type | | user | +------------------------------------------------------+ 37 rows in set (0.00 sec) mysql> exit ; 断开连接 Bye [root@mysql50 ~]# ``` ### 密码管理 #### 目标 1) 在192.168.88.50主机做如下练习: 1. 设置root密码为tarena 2. 修改root密码为123qqq…A 3. 破解root密码为NSD2024…a #### 解析 **root登入** **命令格式: mysql -h数据库服务器ip -P3306 -u用户名 -p密码** **mysqladmin 修改本机数据库密码,明文密码,不安全** **mysqladmin -hlocalhost -P3306 -uroot -p旧密码 password “新密码 ”** //下划线内容表现本机默认,如果是本机执行,可以不写。 --旧密码没有,直接-p #### 步骤 ##### **步骤一:设置root密码为tarena** 设置连接密码 ```bash [root@mysql50 ~]# mysqladmin -uroot -p password "tarena" Enter password: //敲回车(旧密码,没有直接回车) mysqladmin: [Warning] Using a password on the command line interface can be insecure. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety. ``` 无密码连接被拒绝 ```bash [root@mysql50 ~]# mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) ``` 连接时输入密码 ```bash [root@mysql50 ~]# mysql -uroot –ptarena 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 14 Server version: 8.0.26 Source distribution Copyright (c) 2000, 2021, 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> 登陆成功 ``` ##### **步骤二:修改root密码为123qqq…A** 修改密码 mysqladmin -uroot -ptarena password "123qqq...A" ```bash [root@mysql50 ~]# mysqladmin -uroot -ptarena password "123qqq...A" mysqladmin: [Warning] Using a password on the command line interface can be insecure. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety. ``` 旧密码无法登陆 ```bash [root@mysql50 ~]# mysql -uroot –ptarena mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) ``` 新密码登陆 mysql -uroot -p123qqq...A ```bash [root@mysql50 ~]# mysql -uroot -p123qqq...A 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 18 Server version: 8.0.26 Source distribution Copyright (c) 2000, 2021, 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> 登陆成功 ``` ##### **步骤三:破解root密码为NSD2024…a** 说明:在mysql50主机做此练习 **1.修改主配置文件** **添加:skip-grant-tables //手动添加此行 作用登陆时不验证密码** ```bash [root@mysql50 ~]# vim /etc/my.cnf.d/mysql-server.cnf [mysqld] skip-grant-tables //手动添加此行 作用登陆时不验证密码 …… …… :wq [root@mysql50 ~]# systemctl restart mysqld //重启服务 作用让服务以新配置运行 ``` 连接服务 ```bash [root@mysql50 ~]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 8.0.26 Source distribution Copyright (c) 2000, 2021, 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 Mysql> desc mysql.user ; ``` **2.删除不知道的密码** updata 表名 set 修改项=“新值” where 筛选条件 ```bash mysql> update mysql.user set authentication_string="" where user="root" and host="localhost"; ``` 删除后的效果 ```mysql mysql> select host , user , authentication_string from mysql.user where user="root"; +-----------+------+-------------------------------------------+ | host | user | authentication_string | +-----------+------+-------------------------------------------+ | localhost | root | | +-----------+------+-------------------------------------------+ 1 row in set (0.01 sec) mysql> exit; 断开连接 ``` **3.编辑配置文件** ```bash [root@mysql50 ~]# vim /etc/my.cnf.d/mysql-server.cnf [mysqld] #skip-grant-tables //注释添加的行 :wq [root@mysql50 ~]# systemctl restart mysqld //重启服务 作用让注释生效 ``` 无密码登陆 ```bash [root@localhost ~]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.26 Source distribution Copyright (c) 2000, 2021, 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. ``` **4.设置root用户本机登陆密码** 数据库中修改密码: ​ alter user 用户名@数据库地址 identified by “新密码” 刷新权限: flush privileges //刷新后修改密码生效,不用在退出数据库。 ```bash mysql> alter user root@"localhost" identified by "NSD2024...a"; Query OK, 0 rows affected (0.00 sec) mysql> exit 断开连接 ``` 不输密码无法登陆 ```bash [root@localhost ~]# mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) ``` 使用破解的密码登陆 ```bash [root@localhost ~]# mysql -uroot -pNSD2024...a 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 10 Server version: 8.0.26 Source distribution Copyright (c) 2000, 2021, 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> mysql> 登陆成功 mysql> show databases; 查看已有的库 +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.01 sec) ``` ### 筛选条件 #### 3.1 目标 1. 准备练习环境 2. 数值比较 3. 范围匹配 4. 模糊匹配 5. 正则匹配 6. 逻辑比较 7. 字符比较/空/非空 8. 别名/去重/合并 #### 3.2 解析 SQL: 结构化查询语言 **语法规范**: - \c 不执行sql命令 - \G 行输出 - sql命令不区分大小写 **查看命令的语法** ​ 1.查看表里的数据、查看所有行,表头的值 ​ select 表头名 from 库名.表名 ​ 2.仅查看合适筛选条件的行里的表头的值 ​ select 表头名 from 库名.表名 where 筛选条件 **数值比较**: =、>=、<=、<、>、!= **字符比较**: 必须两端都是字符才可以比较 **逻辑比较** ==and优先级大于or== #### 3.3 步骤 实现此案例需要按照如下步骤进行。 拷贝tarena.sql文件到mysql50主机里,然后使用tarena.sql创建练习使用的数据。 ---- ##### 步骤一:环境搭建 //拷贝tarena.sql 拷贝到 mysql50主机的/root 下 [openeuler@server1 ~]$ scp /linux-soft/s3/tarena.sql root@192.168.88.50:/root/ root@192.168.88.50's password: tarena.sql 100% 284KB 171.9MB/s 00:00 //连接mysql50主机 ```bash [openeuler@server1 ~]$ ssh root@192.168.88.50 root@192.168.88.50's password: Last login: Tue May 23 10:59:57 2023 from 192.168.88.254 ``` //恢复数据 ```bash [root@mysql50 ~]# mysql -uroot -pNSD2024...a < /root/tarena.sql mysql: [Warning] Using a password on the command line interface can be insecure. ``` //连接服务 ```bash [root@mysql50 ~]# mysql -uroot -pNSD2024...a mysql> show databases; //查看库 +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | tarena | 恢复的库 +--------------------+ 5 rows in set (0.00 sec) mysql> use tarena; //进入库 Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; //查看表 +------------------+ | Tables_in_tarena | +------------------+ | departments | 部门表 | employees | 员工表 | salary | 工资表 | user | 用户表 +------------------+ 4 rows in set (0.00 sec) ``` 使用user 表做查询练习 user表里存储的是 系统用户信息( 就是 /etc/passwd 文件的内容) ```bash mysql> desc tarena.user; //查看表头 +----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment |行号 | name | char(20) | YES | | NULL | |用户名 | password | char(1) | YES | | NULL | |密码占位符 | uid | int(11) | YES | | NULL | | uid号 | gid | int(11) | YES | | NULL | | gid号 | comment | varchar(50) | YES | | NULL | | 描述信息 | homedir | varchar(80) | YES | | NULL | | 家目录 | shell | char(30) | YES | | NULL | | 解释器 +----------+-------------+------+-----+---------+----------------+ 8 rows in set (0.00 sec) ``` select命令格式演示 **语法格式1 SELECT 字段列表 FROM 库名.表名;** **语法格式2 SELECT 字段列表 FROM 库名.表名 where 筛选条件;** ```bash mysql> select name from tarena.user; //查看一个表头 mysql> select name ,uid from tarena.user; //查看多个表头 mysql> select * from tarena.user; //查看所有表头 ``` 加筛选条件 ```bash mysql> select * from tarena.user where name = “root”; //查找root用户信息 +----+------+----------+------+------+---------+---------+-----------+ | id | name | password | uid | gid | comment | homedir | shell | +----+------+----------+------+------+---------+---------+-----------+ | 1 | root | x | 0 | 0 | root | /root | /bin/bash | +----+------+----------+------+------+---------+---------+-----------+ 1 row in set (0.00 sec) mysql> mysql> select * from tarena.user where id = 2 ; //查找第2行用户信息 +----+------+----------+------+------+---------+---------+--------------+ | id | name | password | uid | gid | comment | homedir | shell | +----+------+----------+------+------+---------+---------+--------------+ | 2 | bin | x | 1 | 1 | bin | /bin | /sbin/nologin | +----+------+----------+------+------+---------+---------+--------------+ 1 row in set (0.00 sec) ``` ##### **步骤二:数值比较** 比较符号: = != > >= < <= 相等 不相等 大于 大于等于 小于 小于等于 符号两边要是数字或数值类型的表头 符号左边与符号右边做比较 ---- ```bash //查看第3行的行号、用户名、uid、gid 四个表头的值 mysql> select id,name,uid,gid from tarena.user where id = 3; +----+--------+------+------+ | id | name | uid | gid | +----+--------+------+------+ | 3 | daemon | 2 | 2 | +----+--------+------+------+ 1 row in set (0.00 sec) //查看前2行的行号用户名、uid、gid 四个表头的值 mysql> select id,name,uid,gid from tarena.user where id < 3; +----+------+------+------+ | id | name | uid | gid | +----+------+------+------+ | 1 | root | 0 | 0 | | 2 | bin | 1 | 1 | +----+------+------+------+ 2 rows in set (0.00 sec) //查看前3行的行号、用户名、uid、gid 四个表头的值 mysql> select id,name,uid,gid from tarena.user where id <= 3; +----+--------+------+------+ | id | name | uid | gid | +----+--------+------+------+ | 1 | root | 0 | 0 | | 2 | bin | 1 | 1 | | 3 | daemon | 2 | 2 | +----+--------+------+------+ 3 rows in set (0.00 sec) //查看前uid号大于6000的行号、用户名、uid、gid 四个表头的值 mysql> select id,name,uid,gid from tarena.user where uid > 6000; +----+-----------+-------+-------+ | id | name | uid | gid | +----+-----------+-------+-------+ | 22 | nfsnobody | 65534 | 65534 | +----+-----------+-------+-------+ 1 row in set (0.00 sec) //查看前uid号大于等于1000的行号、用户名、uid、gid 四个表头的值 mysql> select id,name,uid,gid from tarena.user where uid >= 1000; +----+-----------+-------+-------+ | id | name | uid | gid | +----+-----------+-------+-------+ | 22 | nfsnobody | 65534 | 65534 | | 24 | plj | 1000 | 1000 | +----+-----------+-------+-------+ 2 rows in set (0.00 sec) //查看uid号和gid号相同的行 仅显示行号、用户名、uid、gid 四个表头的值 mysql> select id,name,uid,gid from tarena.user where uid = gid; +----+-----------------+-------+-------+ | id | name | uid | gid | +----+-----------------+-------+-------+ | 1 | root | 0 | 0 | | 2 | bin | 1 | 1 | | 3 | daemon | 2 | 2 | | 13 | nobody | 99 | 99 | | 14 | systemd-network | 192 | 192 | | 15 | dbus | 81 | 81 | | 17 | sshd | 74 | 74 | | 18 | postfix | 89 | 89 | | 20 | rpc | 32 | 32 | | 21 | rpcuser | 29 | 29 | | 22 | nfsnobody | 65534 | 65534 | | 23 | haproxy | 188 | 188 | | 24 | plj | 1000 | 1000 | | 25 | apache | 48 | 48 | | 26 | mysql | 27 | 27 | +----+-----------------+-------+-------+ 15 rows in set (0.00 sec) //查看uid号和gid号不一样的行 仅显示行号、用户名、uid、gid 四个表头的值 mysql> select id,name,uid,gid from tarena.user where uid != gid; +----+----------+------+------+ | id | name | uid | gid | +----+----------+------+------+ | 4 | adm | 3 | 4 | | 5 | lp | 4 | 7 | | 6 | sync | 5 | 0 | | 7 | shutdown | 6 | 0 | | 8 | halt | 7 | 0 | | 9 | mail | 8 | 12 | | 10 | operator | 11 | 0 | | 11 | games | 12 | 100 | | 12 | ftp | 14 | 50 | | 16 | polkitd | 999 | 998 | | 19 | chrony | 998 | 996 | +----+----------+------+------+ 11 rows in set (0.00 sec) mysql> ``` **字符比较:符号两边必须是字符 或字符类型的表头** **= 相等比较** **!= 不相等比较。** 查看表里是否有名字叫apache的用户 ```bash mysql> select name from tarena.user where name="apache" ; +--------+ | name | +--------+ | apache | +--------+ 1 row in set (0.00 sec) ``` 输出解释器不是/bin/bash的用户名 及使用的解释器 ```bash mysql> select name , shell from tarena.user where shell != "/bin/bash"; +-----------------+----------------+ | name | shell | +-----------------+----------------+ | bin | /sbin/nologin | | daemon | /sbin/nologin | | adm | /sbin/nologin | | lp | /sbin/nologin | | sync | /bin/sync | | shutdown | /sbin/shutdown | | halt | /sbin/halt | | mail | /sbin/nologin | | operator | /sbin/nologin | | games | /sbin/nologin | | ftp | /sbin/nologin | | nobody | /sbin/nologin | | systemd-network | /sbin/nologin | | dbus | /sbin/nologin | | polkitd | /sbin/nologin | | sshd | /sbin/nologin | | postfix | /sbin/nologin | | chrony | /sbin/nologin | | rpc | /sbin/nologin | | rpcuser | /sbin/nologin | | nfsnobody | /sbin/nologin | | haproxy | /sbin/nologin | | apache | /sbin/nologin | | mysql | /bin/false | +-----------------+----------------+ 24 rows in set (0.00 sec) mysql> ``` ##### **步骤三:范围匹配** **in (值列表) //在…里** **not in (值列表) //不在…里** **between 数字1 and 数字2 //在…之间,数字1<数字2** 命令操作如下所示: ```bash //uid号表头的值 是 (1 , 3 , 5 , 7) 中的任意一个即可 mysql> select name , uid from tarena.user where uid in (1 , 3 , 5 , 7); +------+------+ | name | uid | +------+------+ | bin | 1 | | adm | 3 | | sync | 5 | | halt | 7 | +------+------+ //shell 表头的的值 不是 "/bin/bash"或"/sbin/nologin" 即可 mysql> select name , shell from tarena.user where shell not in ("/bin/bash","/sbin/nologin"); +----------+----------------+ | name | shell | +----------+----------------+ | sync | /bin/sync | | shutdown | /sbin/shutdown | | halt | /sbin/halt | | mysql | /bin/false | +----------+----------------+ //id表头的值 在 10 到 20 之间即可 包括 10 和 20 本身 mysql> select id , name , uid from tarena.user where id between 10 and 20 ; +----+-----------------+------+ | id | name | uid | +----+-----------------+------+ | 10 | operator | 11 | | 11 | games | 12 | | 12 | ftp | 14 | | 13 | nobody | 99 | | 14 | systemd-network | 192 | | 15 | dbus | 81 | | 16 | polkitd | 999 | | 17 | sshd | 74 | | 18 | postfix | 89 | | 19 | chrony | 998 | | 20 | rpc | 32 | +----+-----------------+------+ 11 rows in set (0.00 sec)mysql> ``` ##### **步骤四:模糊匹配** **where 字段名 like "表达式";** 通配符 _ **表示 1个字符** **% 表示零个或多个字符** 命令操作如下所示: ```bash //找名字必须是3个字符的 (没有空格挨着敲) mysql> select name from tarena.user where name like "___"; +------+ | name | +------+ | bin | | adm | | ftp | | rpc | | plj | | bob | +------+ 6 rows in set (0.00 sec) //找名字必须是4个字符的(没有空格挨着敲) mysql> select name from tarena.user where name like "_ _ _ _"; +------+ | name | +------+ | root | | sync | | halt | | mail | | dbus | | sshd | | null | +------+ 7 rows in set (0.00 sec) //找名字以字母a开头的(没有空格挨着敲) mysql> select name from tarena.user where name like "a%"; //查找名字至少是4个字符的表达式 mysql> select name from tarena.user where name like "%_ _ _ _%";(没有空格挨着敲) mysql> select name from tarena.user where name like "_ _%_ _";(没有空格挨着敲) mysql> select name from tarena.user where name like "_ _ _ _%";(没有空格挨着敲) ``` ##### **步骤五:正则匹配** **格式:select 字段名列表 from 库名.表名 where字段名 regexp '正则表达式';** ---- **(正则符号)** **^ 匹配行首** **$ 匹配行尾** **[] 匹配范围内任意一个** **\* 前边的表达式出现零次或多次** **| 或者** **. 任意一个字符** ---- **插入:** 1.直接插入完整的一行 INSERT INTO table_name VALUES (value1,value2,value3,...); 2.指定列名及被插入的值 INSERT INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3,...); **正则表达式筛选** select 表头... from 表名 where 表头 regexp “正则表达式” ---- ```bash //添加有数字的名字 insert into tarena.user(name)values("yaya9"); insert into tarena.user(name)values("6yaya"); insert into tarena.user(name)values("ya7ya"); insert into tarena.user(name)values("yay8a"); //查看名字里有数字的 mysql> select name from tarena.user where name regexp "[0-9]"; +-------+ | name | +-------+ | yaya9 | | 6yaya | | ya7ya | | yay8a | +-------+ 4 rows in set (0.00 sec) //查看名字以数字开头 mysql> select name from tarena.user where name regexp "^[0-9]"; +-------+ | name | +-------+ | 6yaya | +-------+ 1 row in set (0.00 sec) //查看名字以数字结尾 mysql> select name from tarena.user where name regexp "[0-9]$"; +-------+ | name | +-------+ | yaya9 | +-------+ 1 row in set (0.00 sec) mysql> //查看名字以r开头 mysql> select name from tarena.user where name regexp "^r"; +---------+ | name | +---------+ | root | | rpc | | rpcuser | +---------+ 3 rows in set (0.00 sec) //查看名字以t结尾 mysql> select name from tarena.user where name regexp "t$"; +------+ | name | +------+ | root | | halt | +------+ 2 rows in set (0.00 sec) mysql> //查看名字以r开头或t结尾 mysql> select name from tarena.user where name regexp "^r|t$"; +---------+ | name | +---------+ | root | | halt | | rpc | | rpcuser | +---------+ 4 rows in set (0.00 sec) //名字r开头t结尾 mysql> select name from tarena.user where name regexp "^r.*t$"; +------+ | name | +------+ | root | +------+ 1 row in set (0.00 sec) mysql> ``` ##### **步骤六:逻辑比较** 多个判断条件 逻辑与 and (&&) 多个判断条件必须同时成立 逻辑或 or (||) 多个判断条件其中某个条件成立即可 逻辑非 not (!) 取反 ---- **命令操作如下所示:** ```bash //逻辑非例子,查看解释器不是/bin/bash 的 mysql> select name,shell from tarena.user where shell != "/bin/bash"; //not 也是取反 要放在表达式的前边 mysql> select name,shell from tarena.user where not shell = "/bin/bash"; //id值不在 10 到 20 之间 mysql> select id , name from tarena.user where not id between 10 and 20 ; //逻辑与 例子 mysql> select name , uid from tarena.user where name="root" and uid = 1; Empty set (0.00 sec) mysql> select name , uid from tarena.user where name="root" and uid = 0; +------+------+ | name | uid | +------+------+ | root | 0 | +------+------+ 1 row in set (0.00 sec) //逻辑或 例子 mysql> select name , uid from tarena.user where name = "root" or name = "bin" or uid = 1; +------+------+ | name | uid | +------+------+ | root | 0 | | bin | 1 | +------+------+ mysql> ``` **() 提高优先级** ```bash mysql> select 2 + 3 * 5 ; //使用默认计算顺序 先乘除后加减 +------------+ | 2 + 3 * 5 | +------------+ | 17 | +------------+ 1 row in set (0.00 sec) mysql> select (2 + 3 ) * 5 ; //先加法再乘法 +---------------+ | (2 + 3 ) * 5 | +---------------+ | 25 | +---------------+ 1 row in set (0.00 sec) mysql> ``` **逻辑匹配什么时候需要加()** ==**逻辑与and 优先级高于逻辑或 or**== **如果在筛选条件里既有and 又有 or 默认先判断and 再判断or** ```bash //没加() 的查询结果 select name , uid from tarena.user where name = "root" or name = "bin" and uid = 1 ; +------+------+ | name | uid | +------+------+ | root | 0 | | bin | 1 | +------+------+ 2 rows in set (0.00 sec) //加()的查询结果 select name , uid from tarena.user where (name = "root" or name = "bin") and uid = 1 ; +------+------+ | name | uid | +------+------+ | bin | 1 | +------+------+ 1 row in set (0.00 sec) mysql> ``` ##### **步骤七:空/非空** 空 : is null 表头下没有数据 非空: is not null 表头下有数据 mysql服务 使用关键字 null 或 NULL 表示表头没有数据 ---- 添加新行, 仅给行中的id 表头和name表头赋值 ```mysql mysql> insert into tarena.user(id,name) values(71,""); //零个字符 mysql> insert into tarena.user(id,name) values(72,"null");//普通字母 mysql> insert into tarena.user(id,name) values(73,NULL); //表示空,没有储存数据 mysql> insert into tarena.user(id,name) values(74,null); //表示空,没有储存数据 ``` 查看id表头值大于等于70 的行 仅显示行中 id表头 和 name 表头的值 ```mysql mysql> select id , name from tarena.user where id >= 71; +----+------+ | id | name | +----+------+ | 71 | | | 72 | null | | 73 | NULL | | 74 | NULL | +----+------+ ``` 查看name 表头没有数据的行 仅显示行中id表头 和 name 表头的值 ```bash mysql> select id , name from tarena.user where name is null; +----+------+ | id | name | +----+------+ | 28 | NULL | | 29 | NULL | | 73 | NULL | | 74 | NULL | +----+------+ ``` 查看name 表头是0个字符的行, 仅显示行中id表头 和 name 表头的值 ```mysql mysql> select id , name from tarena.user where name=""; +----+------+ | id | name | +----+------+ | 71 | | +----+------+ 1 row in set (0.00 sec) ``` 查看name 表头值是null的行, 仅显示行中id表头 和 name 表头的值 ```mysql mysql> select id , name from tarena.user where name="null"; +----+------+ | id | name | +----+------+ | 72 | null | +----+------+ 1 row in set (0.00 sec) ``` 查看name 表头有数据的行, 仅显示行中id表头 和 name 表头的值 ```mysql mysql> select id , name from tarena.user where name is not null; +----+-----------------+ | id | name | +----+-----------------+ | 1 | root | | 2 | bin | | 3 | daemon | | 4 | adm | | 5 | lp | .... .... | 27 | bob | | 71 | | | 72 | null | +----+-----------------+ ``` **步骤八、别名/去重/合并** **定义别名使用 as 或 空格 ** ```mysql mysql> select name , homedir from tarena.user; mysql> select name as 用户名 , homedir 家目录 from tarena.user; ``` **拼接 concat()** ```mysql mysql> select concat(name,"-",uid) as 用户信息 from tarena.user where uid <= 5; +--------------+ | 用户信息 | +--------------+ | root-0 | | bin-1 | | daemon-2 | | adm-3 | | lp-4 | | sync-5 | +--------------+ 6 rows in set (0.00 sec) //拼接 mysql> select concat(name , "-" , uid) as 用户信息 from tarena.user where uid <= 5; ``` **多列拼接** ```mysql mysql> select concat(name , "-" , uid , "-" , gid) as 用户信息 from tarena.user where uid <= 5; +--------------+ | 用户信息 | +--------------+ | root-0-0 | | bin-1-1 | | daemon-2-2 | | adm-3-4 | | lp-4-7 | | sync-5-0 | +--------------+ ``` **去重显示 distinct 字段名列表** distinct只能是单列去重,不能多个表头放在一起 ---- ```mysql //去重前输出 mysql> select shell from tarena.user where shell in ("/bin/bash","/sbin/nologin") ; +---------------+ | shell | +---------------+ | /bin/bash | | /sbin/nologin | | /sbin/nologin | | /sbin/nologin | | /sbin/nologin | | /sbin/nologin | | /sbin/nologin | | /sbin/nologin | | /sbin/nologin | | /sbin/nologin | | /sbin/nologin | | /sbin/nologin | | /sbin/nologin | | /sbin/nologin | | /sbin/nologin | | /sbin/nologin | | /sbin/nologin | | /sbin/nologin | | /sbin/nologin | | /sbin/nologin | | /bin/bash | | /sbin/nologin | +---------------+ 22 rows in set (0.00 sec) //去重后查看 mysql> select distinct shell from tarena.user where shell in ("/bin/bash","/sbin/nologin") ; +---------------+ | shell | +---------------+ | /bin/bash | | /sbin/nologin | +---------------+ 2 rows in set (0.01 sec) mysql> ``` ### 安装图形软件 #### 4.1 目标 - 在IP地址192.168.88.50主机安装phpmyadmin软件 - 客户端通过访问phpmyadmin软件管理数据库 #### 4.2 方案 把用到的软件拷贝的虚拟机mysql50里 在mysql50主机,首先配置运行环境LNP,然后安装phpmyadmin软件,最后打开真机的浏览器输入phpmyadmin的网址访问。 #### 4.3 步骤 ##### 步骤一:准备运行环境 命令操作如下所示: ```bash gcc 源码包编译工具 unzip 提供解压.zip 压缩包命令 make 源码软件编译命令 pcre-devel 支持正则表达式 zlib-devel 提供数据压缩命令 [root@mysql50 ~]# yum -y install gcc unzip make pcre-devel zlib-devel //安装依赖 [root@mysql50 ~]# tar -xf nginx-1.22.1.tar.gz //解压源码 [root@mysql50 ~]# cd nginx-1.22.1 //进源码目录 [root@mysql50 nginx-1.22.1]# ./configure //配置 [root@mysql50 nginx-1.22.1]# make && make install //编译并安装 [root@mysql50 nginx-1.22.1]# ls /usr/local/nginx/ //查看安装目录 conf html logs sbin //修改主配置文件 [root@mysql50 nginx-1.22.1]# vim /usr/local/nginx/conf/nginx.conf 43 location / { 44 root html; 45 index index.php index.html index.htm; //添加首页名 46 } 65 location ~ \.php$ { 66 root html; 67 fastcgi_pass 127.0.0.1:9000; //访问.php的请求转给本机的9000端口 68 fastcgi_index index.php; 69 #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 70 include fastcgi.conf; //保存nginx变量文件 71 } :wq [root@mysql50 nginx-1.22.1]# /usr/local/nginx/sbin/nginx //启动服务 [root@mysql50 nginx-1.22.1]# netstat -utnlp | grep 80 //查看端口 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 42182/nginx: master [root@mysql50 nginx-1.22.1]# ``` 软件功能说明 ```basic php 解释php代码 php-devel php扩展包 php-mysqlnd 连接mysql命令包 php-json 支持json代码 php-fpm 提供fpm服务 ``` 安装软件 ```bash [root@mysql50 ~]# yum -y install php php-devel php-mysqlnd php-json php-fpm ``` 修改主配置文件 ```bash [root@mysql50 ~]# vim +38 /etc/php-fpm.d/www.conf listen = 127.0.0.1:9000 :wq [root@mysql50 ~]# systemctl start php-fpm //启动服务 ``` 查看端口 ```bash [root@mysql50 ~]# netstat -utnlp | grep 9000 tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 67251/php-fpm: mast [root@mysql50 ~]# ``` 编写php脚本 ```bash [root@mysql50 ~]# vim /usr/local/nginx/html/test.php ``` 访问脚本 ```bash [root@mysql50 ~]# curl http://localhost/test.php plj [root@mysql50 ~]# ``` ##### 步骤二:安装phpmyadmin软件 解压软件 ```bash [root@mysql50 ~]# which unzip || yum -y install unzip [root@mysql50 ~]# unzip phpMyAdmin-5.2.1-all-languages.zip //解压 ``` 移动并改名 ```bash [root@mysql50 ~]# mv phpMyAdmin-5.2.1-all-languages /usr/local/nginx/html/phpmyadmin ``` 创建主配置文件 ```bash [root@mysql50 ~]# cd /usr/local/nginx/html/phpmyadmin/ [root@mysql50 phpmyadmin]# cp config.sample.inc.php config.inc.php ``` ##### 步骤三:客户端访问 打开浏览器输入此网址 效果如图-1所示 ```bash http://192.168.88.50/phpmyadmin ``` ![image-20240708085958522](https://gitee.com/xiaoxinbupa/linux-note/raw/master/linux_base_picture/image-20240708085958522.png) 说明:输入数据库管理员root 和 密码 成功后如图-2所示 ![image-20240708090028156](https://gitee.com/xiaoxinbupa/linux-note/raw/master/linux_base_picture/image-20240708090028156.png) 在数据库服务器里创建普通用户 ```bash 创建库 mysql> create database gamedb; Query OK, 1 row affected (0.01 sec) 创建用户 mysql> create user plj@"localhost" identified by "123456"; Query OK, 0 rows affected (0.01 sec) 授权权限 mysql> grant all on gamedb.* to plj@"localhost" ; Query OK, 0 rows affected (0.00 sec) mysql> ``` 客户端使用普通用户登陆,如图-3、图-4所示 ![image-20240708090051290](https://gitee.com/xiaoxinbupa/linux-note/raw/master/linux_base_picture/image-20240708090115601.png) ![image-20240708090140163](https://gitee.com/xiaoxinbupa/linux-note/raw/master/linux_base_picture/image-20240708090140163.png) ### mysql常用函数 **函数**:MySQL服务软件内置的命令,函数名( 传参...) user()、 version()、database()... **函数的用法**: 1. 可以单独使用 2. 可以嵌套使用函数,函数调用函数 3. 处理表里的数据 **常用函数的使用**: 使用函数对表里的数据做处理 1. **字符函数** 对字符做处理的命令(表头下储存的是拼音 ,英文字母,汉字) ​ **length(str)** 返回字符串长度,以字节为单位 //一个汉字3个字节 ​ **char_length(str)** 返回字符串长度,以字符为单位 //一个汉字算一个字符 ​ **upper(str)和ucase(str)** 将字符串中的字母全部转换成大写 ​ **lower(str)和lcase(str)** 将str中的字母全部转换成小写 ​ **substr(s, start,end)** 从s的start位置开始取出到end长度的子串 ​ **instr(str,str1)** 返回str1参数,在str参数内的位置 ​ **trim(s)** 返回字符串s删除了两边空格之后的字符串S 2. **数学函数** ​ **abs(x)** 返回x的绝对值 ​ **pi()** 返回圆周率π,默认显示6位小数 ​ **mod(x,y)** 返回x被y除后的余数 ​ **ceil(x)、ceiling(x)** 返回不小于x的最小整数 (x 是小数) ​ **floor(x)** 返回不大于x的最大整数 (x 是有小数的数字) ​ **power(x,y)、pow(x,y)** 返回x的y次方 3. **日期函数** ​ ![image-20240709152502771](https://gitee.com/xiaoxinbupa/linux-note/raw/master/linux_base_picture/image-20240709152502771.png) 4. **聚集函数** 对表头下的数据做统计(单独使用or分组一起使用) ​ **sum(表头名)** 求和 ​ **avg(表头名)** 计算平均值 ​ **min(表头名)** 获取最小值 ​ **max(表头名)** 获取最大值 **count(表头名)** 统计表头值个数 5. **数学计算** ![image-20240709155311190](https://gitee.com/xiaoxinbupa/linux-note/raw/master/linux_base_picture/image-20240709155311190.png) 6. **if函数** **if ( 条件 , v1 , v2 )** 如果条件是TRUE则返回v1,否则返回v2 **ifnull ( v1,v2 )** 如果v1不为NULL,则返回v1,否则返回v2 7. **case函数** ```mysql CASE 表头名 WHEN 值1 THEN 输出结果 WHEN 值2 THEN 输出结果 WHEN 值3 THEN 输出结果 ... ELSE 输出结果 END ``` 或 ```mysql CASE WHEN 判断条件1 THEN 输出结果 WHEN 判断条件2 THEN 输出结果 WHEN 判断条件3 THEN 输出结果 ... ELSE 输出结果 END ``` ### 查询结果处理 对select命令查找的数据==再做处理== SELECT 表头名 FROM 库名.表名 [WHERE条件] 分组 | 排序 | 过滤 | 分页; **1、排序(order by):** ​ select 表头 ...from 表名 where 条件 order by 表头1 asc/desc; ​ -- asc 升序 、 desc 降序 (以表头1作为排序) ---- **2、分组(group by)**:按照指定的列分组,相同的数据分为一组,组名只显示一次 ​ select 表头 ...from 表名 where 条件 group by 表头名; ​ -- 按照什么表头分组,就查什么表头的数据 ​ --group by 可以和聚集函数一起使用avg,sum,count ... 联用是聚合函数使用的参数是组里面的参数 ​ --group by 不能和单个表头连用--简单一点就是前面筛选的数量不匹配,不一致 ---- **3、过滤(having)**: 在已经查找到的数据里再找符合条件的数据 ​ select 表头... from 表名 where 条件 having 筛选条件 ​ --- having使用的筛选的表头是查找之后的表头 ---- **4、分页(limit):** 限制select查询结果显示行数(默认显示全部查询结果) [减轻输出的压力]: ​ select 表头... from 表名 where 条件 limit n //显示前n行select 表头... from 表名 where 条件 limit n ,m //从第n+1行开始,显示m行 ​ --limit n,m 中n表示起始行(从0计数) ---- ### 管理表记录crud **对表里的行做管理** ​ 表中的行,被称为记录 ​ 行中的列,被称为字段 ​ desc 表名 //查看表中所有字段信息 ---- **增(insert into)**: ​ 1.**仅仅改行中的某些字段赋值** ​ insert into 表名(字段1,字段2...) values (值1,值2...) //其余字段为null ​ insert into 表名(字段1,字段2...) values (值1,值2...), (值1,值2...), (值1,值2...), ... //一次性插入多行 ​ --插入的字段与值的数据类型要匹配 2.**给行中所有字段赋值** ​ insert into 表名 values(值1,值2,......,值n) // n=字段数量,列数量 ---- **删(delete from)**: 1. **仅删除与条件匹配的行** delete from 库名.表名 where 条件 2. **清空表记录** ​ delete from 库名.表名 3. **清空表记录(截断)** truncate table 库名.表名 ---truncate table 不支持where条件;自增长列,truncate 后从1开始、delete继续编号 ,truncate不能回滚,delete可以 ,truncate效率高于delete。 ---- **改(update)**: 1. **批量修改** ​ update 库名.表名 set 字段1=值1 ,字段2=值2...; 2. **仅修改与条件匹配的** update 库名.表名 set 字段1=值1,字段2=值2 ...where 条件; ---- ### 内连接查询 **类似于交集** select 表名.表头 from 表名 1 inner join 表名 2 on 连接条件 where|order by | group by | limit | having ; --输出的表头在连接的表里唯一是,表名可以不写 --两个表的相连接形成的是笛卡尔积 **等值连接:** 表1与表2有相同的字段,使用相等判断做连接条件 select 表名.表头 from 表名 1 inner join 表名 2 on ==表1.字段1=表2.字段1== where|order by | group by | limit | having ; **非等值连接:** 表1与表2没有相同的字段,不使用相等判断做连接条件 select 表名.表头 from 表名 1 inner join 表名 2 on ==比较连接条件== where|order by | group by | limit | having ; **多表连接:** 把前两张表连上,再inner join 第三张表 on 连接条件 ---- ### 外连接查询 **类似于差集** **外连接查询作用**: ==比较2个表里数据的不同== ---- **左连接**: 用左表的数据和右表的数据做对比 1. select 表头名1 from 表名1 left join 表名2 on 连接条件; 2. select 表头名1 from 表名1 left join 表名2 on 连接条件 [ where | group by | order by | having | limit ] ; **左连接的输出的结果**: 左表表头数据全输出,右边仅显示与连接条件匹配的行,若右表输出的行比左表少,使用null补全少的行,输出显示。 ---- **右连接**:用右表的数据和左表的数据做对比 1. select 表头名1 from 表名1 right join 表名2 on 连接条件; 2. select 表头名1 from 表名1 right join 表名2 on 连接条件 [ where | group by | order by | having | limit ] ; **右连接的输出的结果**: 右表表头数据全输出,左边仅显示与连接条件匹配的行,若左表输出的行比右表少,使用null补全少的行,输出显示。 ---- ### 全连接查询 **类似于合集** **作用**:把多个select命令 查询的行一起输出 ==多个select 查询,查看的表头个数要一致== 1. **去除重复的行输出** (select 查询语句) union (select 查询语句) 2. **不去除重复的行输出** (select 查询语句) union all (select 查询语句) ### 嵌套查询 select 查询命令里包含select查询命令 ,包含select查询命令放到()里。 包含select查询命令可以存放在: 1. **where命令之后----把(select查询语句)作为查询条件使用** ​ //在select查询出来的结果为多值,使用 in 而不要使用 = (等号只匹配单个值) ​ select 表头名 from 库. 表 where 表头名 判断符号 (select查询语句) ---- 2. **having命令之后----把(select查询语句)作为筛选条件使用** ---- 3. **from命令之后 ----把(select查询语句)作为表使用** ---- 4. **select命令之后-----把()里的查询结果作为表头** ### 表管理 1. #### **创建数据库** ​ create database 库名 create database if not exists 库名 //加if not exists 命令避免重名报错 ​ **库名命名规则**:仅可以使用数字、字母、下划线、不能纯数字,区分字母大小写,具有唯一性,不可使用MySQL命令或特殊字符 2. #### **删除数据库** ​ drop database 库名; //删除库, 删除没有的库报错 ​ drop database if exists 库名; //删除库,加if exists 删除没有的库,也不报错 3. #### **创建表** create table 库名.表名 4. #### **查看表和表头信息** desc 表名 //查看表头的所有字段的信息 5. #### **删除表** drop table 库名.表名 //删除该表 drop table if exists 表名 6. #### **修改表头** alter table 表名 add | drop | modify | rename |change ---- ##### **6.1. alter add添加表头** ​ alter table 表名 add 表头名 数据类型 first //first 把表头添加首位 ​ alter table 表名 add 表头名 数据类型 after 已有表头名1 //after 添加在指定表头名的下方 ---- ##### **6.2 alter drop 删除表头** ​ alter tables 库名.表名 drop 表头; //删除当个表头 ​ alter tables 库名.表名 drop 表头1 ,drop 表头2,.... ; //删除多个表头 ---- ##### **6.3 alter modify 修改表头的位置和修改数据类型** ​ alter table 库名.表名 modify 表头1 表头1的数据类型 after 表头2 ; //将表头1插入到表头2后面 ​ alter table 库名.表名 modify 表头1 表头1的数据类型 first ; //将表头1插入首位 ---- ##### **6.4 alter change 修改表头名** ​ alter table 库名.表名 change 表头1 新表头1名 表头1的数据类型 ; ---- ##### **6.5 alter rename 修改表名** alter table 库名.表名 1 rename 新表名 //修改表名 ---- 7. #### **复制表(拷贝已有的表 和系统命令 cp 的功能一样 )** create table 库名.表名 select * from 库名1.表名1 //复制表名1的所有内容,包括表结构。==但是不包括key== create table 库名.表名 like 库名1.表名1 //仅复制表头,==源表的key 会被复制== ---- ### 数据类型 1. **整型类型**: 仅储存数值的整数类型 ![image-20240711215858774](https://gitee.com/xiaoxinbupa/linux-note/raw/master/linux_base_picture/image-20240711215858774.png) 2. **浮点类型**: 储存有小数点的数 ![image-20240711215945756](https://gitee.com/xiaoxinbupa/linux-note/raw/master/linux_base_picture/image-20240711215945756.png) 3. **枚举类型** ​ 3.1. **单选枚举(enum)** ![image-20240711220108763](https://gitee.com/xiaoxinbupa/linux-note/raw/master/linux_base_picture/image-20240711220108763.png) ![image-20240711220125466](https://gitee.com/xiaoxinbupa/linux-note/raw/master/linux_base_picture/image-20240711220125466.png) ​ 3.2. **多选枚举(set)** ![image-20240711220225786](https://gitee.com/xiaoxinbupa/linux-note/raw/master/linux_base_picture/image-20240711220225786.png) ![image-20240711220239827](https://gitee.com/xiaoxinbupa/linux-note/raw/master/linux_base_picture/image-20240711220239827.png) 4. **日期类型**: 储存如生日、注册时间/出生年份... ![image-20240711220416063](https://gitee.com/xiaoxinbupa/linux-note/raw/master/linux_base_picture/image-20240711220416063.png) 5. **字符类型** 1. 定长字符类型 char 字符个数 255 当定义变量值小于定长,会自动补齐 2. 变长字符类型 varchar 字符个数65535 (实际65532) ### 批量处理 **检索目录**: 导入或导出操作时,存放数据的文件必须保存在mysql服务要求的目录下 ​ **1. 查看默认的检索目录** ​ show variables; 查看所有的默认配置项 ​ show variables like "%关键字%"; 查看与某类配置相关的配置项 ​ show variables like “配置项名字”; 仅查看某一配置项的值 ​ **show** variables **like** "secure_file_priv"; 查看mysql的数据存放的检索目录 ![image-20240712103036867](https://gitee.com/xiaoxinbupa/linux-note/raw/master/linux_base_picture/image-20240712103036867.png) ​ **2.自定义检索目录** 批量导入: 一次把文件的所有内容存放到数据库的表里 1. 创建储存数据的库和表 2. 把存放数据的的文件拷贝到检索目录下 3. 执行导入数据的命令 ​ load data infile "/目录名/文件名" into table 库名.表名 fields terminated by "分隔符" lines terminated by "\n"; ​ 可以在数据库中使用system linux语句 来对服务器操作,方便数据文件的变更 4. 查看数据 批量导出 ​ select 查询命令 into outfile “/检索目录/文件名” > [defined: fields terminated by "列的间隔符是空格"]\> [defined: lines terminated by "行的间隔符是\n"] ; ### 表头的基本约束 设置在表头上,控制如何给表头赋值 基本约束: 不允许赋null值 not null 、设置默认值 不给表头赋值时,储存的值 default 值、唯一索引,表头的值必须唯一 unique ### 表头的高级约束 **主键 primary** **使用规则** 1. ==表头值不允许重复,不允许赋NULL值== 2. 一个表中只能有一个primary key 表头 3. 多个表头做主键,称为复合主键,必须一起创建和删除 4. 主键标志PRI 5. 主键通常与auto_increment连用 6. 通常把表中唯一标识记录的表头设置为主键[行号表] **查看主键**: desc 库.表 **删除主键**:alter table 库.表 drop primary key; **添加主键**: alter table 库.表 add primary key(表头名); **验证主键的约束**: 主键的值唯一而且不允许赋值为空 ---- **复合主键**: 多个表头一起做主键 ​ 约束: 复合主键表头的值不同时重复即可 ​ **删除复合主键** : alter table 库.表 drop primary key; ​ **添加复合主键**: alter table 库.表 add primary key (表头名1,表头名2,...) ---- **主键与auto_increment连接** ​ 自增长: 不给表头赋值时,通过自+1 的结算结果给表头赋值 ​ 自增长必须和主键连用 ​ 自增长是计算最后一行的行号加1,不受delete删除的影响,但是受truncate截断的影响 **给已有表添加行号表头** ​ alter table 库名.表名 add id(自定义) int primary key auto_increment first; ---- **外键 foreigh key** 外键的作用就是==保证数据的一致性== ---- 创建表a时,将表b中某一个表头(数据类型与表a对应的表头一致)作为表a的外键 create table 库.表a ( 表头1 数据类型, ... , 表头n 数据类型, ​ foreign key (表a的表头名) reference 库.表b(表b里的表头名) ​ on update cascade //同步更新 ​ on delete cascade //同步删除 ) engine= innodb //mysql8以下的版本要加上默认检索引擎innodb,8以上默认。 ---- **查看表的外键** : show create table 库名.表名; **删除外键**:alter table 库名.表名 drop foreign key 外键名 ; //外键名是上面show查找出来的 **添加外键** :**alter table 库名.表a add foreign key (表a的表头名) reference 库.表b(表b里的表头名) on update cascade on delete cascade** MYSQL索引 ​ 索引:数据结构 ​ 设置再表头,设置后会给表头下存储的数据生成排队信息,把排队信息表保存在表对应的文件里 ​ 索引的优点: 通过索引信息,查找数据可以加快查找速度 ​ 缺点: 减慢对数据做写访问(insert, update,delete....)的速度 ​ 类型 ​ unique: 表头值唯一 ​ primary key 表头值唯一且不允许赋值null ​ index 普通索引,对表头储存没有任何要求,只负责给表头的数据生成排队信息,一个表里可以给多个表头生成排队信息 ​ 单列索引、多列索引 ​ 普通索引MUL的使用 ​ 创建: 建表时给表头加索引标签 index(表头名) ​ create table 库名( ​ .... ​ index(表名1), index(表名2)... ​ ) ​ 添加: create index 索引表名 on 库名.表名(表头名) //默认一致 ​ 删除: drop index 索引名 on 库名.表名 ​ 查看: show index from 表名 ```mysql Table: t2 //索引的表 Non_unique: 1 Key_name: name //索引名,默认与olumn_name一致 Seq_in_index: 1 Column_name: name //索引的表头名 Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: YES Index_type: BTREE //索引算法 Comment: Index_comment: Visible: YES Expression: NULL ``` 建表三范式 ​ 衡量表创建是否合理的标准 1NF , 2NF ,3NF,4NF,...7NF,BCNF ​ 1NF: 表头下储存的数据不能再拆分成全体属性(原子性) ​ 2NF:表里要有主键 ​ 3NF:非主属性之间必须不存在传递依赖 用户授权 ​ 连接数据库服务后,在数据库服务里添加用户。给客户端连接mysql服务使用。默认仅允许数据库管理员root用户访问本机的数据库服务 ​ 1、 创建用户并设置密码 create user ​ create user 用户名@“客户端地址” identified by “密码”; ​ create user root@“%” identified by “123456” //%表示任何主机 ​ 2、授予/追加权限 grant (库名,客户端地址,用户都不变就是追加权限) ​ grant 权限 on 库名 to 用户名@“客户端地址” ​ 如果给全部权限,直接写all ​ 3、 撤销权限 ​ 删除用户已经有的权限 revoke ​ revoke 权限 on l库名 from 用户名@“客户端地址”; ​ 仅删除一个权限 revoke delete on \*.\* from root@"%" ​ 一起删除多个权限 revoke update , create, drop on \*\.* from root@"%" ​ 删除所有权限 revoke all on \*.\* from root@"%"; ​ 删除用户 drop user 用户名@“客户端地址” ; 4、查看相关用户的权限 ​ show grants for 用户名@“客户端地址”; ​ flush privileges; 更新权限 相关权限 ​ ![image-20240715190901750](https://gitee.com/xiaoxinbupa/linux-note/raw/master/linux_base_picture/image-20240715190901750.png) ![image-20240715190941559](https://gitee.com/xiaoxinbupa/linux-note/raw/master/linux_base_picture/image-20240715190941559.png) mysql系统表记录相关权限 ![image-20240715192636906](https://gitee.com/xiaoxinbupa/linux-note/raw/master/linux_base_picture/image-20240715192636906.png)