文章目录
1. 关系型数据库介绍
1.1 数据结构模型
数据结构模型主要有:
- 层次模型
- 网状结构
- 关系模型
关系模型:
二维关系:row,column
数据库管理系统:DBMS
关系:Relational,RDBMS
1.2 RDBMS专业名词
数据(Data): 反映客观世界的事实,并可以区分其特征的符号:字符、数字、文本、声音、图形、图像、图表、图片等,它们是现实世界中客观存在的,可以输入到计算机中进行存储和管理的。
信息(Information): 由原始数据经加工提炼而成的、用于决定行为、计划或具有一定语义的数据称为信息。
数据库 DB(Data Base): 是现实世界中相互关联的大量数据及数据间关系的集合。
常见的关系型数据库管理系统:
数据库管理系统是对数据库中的数据进行存储和管理的软件系统。包括存储、管理、检索和控制数据库 中数据的各种语言和工具,是一套系统软件。
- MySQL:MySQL,MariaDB,Percona-Server
- PostgreSQL:简称为pgsql
- Oracle
- MSSQL
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安装方式有三种:
- 源代码:编译安装
- 二进制格式的程序包:展开至特定路径,并经过简单配置后即可使用
- 程序包管理器管理的程序包:
- rpm:有两种
- OS Vendor:操作系统发行商提供的
- 项目官方提供的
- deb
- rpm:有两种
[root@localhost ~]# which wget
/usr/bin/wget
[root@localhost ~]# wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm
[root@localhost ~]# rpm -ivh mysql57-community-release-el7-10.noarch.rpm
[root@localhost ~]# ls
公共 视频 文档 音乐 anaconda-ks.cfg mysql57-community-release-el7-10.noarch.rpm
模板 图片 下载 桌面 initial-setup-ks.cfg
[root@localhost ~]# ls /etc/yum.repos.d/
mysql-community.repo mysql-community-source.repo redhat.repo zj.repo
[root@localhost ~]# dnf clean all
[root@localhost ~]# dnf makecache
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
MySQL Connectors Community 102 kB/s | 133 kB 00:01
MySQL Tools Community 405 kB/s | 832 kB 00:02
MySQL 5.7 Community Server 1.0 MB/s | 2.5 MB 00:02
BaseOS 165 MB/s | 2.3 MB 00:00
AppStream 181 MB/s | 5.8 MB 00:00
元数据缓存已建立。
[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-compat-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 ~]# rm -rf mysql57-community-release-el7-10.noarch.rpm
[root@localhost ~]# ls
公共 anaconda-ks.cfg
模板 initial-setup-ks.cfg
视频 mysql57-community-release-el7-10.noarch.rpm
图片 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
音乐 mysql-community-libs-5.7.37-1.el7.x86_64.rpm
桌面 mysql-community-server-5.7.37-1.el7.x86_64.rpm
[root@localhost ~]# dnf -y install *.rpm
2.2 mysql配置
[root@localhost ~]# systemctl enable --now mysqld //启动mysql并设置开机自动启动
[root@localhost ~]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset:>
Active: active (running) since Mon 2022-04-18 19:03:14 CST; 13s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 71032 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld>
Process: 70476 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/S>
Main PID: 71037 (mysqld)
Tasks: 27 (limit: 11159)
Memory: 302.3M
CGroup: /system.slice/mysqld.service
└─71037 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.>
4月 18 19:03:00 localhost.localdomain systemd[1]: Starting MySQL Server...
4月 18 19:03:14 localhost.localdomain systemd[1]: Started MySQL Server.
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
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 [::]:*
使用获取到的临时密码登录mysql
[root@localhost ~]# grep "password" /var/log/mysqld.log
2022-04-18T11:03:10.946413Z 1 [Note] A temporary password is generated for root@localhost: 2+fipbIa9yll
[root@localhost ~]# mysql -uroot -p2+fipbIa9yll
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登录密码
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.00 sec)
mysql> set password = password('090.Com');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
[root@localhost ~]# mysql -uroot -p090.Com
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 4
Server version: 5.7.37 MySQL Community Server (GPL)
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> quit
Bye
[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 -uroot -p090.Com
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 5
Server version: 5.7.37 MySQL Community Server (GPL)
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> GRANT ALL ON *.* TO 'root'@'192.168.201.1' IDENTIFIED BY '090.Com!';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.01 sec)
mysql> quit
Bye
[root@localhost ~]# systemctl disable --now firewalld.service
[root@localhost ~]# setenforce 0
在Navicat软件内创建连接 输入需要连接的虚拟机ip和数据库密码加粗样式