1.1 数据结构模型

数据结构模型主要有:

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

关系模型:
二维关系: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安装方式有三种:

  • 源代码:编译安装
  • 二进制格式的程序包:展开至特定路径,并经过简单配置后即可使用
  • 程序包管理器管理的程序包:
    • rpm:有两种
      • OS Vendor:操作系统发行商提供的
      • 项目官方提供的
    • deb

编译安装mysql

Index of /232905 (mysql.com)

https://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm        //下载157这版本的rpm包

[root@mingzi ~]# ls
anaconda-ks.cfg  mysql57-community-release-el7-10.noarch.rpm
[root@mingzi ~]# rpm -ivh mysql57-community-release-el7-10.noarch.rpm
warning: mysql57-community-release-el7-10.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql57-community-release-el7-10 ################################# [100%]

//安装

Index of /232905/yum/mysql-5.7-community/el/7打开这个链接,下载5个依赖包

http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-client-5.7.37-1.el7.x86_64.rpm

http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-common-5.7.37-1.el7.x86_64.rpm

http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-devel-5.7.37-1.el7.x86_64.rpm

http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-libs-5.7.37-1.el7.x86_64.rpm

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@mingzi ~]# ls
anaconda-ks.cfg
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

//下载好查看这5个rpm包,把多余的rpm包删除

[root@mingzi ~]# yum -y install *.rpm
//打包安装5个rpm包

 mysql配置

设置开机自启

[root@mingzi ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; ven>
   Active: inactive (dead)
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
[root@mingzi ~]# systemctl enable --now mysqld

[root@mingzi ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; ven>
   Active: active (running) since Mon 2022-04-18 14:00:46 CST; 51s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 12852 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var>
  Process: 12802 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited,>
 Main PID: 12854 (mysqld)
    Tasks: 27 (limit: 11217)
   Memory: 294.3M
   CGroup: /system.slice/mysqld.service
           └─12854 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mys>

Apr 18 14:00:43 mingzi systemd[1]: Starting MySQL Server...
Apr 18 14:00:46 mingzi systemd[1]: Started MySQL Server.

查看端口号

[root@mingzi ~]# ss -ant
State  Recv-Q  Send-Q   Local Address:Port   Peer Address:Port  Process 
LISTEN 0       128            0.0.0.0:22          0.0.0.0:*             
ESTAB  0       0         192.168.80.3:22     192.168.80.1:64468         
ESTAB  0       52        192.168.80.3:22     192.168.80.1:59195         
LISTEN 0       80                   *:3306              *:*             
LISTEN 0       128               [::]:22             [::]:*      

过滤出mysql的临时密码

[root@mingzi ~]# grep 'password' /var/log/mysqld.log 
2022-04-18T06:00:44.028092Z 1 [Note] A temporary password is generated for root@localhost: l/)L%pqib2.T


l/)L%pqib2.T

使用-uroot,输入临时密码进入数据库

[root@mingzi ~]# mysql -ueoot -p'l/)L%pqib2.T'
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'eoot'@'localhost' (using password: YES)
[root@mingzi ~]# mysql -uroot -p'l/)L%pqib2.T'
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('Mingzi540..');
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> 

//加上括号是为了给密码加密

使用新密码登录

[root@mingzi ~]# mysql -uroot -p'Mingzi540..'
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> 
//为避免mysql自动升级,这里需要卸载最开始安装的yum源
rpm -e mysql57-community-release

mysql的程序组成

  • 客户端
    • mysql:CLI交互式客户端程序
    • mysql_secure_installation:安全初始化,强烈建议安装完以后执行此命令
    • mysqldump:mysql备份工具
    • mysqladmin
  • 服务器端
    • mysqld

使用Navicat fo mysql 链接客户端

 先关闭防火墙

[root@mingzi ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.



[root@mingzi ~]# setenforce 0
[root@mingzi ~]# vi /etc/selinux/config 


# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled

给真机ip做一个授权

mysql> GRANT all ON *.* TO 'root'@'192.168.80.1' IDENTIFIED BY 'Mingzi540..';
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> 

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

 

 链接成功

 mysql工具使用

//查看当前使用的mysql版本
[root@mingzi ~]# mysql -V
mysql  Ver 14.14 Distrib 5.7.37, for Linux (x86_64) using  EditLine wrapper
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> 
//不登录mysql执行sql语句后退出,常用于脚本
[root@mingzi ~]# mysql -uroot -p'Mingzi540..' -e 'SHOW DATABASES;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
[root@mingzi ~]# mysql -uroot -p -e 'SHOW DATABASES;'
Enter password: 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
[root@mingzi ~]# mysql -uroot -p -e 'SHOW DATABASES;'
Enter password: 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
//注意,不推荐直接在命令行里直接用-pPASSWORD的方式登录,而是使用-p选项,然后交互式输入密码