关系型数据库介绍
数据结构模型
数据结构模型主要有:
- 层次模型
- 网状结构
- 关系模型
关系模型:
二维关系:row(行),column(列)
数据库管理系统:DBMS
关系:Relational,RDBMS
RDBMS专业名词
常见的关系型数据库管理系统:
- MySQL:MySQL,MariaDB,Percona-Server
- PostgreSQL:简称为pgsql
- Oracle
- MSSQL
SQL:Structure Query Language,结构化查询语言
约束:constraint,向数据表提供的数据要遵守的限制
-
主键约束:一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。且必须提供数据,不能为空(NOT NULL)。
一个表只能存在一个 -
惟一键约束:一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。允许为空(NULL)
一个表可以存在多个 -
外键约束:一个表中的某字段可填入数据取决于另一个表的主键已有的数据
-
检查性约束
索引:将表中的一个或多个字段中的数据复制一份另存,并且这些数据需要按特定次序排序存储
关系型数据库的常见组件
关系型数据库的常见组件有:
- 数据库:database
- 表:table,由行(row)和列(column)组成
- 索引:index
- 视图:view
- 用户:user
- 权限:privilege
- 存储过程:procedure
- 存储函数:function
- 触发器:trigger
- 事件调度器:event scheduler
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:移除授权 |
mysql安装与配置
mysql安装
mysql安装方式有三种:
- 源代码:编译安装
- 二进制格式的程序包:展开至特定路径,并经过简单配置后即可使用
- 程序包管理器管理的程序包:
- rpm:有两种
- OS Vendor:操作系统发行商提供的
- 项目官方提供的
- deb
- rpm:有两种
配置mysql的yum源
[root@localhost ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
位置:https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm [跟随至新的 URL]
--2022-07-22 05:48:05-- https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
正在解析主机 repo.mysql.com (repo.mysql.com)... 23.55.248.248
正在连接 repo.mysql.com (repo.mysql.com)|23.55.248.248|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:25680 (25K) [application/x-redhat-package-manager]
正在保存至: “mysql57-community-release-el7-11.noarch.rpm”
mysql57-community-release-el7-11.noarch.rpm 100%[=================================================================================================>] 25.08K 83.1KB/s 用时 0.3s
2022-07-22 05:48:07 (83.1 KB/s) - 已保存 “mysql57-community-release-el7-11.noarch.rpm” [25680/25680])
[root@localhost ~]#
[root@localhost ~]# rpm -Uvh mysql57-community-release-el7-11.noarch.rpm
警告:mysql57-community-release-el7-11.noarch.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
Verifying... ################################# [100%]
准备中... ################################# [100%]
正在升级/安装...
1:mysql57-community-release-el7-11 ################################# [100%]
使用yum安装mysql5.7需要的软件包,,关闭此模块yum module disable mysql
[root@localhost ~]# yum module disable mysql
上次元数据过期检查:0:13:35 前,执行于 2022年07月22日 星期五 06时04分16秒。
依赖关系解决。
=========================================================================================================================================================================================
软件包 架构 版本 仓库 大小
=========================================================================================================================================================================================
禁用模块:
mysql
事务概要
=========================================================================================================================================================================================
确定吗?[y/N]: y
完毕!
[root@localhost ~]# yum -y install mysql-community-server mysql-community-client mysql-community-common mysql-community-devel --nogpgcheck
....
已安装:
mysql-community-client-5.7.38-1.el7.x86_64 mysql-community-common-5.7.38-1.el7.x86_64 mysql-community-devel-5.7.38-1.el7.x86_64 mysql-community-libs-5.7.38-1.el7.x86_64
mysql-community-server-5.7.38-1.el7.x86_64 ncurses-compat-libs-6.1-9.20180224.el8.x86_64
完毕!
[root@localhost ~]#
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 Fri 2022-07-22 06:22:19 EDT; 2s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 3856 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 3802 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 3858 (mysqld)
Tasks: 27 (limit: 23330)
Memory: 328.2M
CGroup: /system.slice/mysqld.service
└─3858 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
7月 22 06:22:15 localhost.localdomain systemd[1]: Starting MySQL Server...
7月 22 06:22:19 localhost.localdomain systemd[1]: Started MySQL Server.
[root@localhost ~]#
查看端口
[root@localhost ~]# ss -antlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 127.0.0.1:6010 0.0.0.0:* users:(("sshd",pid=2427,fd=15))
LISTEN 0 128 0.0.0.0:111 0.0.0.0:* users:(("rpcbind",pid=1016,fd=4),("systemd",pid=1,fd=116))
LISTEN 0 32 192.168.122.1:53 0.0.0.0:* users:(("dnsmasq",pid=2125,fd=6))
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=1279,fd=5))
LISTEN 0 5 127.0.0.1:631 0.0.0.0:* users:(("cupsd",pid=1285,fd=10))
LISTEN 0 128 [::1]:6010 [::]:* users:(("sshd",pid=2427,fd=14))
LISTEN 0 80 *:3306 *:* users:(("mysqld",pid=3858,fd=22))
LISTEN 0 128 [::]:111 [::]:* users:(("rpcbind",pid=1016,fd=6),("systemd",pid=1,fd=118))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=1279,fd=7))
LISTEN 0 5 [::1]:631 [::]:* users:(("cupsd",pid=1285,fd=9))
[root@localhost ~]#
在日志文件中找出临时密码
[root@localhost ~]# grep "password" /var/log/mysqld.log
2022-07-22T10:22:17.900718Z 1 [Note] A temporary password is generated for root@localhost: bf.Zx4-qShsi
登录并修改密码
[root@localhost ~]# mysql -uroot -pbf.Zx4-qShsi
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 2
Server version: 5.7.38
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> 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> ALTER USER 'root'@'localhost' IDENTIFIED BY 'haha123';
Query OK, 0 rows affected (0.01 sec)
mysql> exit
Bye
[root@localhost ~]#
mariadb安装与设置密码
[root@localhost ~]# yum -y install mariadb*
......
已安装:
Judy-1.0.5-18.module_el8.3.0+757+d382997d.x86_64 copy-jdk-configs-4.0-2.el8.noarch
galera-25.3.32-1.module_el8.3.0+757+d382997d.x86_64 java-1.8.0-openjdk-headless-1:1.8.0.322.b06-11.el8.x86_64
javapackages-filesystem-5.3.0-1.module_el8.0.0+11+5b8c10bd.noarch jna-4.5.1-5.el8.x86_64
keyutils-libs-devel-1.5.10-9.el8.x86_64 krb5-devel-1.18.2-14.el8.x86_64
libaio-devel-0.3.112-1.el8.x86_64 libcom_err-devel-1.45.6-2.el8.x86_64
libkadm5-1.18.2-14.el8.x86_64 libselinux-devel-2.9-5.el8.x86_64
libsepol-devel-2.9-3.el8.x86_64 libverto-devel-0.3.0-5.el8.x86_64
lksctp-tools-1.0.18-3.el8.x86_64 mariadb-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64
mariadb-backup-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64 mariadb-common-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64
mariadb-connector-c-3.1.11-2.el8_3.x86_64 mariadb-connector-c-config-3.1.11-2.el8_3.noarch
mariadb-connector-c-devel-3.1.11-2.el8_3.x86_64 mariadb-connector-odbc-3.1.12-1.el8.x86_64
mariadb-devel-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64 mariadb-embedded-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64
mariadb-embedded-devel-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64 mariadb-errmsg-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64
mariadb-gssapi-server-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64 mariadb-java-client-2.7.1-2.el8.noarch
mariadb-oqgraph-engine-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64 mariadb-server-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64
mariadb-server-galera-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64 mariadb-server-utils-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64
mariadb-test-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64 openssl-devel-1:1.1.1k-5.el8_5.x86_64
pcre2-devel-10.32-2.el8.x86_64 pcre2-utf16-10.32-2.el8.x86_64
pcre2-utf32-10.32-2.el8.x86_64 perl-DBD-MySQL-4.046-3.module_el8.3.0+419+c2dec72b.x86_64
perl-Env-1.04-395.el8.noarch perl-Memoize-1.03-421.el8.noarch
perl-Test-Simple-1:1.302135-1.el8.noarch perl-Time-HiRes-4:1.9758-2.el8.x86_64
tzdata-java-2022a-2.el8.noarch unixODBC-2.3.7-1.el8.x86_64
zlib-devel-1.2.11-17.el8.x86_64
完毕!
.....
[root@localhost ~]# systemctl start mariadb.service
[root@localhost ~]# systemctl enable --now mariadb.service
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
[root@localhost ~]#
[root@localhost ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.28-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> set password = password('1');
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> exit
Bye
[root@localhost ~]# mysql -uroot -p1
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 15
Server version: 10.3.28-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
3060

被折叠的 条评论
为什么被折叠?



