交流群:692356620,有不同的问题或见解可以来群里讨论,或者私聊我qq:1251611916
文章目录
1. 关系型数据库介绍
1.1 数据结构模型(数据库的种类)
数据结构模型主要有:
- 层次模型
- 网状结构
- 关系模型
关系模型:
二维关系:row(行),column(列)
数据库管理系统:DBMS(DataBase Management System)
关系:Relational,RDBMS(关系数据库管理系统)
1.2 RDBMS专业名词
常见的关系型数据库管理系统:
- MySQL:MySQL,MariaDB,Percona-Server
- PostgreSQL:简称为pgsql
- Oracle
- MSSQL (MicroSoft SQLServer)
SQL:(Structure Query Language)结构化查询语言
**约束:**constraint,向数据表提供的数据要遵守的限制
-
主键约束:一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。且必须提供数据,不能为空(NOT NULL)。
- 一个表只能存在一个
-
惟一键约束:一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。允许为空(NULL)
- 一个表可以存在多个
-
外键约束:一个表中的某字段可填入数据取决于另一个表的主键已有的数据
-
检查性约束
ps:字段,为列表中的列,单位为个
记录,为列表中的行,单位为行
索引:**将表中的一个或多个字段中的数据复制一份另存,并且这些数据需要按特定次序排序存储
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安装方式有三种:(常用于第二种)
- 源代码:编译安装
- 二进制格式的程序包:展开至特定路径,并经过简单配置后即可使用
- 程序包管理器管理的程序包:(yum或rpm的方式装)
- rpm:有两种
- OS Vendor:操作系统发行商提供的
- 项目官方提供的
- deb
- rpm:有两种
[root@duanruhui ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
[root@duanruhui ~]# ls
mysql57-community-release-el7-10.noarch.rpm
[root@duanruhui ~]# rpm -ivh mysql57-community-release-el7-10.noarch.rpm //安装mysql源
wget https://repo.mysql.com//yum/mysql-5.7-community/el/7/x86_64/mysql-community-server-5.7.37-1.el7.x86_64.rpm //下载server
wget https://repo.mysql.com//yum/mysql-5.7-community/el/7/x86_64/mysql-community-common-5.7.37-1.el7.x86_64.rpm //下载common
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 //下载client
wget https://repo.mysql.com//yum/mysql-5.7-community/el/7/x86_64/mysql-community-devel-5.7.37-1.el7.x86_64.rpm //下载devel
wget https://repo.mysql.com//yum/mysql-5.7-community/el/7/x86_64/mysql-community-libs-5.7.37-1.el7.x86_64.rpm //下载libs
[root@duanruhui ~]# ls
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@duanruhui ~]# dnf -y install *.rpm
2.2 mysql配置
#启动mysql并设置开机自动启动
[root@duanruhui ~]# systemctl enable --now mysqld
[root@duanruhui ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor prese>
Active: inactive (dead)
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
#确保3306端口已经监听起来
[root@duanruhui ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:* “
#在日志文件中找出临时密码
[root@duanruhui ~]# grep "password" /var/log/mysqld.log
2022-04-18T07:02:21.834459Z 1 [Note] A temporary password is generated for root@localhost: 6Iov(_Lh+<ld
#使用获取到的临时密码登录mysql
[root@duanruhui ~]# mysql -uroot -p"6Iov(_Lh+<ld"
或Enter password: //此处输入密码,可以直接复制你的密码粘贴至此处
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>
//看到有这样的标识符则表示成功登录了
//修改mysql登录密码
mysql> set password = password('HJLdrh123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
//为避免mysql自动升级,这里需要卸载最开始安装的yum源
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@duanruhui ~]# mysql -V
mysql Ver 14.14 Distrib 5.7.37, for Linux (x86_64) using EditLine wrapper
//客户机
mysql> GRANT all ON *.* TO'root'@'192.168.140.129' IDENTIFIED BY 'HJLdrh123!';//设置权限
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges;//刷新
Query OK, 0 rows affected (0.01 sec)
[root@duanruhui ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
//服务机
mysql> GRANT all ON *.* TO'root'@'192.168.140.130' IDENTIFIED BY 'HJLdrh123!';//设置权限
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges;//刷新
Query OK, 0 rows affected (0.01 sec)
[root@duanruhui ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
//让客户机访问服务机的mysql
[root@duanruhui ~]# mysql -uroot -p'HJLdrh123!' -h192.168.140.129
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 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>
[root@ser ~]# mysql -uroot -p'HJLdrh123!' -h192.168.140.130 -e'SHOW DATABASES'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
Navicat 与虚拟机相连接
mysql> GRANT all ON *.* TO'root'@'192.168.140.130' IDENTIFIED BY 'HJLdrh123!';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
//192.168.140.130是你vm8上的ip