MySQL 1

本文介绍了数据库的基础知识,包括关系型数据库如MySQL、MariaDB和Oracle,以及非关系型数据库如Redis和MongoDB。重点讲述了关系型数据库的关系模型、专业名词和常见组件,并详细说明了SQL语句的种类。此外,还提供了MySQL和MariaDB的安装步骤,以及如何修改初始密码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据库介绍

关系型数据库(数据存放在硬盘):

  • MySQL 5.7 及之前的版本,开源软件 → 8.0 商业软件
  • Oracle(甲骨文) 商业软件
  • MariaDB 开源软件
  • SqlServer
  • msSQL 微软

非关系型数据库(数据存放在内存,变量):

  • Redis
  • MemCache
  • MongoDB
  • PostgreSQL

其它(数据存放在文件中,由 sql 语句处理):

  • SQLite

关系型数据库介绍

数据结构模型

数据结构模型主要有:

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

关系模型:
二维关系: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:修改
DMLINSERT:向表中插入数据
DELETE:删除表中数据
UPDATE:更新表中数据
SELECT:查询表中数据
DCLGRANT:授权
REVOKE:移除授权

mysql 安装

mysql 安装方式有三种:

  • 源代码:编译安装
  • 二进制格式的程序包:展开至特定路径,并经过简单配置后即可使用
  • 程序包管理器管理的程序包:
    • rpm:有两种
      • OS Vendor:操作系统发行商提供的
      • 项目官方提供的
    • deb
//配置mysql的yum源
[root@localhost ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
[root@localhost ~]# rpm -Uvh mysql57-community-release-el7-11.noarch.rpm

//禁用mysql模块
[root@localhost ~]# yum module disable mysql
Last metadata expiration check: 0:03:50 ago on Sun 24 Jul 2022 05:52:57 PM CST.
Dependencies resolved.
===================================================================================
 Package            Architecture      Version             Repository          Size
===================================================================================
Disabling modules:
 mysql                                                                            

Transaction Summary
===================================================================================

Is this ok [y/N]: y
Complete!

//安装mysql
[root@localhost ~]# dnf -y install mysql-community-server mysql-community-client  mysql-community-common mysql-community-devel --nogpgcheck

//开启mysql服务,并设置开机自启
[root@localhost ~]# systemctl enable --now mysqld
[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset:>
   Active: active (running) since Sun 2022-07-24 18:03:55 CST; 30s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 14213 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld>
  Process: 14164 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/S>
 Main PID: 14216 (mysqld)
    Tasks: 27 (limit: 11175)
   Memory: 309.8M
   CGroup: /system.slice/mysqld.service
           └─14216 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.>

Jul 24 18:03:53 localhost.localdomain systemd[1]: Starting MySQL Server...
Jul 24 18:03:55 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-24T10:03:54.185851Z 1 [Note] A temporary password is generated for root@localhost: pBeikU#kN0MY
[root@localhost ~]# mysql -uroot -ppBeikU#kN0MY

//修改密码
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.00 sec)

mysql> alter user 'root'@'localhost' identified by 'skye123!';
Query OK, 0 rows affected (0.00 sec)

//数据库的基本使用
mysql> show databases;      //查找数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> use mysql;       //使用数据库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;     //查找表格
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| engine_cost               |
| event                     |
| func                      |
| general_log               |
| gtid_executed             |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| server_cost               |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
31 rows in set (0.00 sec)

mysql> exit     //退出
Bye

//为避免mysql自动升级,需卸载最开始安装的yum源
[root@localhost ~]# rpm -e mysql57-community-release

MariaDB 安装

//安装mariadb
[root@localhost ~]# dnf -y install 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.
[root@localhost ~]# ss -antl
State   Recv-Q  Send-Q   Local Address:Port     Peer Address:Port  Process  
LISTEN  0       80             0.0.0.0:3306          0.0.0.0:*              
LISTEN  0       128            0.0.0.0:22            0.0.0.0:*              
LISTEN  0       128               [::]:22               [::]:*

//启动
[root@localhost ~]# 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('skye123!');
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> exit
Bye

[root@localhost ~]# mysql -uroot -pskye123!
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
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)]> 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值