1. 数据库的种类
1.1 数据结构模型
数据结构模型主要有:
-
层次模型
-
网状结构
-
关系模型
-
关系模型:
二维关系:row,column
在早期数据库的类型一般分为三种 关系型数据库,非关系型数据库,以及网络式数据库
但是在现在的物联网通常把数据库分为两类:关系型数据库和非关系型数据库
- 采用的是二维表结构更加适合开发逻辑
- 数据存储在磁盘中,相比之下更加的安全
- 可以使用SQL语句在多个表中做复杂的查询
- 全部由表构成,文件格式基本一致
- #####非关系型数据库的特点:
- 可以实现数据分布式处理
- 因为是非关系型数据库所以具有很好的扩展性,稳定性和低成本等等特点
- 效率更高速度更快,存储方式可以使用硬盘也可以用随机存储器为载体
- 文件类型可以有很多种,例如key-value形式,文档形式,图片形式等等
数据库管理系统:DBMS
关系:Relational,RDBMS
2. RDBMS专业名词
2.1常见的关系型数据库管理系统:
-
MySQL:MySQL,MariaDB,Percona-Server
-
PostgreSQL:简称为pgsql
-
Oracle
-
MSSQL
-
SQL:Structure Query Language,结构化查询语言
-
约束:constraint,向数据表提供的数据要遵守的限制
-
主键约束:一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。且必须提供数据,不能为空(NOT NULL)。
一个表只能存在一个 -
惟一键约束:一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。允许为空(NULL)
一个表可以存在多个 -
外键约束:一个表中的某字段可填入数据取决于另一个表的主键已有的数据
检查性约束
索引:将表中的一个或多个字段中的数据复制一份另存,并且这些数据需要按特定次序排序存储
2.2关系型数据库的常见组件
关系型数据库的常见组件有:
- 数据库:database
- 表:table,由行(row)和列(column)组成
- 索引:index
- 视图:view
- 用户:user
- 权限:privilege
- 存储过程:procedure
- 存储函数:function
- 触发器:trigger
- 事件调度器:event scheduler
2.3 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源
[root@cxr ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
//安装mysql源
[root@cxr ~]# rpm -ivh mysql57-community-release-el7-10.noarch.rpm
- 然后需要进到https://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/ 在里面下载以下文件
https://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-client-5.7.37-1.el7.x86_64.rpm
https://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-common-5.7.37-1.el7.x86_64.rpm
https://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-devel-5.7.371.el7.x86_64.rpm
https://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-libs-5.7.37-1.el7.x86_64.rpm
https://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-server-5.7.371.el7.x86_64.rpm
- 用wget命令将它们下载到我们的虚拟机上
[root@cxr ~]# wget https://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-client-5.7.37-1.el7.x86_64.rpm
https://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-common-5.7.37-1.el7.x86_64.rpm
https://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-devel-5.7.37-1.el7.x86_64.rpm
https://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-libs-5.7.37-1.el7.x86_64.rpm
https://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-server-5.7.37-1.el7.x86_64.rpm
[root@cxr ~]# dnf -y install *.rpm //安装当前目录下的所有rpm包
已安装:
mysql-community-client-5.7.37-1.el7.x86_64 mysql-community-common-5.7.37-1.el7.x86_64 mysql-community-devel-5.7.37-1.el7.x86_64 mysql-community-libs-5.7.37-1.el7.x86_64
mysql-community-server-5.7.37-1.el7.x86_64 ncurses-compat-libs-6.1-7.20180224.el8.x86_64
完毕! //安装过程省略一部分
- 设置MySQL开机自启
[root@cxr ~]# systemctl enable --now mysqld
[root@cxr ~]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: d>
Active: active (running) since Mon 2022-04-18 05:52:34 EDT; 11s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 110813 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/>
Process: 110551 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SU>
Main PID: 110815 (mysqld)
Tasks: 27 (limit: 23648)
Memory: 338.1M
CGroup: /system.slice/mysqld.service
└─110815 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.p>
4月 18 05:52:27 cxr.example.com systemd[1]: Starting MySQL Server...
4月 18 05:52:34 cxr.example.com systemd[1]: Started MySQL Server.
[root@cxr ~]# ss -antl //查看一下没有3306端口
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 [::]:*
- 在日志文件中找到临时密码
[root@cxr ~]# grep 'password' /var/log/mysqld.log
2022-04-18T09:52:31.392560Z 1 [Note] A temporary password is generated for root@localhost:
i69>zkxu0q,D //这里的密码每个用户都是不一样的
- 使用临时密码登录到MySQL
[root@cxr ~]# mysql -uroot -p"i69>zkxu0q,D"
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.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> set password = password('Cxr1234!'); //进来第一步先改密码,否则会导致无法登录
Query OK, 0 rows affected, 1 warning (0.01 sec) //显示这样就表示已经成功
mysql> quit 退出MySQL
Bye
[root@cxr ~]# rpm -e mysql57-community-release //卸载掉最开始装的源,以防自动更新到最新版本
- 在开启一台虚拟机一个当客户端,一个当服务端。
[root@cxr ~]# mysql -uroot -p //使用root管理员进入到MySQL
Enter password: //输入刚才修改的密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
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.106.15' IDENTIFIED BY 'Cxr1234!'; //给客户端设置权限
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges
-> ;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges; //刷新一下
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT all ON *.* TO'root'@'192.168.106.18' IDENTIFIED BY 'Cxr1234!'; //给服务端设置权限
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges; //刷新一下
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
[root@cxr ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
- 用客户机访问服务机的mysql
[root@cxr ~]# mysql -uroot -p -h192.168.106.18
Enter password: //输入密码
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>
- 用Navicat 和虚拟机相连
[root@cxr ~]# mysql -uroot -p'Cxr1234!'
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 7
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.106.1' IDENTIFIED BY 'Cxr1234!';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
- 能够远程连接到就说明没有问题了