1. 关系型数据库介绍
1.1 数据结构模型
数据结构模型主要有:
- 层次模型
- 网状结构
- 关系模型
数据库
关系型数据库:
- mysql 5.7 免费/ mariadb 10.0
- MySQL 8.0 收费
- Oracle 收费
- mongodb
- sqlite
- MSSQL MicroSoft 图形化数据库
缓存数据库:
- menmcache
- redis
关系模型:
二维关系: row,column (行,列)
数据库管理系统:DBMS(DataBase Managerment System)
关系:RDBMS(Relationship DataBase Managerment System)
1.2 RDBMS专业名词
常见的关系型数据库管理系统:
- 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:有两种
2.2 mysql配置
启动mysql并设置开机自动启动
systemctl enable --now mysqld
systemctl status mysqld
确保3306端口已经监听起来
ss -antl
添加密码
[root@localhost ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.17-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('my');
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> quit
Bye
使用密码登录
[root@localhost ~]# mysql -uroot -pmy
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.3.17-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)]> quit
Bye
3. mysql的程序组成
- 客户端
mysql:CLI交互式客户端程序
mysql_secure_installation:安全初始化,强烈建议安装完以后执行此命令
mysqldump:mysql备份工具
mysqladmin - 服务器端
mysqld
3.1 MySQL工具使用
构建yum仓库
[root@yibie ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2595 100 2595 0 0 3691 0 --:--:-- --:--:-- --:--:-- 3691
[root@yibie ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@yibie ~]# yum clean all
Failed to set locale, defaulting to C.UTF-8
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.
0 files removed
[root@yibie ~]#
下载mariadb
[root@yibie ~]# yum -y install mariadb mariadb-server
Failed to set locale, defaulting to C.UTF-8
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.
CentOS-8 - Base - mirrors.aliyun.com 435 kB/s | 5.6 MB 00:13
CentOS-8 - Extras - mirrors.aliyun.com 14 kB/s | 10 kB 00:00
CentOS-8 - AppStream - m 27% [====== ] 307 kB/s | 2.4 MB 00:21 ETA
…………
Verifying : mariadb-server-utils-3:10.3.28-1.module_el8.3.0+757+d3829 9/10
Verifying : perl-DBD-MySQL-4.046-3.module_el8.1.0+203+e45423dc.x86_64 10/10
Installed products updated.
Installed:
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-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-server-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
perl-DBD-MySQL-4.046-3.module_el8.1.0+203+e45423dc.x86_64
Complete!
查看版本号
[root@yibie ~]# mysql -V
mysql Ver 15.1 Distrib 10.3.28-MariaDB, for Linux (x86_64) using readline 5.1
启动并初始化mariadb
[root@yibie ~]# systemctl restart mariadb.service
[root@yibie ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
You already have a root password set, so you can safely answer 'n'.
Change the root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
登录mysql
// 建议不在命令行输入密码
[root@yibie ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 26
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)]>
// 在命令行输入密码
[root@yibie ~]# mysql -uroot -p123
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 27
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)]>
mysql客户端工具:
mysql
navicat 商业的,收费的,支持中英文
workbench 开源的,免费的