第二十天
mysql基础
关系型数据库:
mysql 开源软件
Oracle 商业软件
mariadb 开源软件
SQL server
MSsql 微软数据库
nosql:数据是存放在内存上的
redis
memcache
mongodb
pgsql
sqlite 数据是存放在"文件"(数据文件中)sq1
1.1 数据结构模型
数据结构模型主要有:
层次模型
网状结构
关系模型:
二维关系:row,column
数据库管理系统:DBMS
关系:Relational,RDBMS
1.2 RDBMS专业名词
主键约束:一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。且必须提供数据,不能为空(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
//下载MySQL源码包
[root@fuwuduan ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
//升级源码包
[root@fuwuduan ~]# 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%]
//安装MySQL5.7
[root@fuwuduan ~]# yum -y install mysql-community-server mysql-community-client mysql-community-common mysql-community-devel --nogpgcheck
//如果报错,执行dnf module disable mysql关掉MySQL模块,再次安装
所有的匹配结果均已经被参数的模块化过滤条件筛除: mysql-community-server
所有的匹配结果均已经被参数的模块化过滤条件筛除: mysql-community-client
所有的匹配结果均已经被参数的模块化过滤条件筛除: mysql-community-common
所有的匹配结果均已经被参数的模块化过滤条件筛除: mysql-community-devel
错误:没有任何匹配: mysql-community-server mysql-community-client mysql-community-common mysql-community-devel
//设置启动并设置开机自启,查看3306端口是否起来
[root@fuwuduan ~]# systemctl enable --now mysqld
[root@fuwuduan ~]# ss -antl |grep 3306
LISTEN 0 80 *:3306 *:*
//在日志文件中寻找临时密码
[root@fuwuduan ~]# grep "password" /var/log/mysqld.log
2022-07-23T10:53:39.079565Z 1 [Note] A temporary password is generated for root@localhost: 1i6g24yNBf&b
//使用密码登录
[root@fuwuduan ~]# mysql -uroot -p
Enter password:
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>
//修改mysql登录密码
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.01 sec)
mysql> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
//退出测试,密码是否修改成功
[root@fuwuduan ~]# mysql -uroot -p123456
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.38 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源
[root@fuwuduan ~]# rpm -e mysql57-community-release
安装mariadb
[root@kehuduan ~]# dnf -y install mariadb*
[root@kehuduan ~]# systemctl start mariadb.service
[root@kehuduan ~]# ss -antl |grep 3306
LISTEN 0 80 0.0.0.0:3306 0.0.0.0:*
//设置密码
[root@kehuduan ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
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('123456');
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> exit
Bye
//退出测试密码是否成功
[root@kehuduan ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
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)]>