MySQL数据库基础

MySQL数据库基础

1、数据库基本概念

1.1 数据库简介

数据库,简而言之可视为电子化的文件柜——存储电子文件的处所,用户可以对文件中的数据运行新增、截取、更新、删除等操作。所谓“数据库”系以一定方式储存在一起、能予多个用户共享、具有尽可能小的冗余度、与应用程序彼此独立的数据集合;一个数据库由多个表空间(Tablespace)构成。

1.2 数据库结构模型

数据库类型的区分主要参照的是数据结构模型,而常用的数据结构模型有很多:

  • 层次模型
  • 网状模型
  • 关系模型
1.3 关系型数据库

关系型数据库,是指采用了关系模型来组织数据的数据库,其以行和列(也就是表格)的形式存储数据,一组表组成了数据库。通俗来讲这种数据库就是由多张表组成,并且这些表之间存在一定的关系

关系型数据库的优点:

  • 数据以表格的形式存储容易理解
  • 支持SQL语言使用方便
  • 易于维护
  • 事务的一致性。

关系型数据库的缺点:

  • 为了维护一致性所付出的巨大代价就是其读写性能比较差;
  • 高并发读写效率较低;
  • 海量数据的读写效率低;
  • 固定的表结构。

关系型数据库:MySQL、Oracle、mariadb、Microsoft SQL 等。

非关系型数据库(NoSQL):redis、memcache、mongodb

2、数据库管理系统

数据库管理系统(Database Management System,简称DBMS)是为管理数据库而设计的电脑软件系统,一般具有存储、截取、安全保障、备份等基础功能。简单一句话,数据库管理系统是为了我们更方便的使用数据库而诞生的。

管理关系型数据库的系统叫做:关系型数据库管理系统(Relational Database Management System,简称RDBMS)。

常见的关系型数据库管理系统:

  • MySQL:MySQL,MariaDB,Percona-Server
  • PostgreSQL:简称为pgsql
  • Oracle
  • MSSQL
2.1 RDBMS术语

约束:constraint,向数据表提供的数据要遵守的限制

在我们开始学习MySQL 数据库前,让我们先了解下RDBMS的一些术语:

术语解释
SQLStructure Query Language,结构化查询语言
数据库数据库是一些关联表的集合
数据表表是数据的矩阵。在一个数据库中的表看起来像一个简单的电子表格
一列(数据元素) 包含了相同的数据, 例如邮政编码的数据。
一行(元组,或记录)是一组相关的数据,例如一条用户订阅的数据。
主键主键是唯一的;一个数据表中只能包含一个主键;主键约束的列的值必须是非空 + 唯一的。
唯一键约束一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行;允许为空(NULL)
外键外键用于关联两个表,链接两张表的对应关系。
索引使用索引可快速访问数据库表中的特定信息。索引是对数据库表中一列或多列的值进行排序的一种结构。
2.2 关系型数据库的常见组件

关系型数据库的常见组件有:

  • 数据库:database
  • 表:table,由行(row)和列(column)组成
  • 索引:index
  • 视图:view
  • 用户:user
  • 权限:privilege
  • 存储过程:procedure
  • 存储函数:function
  • 触发器:trigger
  • 事件调度器:event scheduler
2.3 SQL语句

SQL语句有三种类型:

  • 数据定义语言(DDL)

数据定义语言 (Data Definition Language, DDL) 是SQL语言集中,负责数据结构定义与数据库对象定义的语言

  • 数据操纵语言(DML)

数据操纵语言(Data Manipulation Language, DML)是SQL语言中,负责对数据库对象运行数据访问工作的指令集

  • 数据控制语言(DCL)

数据控制语言 (Data Control Language) 在SQL语言中,是一种可对数据访问权进行控制的指令,它可以控制特定用户账户对数据表、查看表、预存程序、用户自定义函数等数据库对象的控制权

SQL语句类型对应操作
DDLCREATE:创建
DROP:删除
ALTER:修改
DMLINSERT:向表中插入数据
DELETE:删除表中数剧
UPDATE:更新表中数据
SELECT:查询表中数据
DCLGRANT:授权
REVOKE:移除授权
DQLFROM:什么表
WHERE:查找限制条件

3、mysql安装与配置

3.1 mysql安装

mysql安装方式有三种:

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

配置mysql的yum源

a).下载mysql的yum repository

[root@localhost ~]#wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

b).更新下载好的yum repository

[root@localhost ~]# rpm -Uvh mysql57-community-release-el7-11.noarch.rpm 

c).查看/etc/yum.repos.d/下多了两个mysql的repo

[root@localhost ~]# ls /etc/yum.repos.d/
centos8.repo  mysql-community.repo  mysql-community-source.repo

d).开始安装mysql

[root@localhost ~]# yum -y install mysql-community-server mysql-community-client mysql-community-common mysql-community-devel --nogpgcheck

报错信息

All matches were filtered out by modular filtering for argument: mysql-community-server
All matches were filtered out by modular filtering for argument: mysql-community-client
All matches were filtered out by modular filtering for argument: mysql-community-common
All matches were filtered out by modular filtering for argument: mysql-community-devel
Error: Unable to find a match: mysql-community-server mysql-community-client mysql-community-common mysql-community-devel

网上查了各种都是这个安装顺序,都可以提示成功,摸不着头脑,于是去mysql官网去找文档,发现了重点

在这里插入图片描述

解决方案

##安装mysql之前要先禁用默认的mysql模块
[root@localhost ~]# yum module disable mysql
3.2 mysql配置

启动mysql并设置开机自动启动

[root@localhost ~]# systemctl enable --now mysqld

确保3306端口已经监听起来

[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2022-07-23 15:21:28 CST; 22s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 17746 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD>
  Process: 17697 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 17749 (mysqld)
    Tasks: 27 (limit: 11202)
   Memory: 306.0M
   CGroup: /system.slice/mysqld.service
           └─17749 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Jul 23 15:21:25 localhost.localdomain systemd[1]: Starting MySQL Server...
Jul 23 15:21:28 localhost.localdomain systemd[1]: Started MySQL Server.
[root@localhost ~]# 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@localhost ~]# grep "password" /var/log/mysqld.log
2022-07-23T07:21:26.283563Z 1 [Note] A temporary password is generated for root@localhost: >Hb%aaBqa0yI

使用获取到的临时密码登录mysql

[root@localhost ~]# 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> alter user 'root'@'localhost' identified by 'Passwd123!';
Query OK, 0 rows affected (0.00 sec)

注意

默认情况下安装validate_password。实现的默认密码策略要求密码至少包含一个大写字母、一个小写字母、一个数字和一个特殊字符,并且总密码长度至少为 8 个字符。

使用设置的新密码登录mysql

[root@localhost ~]# mysql -uroot -pPasswd123!
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.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@localhost ~]# rpm -e mysql57-community-release

4、mariadb安装与配置

4.1 mariadb安装
[root@localhost ~]# dnf install -y mariadb*
4.2 mariadb配置

启动mariadb并设置开机自动启动

[root@localhost ~]# systemctl enable --now mariadb
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.

进入mariadb数据库中并设置密码

mariadb启动后不需要密码可直接进入

[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
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('Passwd123!');
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> exit
Bye

使用设置的新密码登录mariadb

[root@localhost ~]# mysql -uroot -pPasswd123!
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)]> 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值