mysql服务

这篇博客介绍了关系型数据库的基础知识,包括关系模型、RDBMS的专业术语如事务、约束和索引。重点讲解了MySQL的安装配置,提供了详细的步骤,包括设置yum源、启动服务、修改登录密码等。此外,还给出了关于创建数据库、表以及执行SQL语句(如INSERT、SELECT、UPDATE、DELETE)的实践习题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 关系型数据库介绍
    1.1 数据结构模型

数据结构模型主要有:

层次模型
网状结构
关系模型

关系模型:
二维关系:row,column

数据库管理系统:DBMS
关系:Relational,RDBMS
1.2 RDBMS专业名词(relational database manager system)

常见的关系型数据库管理系统:

MySQL:MySQL,MariaDB,Percona-Server
PostgreSQL:简称为pgsql
Oracle
MSSQL

事务:多个操作被当作一个整体对待就称为一个事务
要看一个关系型数据库是否支持事务,需要看其是否支持并满足ACID测试
ACID:ACID是事务的一个基本标准

A:Automicity,原子性
C:Consistency,一致性
I:Isolation,隔离性
D:Durability,持久性

如果你对ACID感兴趣,可以查看这里了解详细说明,ACID将不作为我们讲解的重点。

**SQL:**Structure Query Language,结构化查询语言

**约束:**constraint,向数据表提供的数据要遵守的限制

主键约束:一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。且必须提供数据,不能为空(NOT NULL)。
    一个表只能存在一个
惟一键约束:一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。允许为空(NULL)
    一个表可以存在多个
外键约束:一个表中的某字段可填入数据取决于另一个表的主键已有的数据
检查性约束

**索引:**将表中的一个或多个字段中的数据复制一份另存,并且这些数据需要按特定次序排序存储

关系运算:

选择:挑选出符合条件的行(部分行)
投影:挑选出需要的字段
连接

数据抽象方式:

物理层:决定数据的存储格式,即RDBMS在磁盘上如何组织文件
逻辑层:描述DB存储什么数据,以及数据间存在什么样的关系
视图层:描述DB中的部分数据

1.3 关系型数据库的常见组件

关系型数据库的常见组件有:

数据库:database
表:table,由行(row)和列(column)组成
索引:index
视图:view
用户:user
权限:privilege
存储过程:procedure
存储函数:function
触发器:trigger
事件调度器:event scheduler

1.4 SQL语句

SQL语句有三种类型:

DDL:Data Defination Language,数据定义语言
DML:Data Manipulation Language,数据操纵语言
DCL:Data Control Language,数据控制语言

2. mysql安装与配置

2.1 mysql安装

mysql安装方式有三种:

源代码:编译安装
二进制格式的程序包:展开至特定路径,并经过简单配置后即可使用
程序包管理器管理的程序包:
    rpm:有两种
        OS Vendor:操作系统发行商提供的
        项目官方提供的
    deb
//配置mysql的yum源
[root@localhost ~]# cd /usr/src/
[root@localhost src]# ls
debug  kernels
[root@localhost src]# wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
下载过程略
[root@localhost src]# ls
debug  kernels  mysql57-community-release-el7-10.noarch.rpm
[root@localhost src]# yum -y install mysql57-community-release-el7-10.noarch.rpm
Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-
              : manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Examining mysql57-community-release-el7-10.noarch.rpm: mysql57-community-release-el7-10.noarch 
....
Installed:
  mysql57-community-release.noarch 0:el7-10

Complete!
[root@localhost ~]# ls /etc/yum.repos.d/
mysql-community.repo  mysql-community-source.repo


//安装mysql5.7
[root@localhost ~]# yum -y install mysql-community-server mysql-community-client mysql-community-common mysql-community-devel
Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-
              : manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-client.x86_64 0:5.7.23-1.el7 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 5.7.9 for package: mysql-community-client-5.7.23-1.el7.x86_64 
....
Installed:
  mysql-community-client.x86_64 0:5.7.23-1.el7
  mysql-community-common.x86_64 0:5.7.23-1.el7
  mysql-community-devel.x86_64 0:5.7.23-1.el7
  mysql-community-libs.x86_64 0:5.7.23-1.el7
  mysql-community-libs-compat.x86_64 0:5.7.23-1.el7
  mysql-community-server.x86_64 0:5.7.23-1.el7

Replaced:
  mariadb-libs.x86_64 1:5.5.56-2.el7

Complete!

3. mysql的程序组成

客户端
    mysql:CLI交互式客户端程序
    mysql_secure_installation:安全初始化,强烈建议安装完以后执行此命令
    mysqldump:mysql备份工具
    mysqladmin
服务器端
    mysqld
3.1 mysql工具使用
//语法:mysql [OPTIONS] [database]
//常用的OPTIONS:
    -uUSERNAME      //指定用户名,默认为root
    -hHOST          //指定服务器主机,默认为localhost,推荐使用ip地址
    -pPASSWORD      //指定用户的密码
    -P#             //指定数据库监听的端口,这里的#需用实际的端口号代替,如-P3307
    -V              //查看当前使用的mysql版本
    -e          //不登录mysql执行sql语句后退出,常用于脚本
    

[root@localhost ~]# mysql -V
mysql  Ver 14.14 Distrib 5.7.23, for Linux (x86_64) using  EditLine wrapper

习题

1.搭建mysql服务

2.创建一个以你名字为名的数据库,并创建一张表student,该表包含三个字段(id,name,age),表结构如下:

mysql> desc student;
±------±-------------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±------±-------------±-----±----±--------±------+
| id | int(11) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
±------±-------------±-----±----±--------±------+
3 rows in set (0.00 sec)

3.查看下该新建的表有无内容(用select语句)
4.往新建的student表中插入数据(用insert语句),结果应如下所示:

±—±------------±-----+
| id | name | age |
±—±------------±-----+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | NULL |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
±—±------------±-----+

5.修改lisi的年龄为50
6.以age字段降序排序
7.查询student表中年龄最小的3位同学
8.查询student表中年龄最大的4位同学
9.查询student表中名字叫zhangshan的记录
10.查询student表中名字叫zhangshan且年龄大于20岁的记录
11.查询student表中年龄在23到30之间的记录
12.修改wangwu的年龄为100
13.删除student中名字叫zhangshan且年龄小于等于20的记录

解答

1.配置MySQL的yum 源
[root@localhost ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.
[root@localhost ~]# ls
anaconda-ks.cfg mysql57-community-release-el7-10.noarch.rpm
开启服务 查看端口
[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# systemctl enable mysqld
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :22 :
LISTEN 0 100 127.0.0.1:25 :
LISTEN 0 128 :::22 :::

LISTEN 0 100 ::1:25 ::: *
LISTEN 0 80 :::3306
查看临时密码
[root@localhost ~]# grep “password” /var/log/mysqld.log
2019-02-17T11:47:40.912923Z 1 [Note] A temporary password is generated for root@localhost: MBw1HnunrC_)
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.25

mysql> //看到有这样的标识符则表示成功登录了 //修改mysql登录密码
mysql> set global validate_password_policy=0;

Query OK, 0 rows affected (0.00 sec) mysql> set global validate_password_length=1;

Query OK, 0 rows affected (0.01 sec)
mysql> ALTER USER ‘root’@‘localhost’ IDENTIFIED BY ‘gsh’;
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye
为避免mysql自动升级,卸载开始的yum源
[root@localhost ~]# rpm -qa|grep mysql
mysql57-community-release-el7-10.noarch
mysql-community-common-5.7.24-1.el7.x86_64 mysql-community-client-5.7.24-1.el7.x86_64
mysql-community-libs-compat-5.7.24-1.el7.x86_64 mysql-community-libs-5.7.24-1.el7.x86_64
mysql-community-server-5.7.24-1.el7.x86_64
mysql-community-devel-5.7.24-1.el7.x86_64
[root@localhost ~]# yum -y remove mysql57-community-release-el7-10.noarch

2.创建一个以你名字为名的数据库,并创建一张表student,该表包含三个字段(id,name,age),表结构如下:

[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.25 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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> create database gsh;
Query OK, 1 row affected (0.00 sec)

mysql> use gsh;
Database changed
mysql> create table student(id int(10),name varchar(50),age int(10));
Query OK, 0 rows affected (0.03 sec)

mysql> desc students;
ERROR 1146 (42S02): Table 'gsh.students' doesn't exist
mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(10)     | YES  |     | NULL    |       |
| name  | varchar(50) | YES  |     | NULL    |       |
| age   | int(10)     | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

3.查看下该新建的表有无内容(用select语句)

mysql> select * from student;
Empty set (0.00 sec)

4.往新建的student表中插入数据(用insert语句),结果应如下所示:

mysql> insert student(id,name,age) value (1,'tom',20),(2,'jerry',23),(3,'wangqing',25),(4,'sean',28),(5,'zhangshan',26),(6,'zhangshan',20),(7,'lisi',null),(8,'chengshuo',10),(9,'wangwu',3),(10,'qiuyi',15),(11,'qiuxiaotian',20);
Query OK, 11 rows affected (0.06 sec)
Records: 11  Duplicates: 0  Warnings: 0

mysql> select * from student;
+------+-------------+------+
| id   | name        | age  |
+------+-------------+------+
|    1 | tom         |   20 |
|    2 | jerry       |   23 |
|    3 | wangqing    |   25 |
|    4 | sean        |   28 |
|    5 | zhangshan   |   26 |
|    6 | zhangshan   |   20 |
|    7 | lisi        | NULL |
|    8 | chengshuo   |   10 |
|    9 | wangwu      |    3 |
|   10 | qiuyi       |   15 |
|   11 | qiuxiaotian |   20 |
+------+-------------+------+
11 rows in set (0.00 sec)

5.修改lisi的年龄为50

mysql> update student set age = 50 where name = 'lisi';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from student where name = 'lisi';
+------+------+------+
| id   | name | age  |
+------+------+------+
|    7 | lisi |   50 |
+------+------+------+
1 row in set (0.00 sec)

6.以age字段降序排序

mysql> select * from student order by age desc;
+------+-------------+------+
| id   | name        | age  |
+------+-------------+------+
|    7 | lisi        |   50 |
|    4 | sean        |   28 |
|    5 | zhangshan   |   26 |
|    3 | wangqing    |   25 |
|    2 | jerry       |   23 |
|    1 | tom         |   20 |
|    6 | zhangshan   |   20 |
|   11 | qiuxiaotian |   20 |
|   10 | qiuyi       |   15 |
|    8 | chengshuo   |   10 |
|    9 | wangwu      |    3 |
+------+-------------+------+
11 rows in set (0.00 sec)

7.查询student表中年龄最小的3位同学

mysql> select * from student order by age limit 3;
+------+-----------+------+
| id   | name      | age  |
+------+-----------+------+
|    9 | wangwu    |    3 |
|    8 | chengshuo |   10 |
|   10 | qiuyi     |   15 |
+------+-----------+------+
3 rows in set (0.00 sec)

8.查询student表中年龄最大的4位同学

mysql> select * from student order by age desc limit 4;
+------+-----------+------+
| id   | name      | age  |
+------+-----------+------+
|    7 | lisi      |   50 |
|    4 | sean      |   28 |
|    5 | zhangshan |   26 |
|    3 | wangqing  |   25 |
+------+-----------+------+
4 rows in set (0.00 sec)

9.查询student表中名字叫zhangshan的记录

mysql> select * from student where name = 'zhangshan';
+------+-----------+------+
| id   | name      | age  |
+------+-----------+------+
|    5 | zhangshan |   26 |
|    6 | zhangshan |   20 |
+------+-----------+------+
2 rows in set (0.00 sec)

10.查询student表中名字叫zhangshan且年龄大于20岁的记录

mysql> select * from student where age between 23 and 30;
+------+-----------+------+
| id   | name      | age  |
+------+-----------+------+
|    2 | jerry     |   23 |
|    3 | wangqing  |   25 |
|    4 | sean      |   28 |
|    5 | zhangshan |   26 |
+------+-----------+------+
4 rows in set (0.00 sec)

11.查询student表中名字叫zhangshan且年龄大于20岁的记录

mysql> select * from student where name = 'zhangshan' and age >=20;
+------+-----------+------+
| id   | name      | age  |
+------+-----------+------+
|    5 | zhangshan |   26 |
|    6 | zhangshan |   20 |
+------+-----------+------+
2 rows in set (0.00 sec)

12.修改wangwu的年龄为100

mysql> update student set age = 100 where name = 'wangwu';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from student where name = 'wangwu';
+------+--------+------+
| id   | name   | age  |
+------+--------+------+
|    9 | wangwu |  100 |
+------+--------+------+
1 row in set (0.00 sec)

13.删除student中名字叫zhangshan且年龄小于等于20的记录

mysql> delete from student where name = 'zhangshan' and age<=20;
Query OK, 1 row affected (0.00 sec)

mysql> select * from student;
+------+-------------+------+
| id   | name        | age  |
+------+-------------+------+
|    1 | tom         |   20 |
|    2 | jerry       |   23 |
|    3 | wangqing    |   25 |
|    4 | sean        |   28 |
|    5 | zhangshan   |   26 |
|    7 | lisi        |   50 |
|    8 | chengshuo   |   10 |
|    9 | wangwu      |  100 |
|   10 | qiuyi       |   15 |
|   11 | qiuxiaotian |   20 |
+------+-------------+------+
10 rows in set (0.00 sec)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值