1、关系型数据库介绍
1.1数据结构模型
数据结构模型主要有
- 层次模型
- 网状结构
- 关系模型
关系模型:
二维关系:row(行-一条),column(列-一个字段)
数据库管理系统:DBMS(DataBase Management System)
关系:Relational,RDBMS
1.2RDBMS专业名词
常见的关系型数据库管理系统:
- MySQL:MySQL,MariaDB(免费),Percona-Server redis,memcache(缓存)
- PostgreSQL:简称为pgsql mongodb
- Oracle(和MySQL现在是同一家公司)
- MSSQL(MicroSoft SQLServer,公司不用)
SQL:Structure Query Language,结构化查询语言
数据存放方式:
- 结构化数据(数据库)
- 半结构化数据(文本文件)
- 非结构化数据(图片视频音频)
约束:constraint,向数据表提供的数据要遵守的限制
- 主键约束:一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。且必须提供数据,不能为空(NOT NULL)。
- 一个表只能存在一个
- 惟一键约束:一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。允许为空(NULL)
- 一个表可以存在多个
- 外键约束:一个表中的某字段可填入数据取决于另一个表的主键已有的数据
- 检查性约束
索引:将表中的一个或多个字段中的数据复制一份另存,并且这些数据需要按特定次序排序存储
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,数据控制语言
SQL语句类型 | 对应操作 |
---|---|
DDL | CREATE:创建 DROP:删除 ALTER:修改 |
DML | INSERT:向表中插入数据 DELETE:删除表中数据 UPDATE:更新表中数据 SELECT:查询表中数据 |
DCL | GRANT:授权 REVOKE:移除授权 |
2. mysql安装与配置
2.1 mysql安装
mysql安装方式有三种:
- 源代码:编译安装
- 二进制格式的程序包:展开至特定路径,并经过简单配置后即可使用(一般用这个来安装)
- 程序包管理器管理的程序包:(用yum或rpm装,但企业一般不用)
- rpm:有两种
- OS Vendor:操作系统发行商提供的
- 项目官方提供的
- deb
- rpm:有两种
网站https://dev.mysql.com/(和阿里云的源都是8.0版本的)
安装5.7版本
在这个网站里下载
//下载
[root@localhost ~]# wget http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-client-5.7.37-1.el7.x86_64.rpm
[root@localhost ~]# wget http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-common-5.7.37-1.el7.x86_64.rpm
[root@localhost ~]# wget http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-devel-5.7.37-1.el7.x86_64.rpm
[root@localhost ~]# wget http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-libs-5.7.37-1.el7.x86_64.rpm
[root@localhost ~]# wget http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-server-5.7.37-1.el7.x86_64.rpm
[root@localhost ~]# ls
公共 下载 mysql-community-client-5.7.37-1.el7.x86_64.rpm
模板 音乐 mysql-community-common-5.7.37-1.el7.x86_64.rpm
视频 桌面 mysql-community-devel-5.7.37-1.el7.x86_64.rpm
图片 anaconda-ks.cfg mysql-community-libs-5.7.37-1.el7.x86_64.rpm
文档 initial-setup-ks.cfg mysql-community-server-5.7.37-1.el7.x86_64.rpm
[root@localhost ~]# yum -y install *.rpm //因为里面只有mysql这些rpm包了,所以可以直接用这个方式安装
2.2 mysql配置
//设置开机自启
[root@localhost ~]# systemctl enable --now mysqld
[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2022-04-18 16:51:51 CST; 38s ago
....
[root@localhost ~]# ss -antl //查看3306启动了
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
//查看临时密码
[root@localhost ~]# grep 'password' /var/log/mysqld.log
2022-04-18T08:51:49.120021Z 1 [Note] A temporary password is generated for root@localhost: :c?fe_rwQ25a
//使用获取到的临时密码登录
[root@localhost ~]# mysql -uroot -p':c?fe_rwQ25a'
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 3
Server version: 5.7.37
Copyright (c) 2000, 2022, 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> set password = password('Ltt429520.');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
[root@localhost ~]#
//为避免mysql自动升级,把最开始安装的yum源卸载
[root@localhost ~]# rpm -e mysql57-community-release
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.37, for Linux (x86_64) using EditLine wrapper
[root@localhost ~]# mysql -uroot -p'Ltt429520.' -h192.168.160.130
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 9
Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, 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>
//注意,不推荐直接在命令行里直接用-pPASSWORD的方式登录,而是使用-p选项,然后交互式输入密码
[root@localhost ~]# mysql -uroot -p -h192.168.160.130
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, 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>
[root@localhost ~]# mysql -uroot -p -h 192.168.160.130 -e 'SHOW DATABASES;'
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| |
+--------------------+