Mysql基础

Mysql安装

目录

Mysql安装

关系型数据库介绍

数据结构模

RDBMS专业名词

关系型数据库的常见组件

SQL语句

mysql安装与配置

mysql安装

mysql配置

mysql第二种安装

mysql工具使用

服务器监听的两种socket地址

mysql数据库操作

DDL操作

mysql数据类型

日期和时间类型

字符串类型



关系型数据库介绍

数据结构模

数据结构模型主要有:

  • 层次模型

  • 网状结构

  • 关系模型

关系模型: 二维关系: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语句类型对应操作
DDLCREATE:创建 DROP:删除 ALTER:修改
DMLINSERT:向表中插入数据 DELETE:删除表中数据 UPDATE:更新表中数据 SELECT:查询表中数据
DCLGRANT:授权 REVOKE:移除授权

mysql安装与配置

mysql安装

mysql安装方式有三种:

  • 源代码:编译安装

  • 二进制格式的程序包:展开至特定路径,并经过简单配置后即可使用

  • 程序包管理器管理的程序包:

    • rpm:有两种

      • OS Vendor:操作系统发行商提供的

      • 项目官方提供的

    • deb

 #配置mysql的yum源
 [root@localhost ~]# wget https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
 [root@localhost ~]# ls
 anaconda-ks.cfg  mysql80-community-release-el8-4.noarch.rpm  to.sh
 [root@localhost ~]# 
 #安装过这个rpm包
 [root@localhost ~]# rpm -ivh mysql80-community-release-el8-4.noarch.rpm 
 warning: mysql80-community-release-el8-4.noarch.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY
 Verifying...                          ################################# [100%]
 Preparing...                          ################################# [100%]
 Updating / installing...
    1:mysql80-community-release-el8-4  ################################# [100%]
 [root@localhost ~]# 
 #安装mysql
 [root@localhost ~]# yum -y install mysql-community-server mysql-community-client  \
 > mysql-community-common mysql-community-devel
 ​

mysql配置

 #启动mysql并设置开机自动启动
 [root@localhost ~]# systemctl enable --now mysqld
 [root@localhost ~]# systemctl status mysqld
 ​
 #确保3306端口已经监听起来
 [root@localhost ~]# ss -antl
 ​
 #在日志文件中找出临时密码
 [root@localhost ~]# grep "password" /var/log/mysqld.log
 ​
 #使用获取到的临时密码登录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.23
 ​
 Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
 ​
 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 'wangqing123!';
 Query OK, 0 rows affected (0.00 sec)
 ​
 mysql> quit
 Bye
 ​
 ​
 ​
 //为避免mysql自动升级,这里需要卸载最开始安装的yum源
 rpm -e mysql57-community-release

mysql第二种安装

 [root@localhost ~]# yum -y install maridb*
 ----------
 [root@localhost ~]# systemctl enable --now  mariadb
 --------
 [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(1)
     -> ;
 ​
 [root@localhost ~]# mysql -u root -p1
 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)]> show databases
     -> ;
 +--------------------+
 | Database           |
 +--------------------+
 | information_schema |
 | mysql              |
 | performance_schema |
 +--------------------+
 3 rows in set (0.000 sec)
 ​

mysql工具使用

//语法:mysql [OPTIONS] [database]

//常用的OPTIONS: -uUSERNAME //指定用户名,默认为root -hHOST //指定服务器主机,默认为localhost,推荐使用ip地址 -pPASSWORD //指定用户的密码 -P# //指定数据库监听的端口,这里的#需用实际的端口号代替,如P3307 -V //查看当前使用的mysql版本 -e //不登录mysql执行sql语句后退出,常用于脚本

 [root@localhost ~]# mysql -V
 mysql  Ver 15.1 Distrib 10.3.28-MariaDB, for Linux (x86_64) using readline 5.1
 [root@localhost ~]# 
 ​
 [root@localhost ~]# mysql -u root -p1
 Welcome to the MariaDB monitor.  Commands end with ; or \g.
 Your MariaDB connection id is 11
 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)]> 
 MariaDB [(none)]> show databases;
 +--------------------+
 | Database           |
 +--------------------+
 | information_schema |
 | mysql              |
 | performance_schema |
 +--------------------+
 3 rows in set (0.000 sec)
 ​
 MariaDB [(none)]> 

服务器监听的两种socket地址

SOCKET类型说明
ip socket默认监听在tcp的3306端口,支持远程通信
unix sock监听在sock文件上(/tmp/mysql.sock,/var/lib/mysql/mysql.sock) 仅支持本地通信 server地址只能是:localhost,127.0.0.1

mysql数据库操作

DDL操作

数据库操作

 //创建数据库
 //语法:create database [  ] ‘db_name’;
 # MariaDB [(none)]> CREATE DATABASE tang  DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
 # 这是创建utf8格式数据库的格式,通常如果需要输入中文都需要使用utf8格式。
 Query OK, 1 row affected (0.000 sec)
 ​
 MariaDB [(none)]> show databases;
 +--------------------+
 | Database           |
 +--------------------+
 | information_schema |
 | mysql              |
 | performance_schema |
 | tang               |
 +--------------------+
 4 rows in set (0.000 sec)
 ​
 MariaDB [(none)]> 
 ​

表操作

 MariaDB [tang]> create table tang (id int not null,name varchar(10) ,age int not null);
 Query OK, 0 rows affected (0.005 sec)
 ​
 MariaDB [tang]> show tables;
 +----------------+
 | Tables_in_tang |
 +----------------+
 | tang           |
 +----------------+
 1 row in set (0.000 sec)
 MariaDB [tang]> desc tang
     -> ;
 +-------+-------------+------+-----+---------+-------+
 | Field | Type        | Null | Key | Default | Extra |
 +-------+-------------+------+-----+---------+-------+
 | id    | int(11)     | NO   |     | NULL    |       |
 | name  | varchar(10) | YES  |     | NULL    |       |
 | age   | int(11)     | NO   |     | NULL    |       |
 +-------+-------------+------+-----+---------+-------+
 3 rows in set (0.002 sec)
 ​
 MariaDB [tang]> 
 ​

用户操作

mysql用户帐号由两部分组成,如'USERNAME'@'HOST',表示此USERNAME只能从此HOST上远程登录

这里('USERNAME'@'HOST')的HOST用于限制此用户可通过哪些主机远程连接mysql程序,其值可为:

  • IP地址,如:172.16.12.129

  • 通配符

    • %:匹配任意长度的任意字符,常用于设置允许从任何主机登录

    • _:匹配任意单个字符

 /数据库用户创建
 //语法:CREATE USER 'username'@'host' [IDENTIFIED BY 'password'];
 MariaDB [mysql]> create user 'tangyuxuan'@'192.168.245.128' identified by '1';
 Query OK, 0 rows affected (0.000 sec)
 ​
 MariaDB [mysql]> exit
 Bye
 [root@localhost ~]# mysql -utangyuxuan -p -h192.168.245.128
 Enter password: 
 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)]> 
 #删除用户
 MariaDB [(none)]> drop user 'tangyuxuan'@192.168.245.128;
 Query OK, 0 rows affected (0.000 sec)
 ​
 ​

查看命令SHOW

 ​
 MariaDB [(none)]> show character set;
 #查看支持的所有字符集
 +----------+-----------------------------+---------------------+--------+
 | Charset  | Description                 | Default collation   | Maxlen |
 +----------+-----------------------------+---------------------+--------+
 | big5     | Big5 Traditional Chinese    | big5_chinese_ci     |      2 |
 | dec8     | DEC West European           | dec8_swedish_ci     |      1 |
 ---------------
 | geostd8  | GEOSTD8 Georgian            | geostd8_general_ci  |      1 |
 | cp932    | SJIS for Windows Japanese   | cp932_japanese_ci   |      2 |
 | eucjpms  | UJIS for Windows Japanese   | eucjpms_japanese_ci |      3 |
 +----------+-----------------------------+---------------------+--------+
 40 rows in set (0.000 sec)
 ​
 ​
 ariaDB [(none)]> show character set;
 #查看存储引擎
 +----------+-----------------------------+---------------------+--------+
 | Charset  | Description                 | Default collation   | Maxlen |
 +----------+-----------------------------+---------------------+--------+
 | big5     | Big5 Traditional Chinese    | big5_chinese_ci     |      2 |
 | dec8     | DEC West European           | dec8_swedish_ci     |      1 |
 | cp850    | DOS West European           | cp850_general_ci    |      1 |
 ----------------------------------------------------      |
 +--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
 12 rows in set (0.000 sec)
 ​
 ​
 MariaDB [(none)]> show tables from tang;    
 #不进入数据库查看所有表
 +----------------+
 | Tables_in_tang |
 +----------------+
 | tang           |
 +----------------+
 1 row in set (0.000 sec)
 ​
 MariaDB [(none)]> show databases;
 #查看数据库信息
 +--------------------+
 | Database           |
 +--------------------+
 | information_schema |
 | mysql              |
 | performance_schema |
 | tang               |
 +--------------------+
 4 rows in set (0.000 sec)
 ​
 MariaDB [(none)]> 
 ​
 MariaDB [(none)]> desc tang.tang
     -> ;
     #查看表结构
 +-------+-------------+------+-----+---------+-------+
 | Field | Type        | Null | Key | Default | Extra |
 +-------+-------------+------+-----+---------+-------+
 | id    | int(11)     | NO   |     | NULL    |       |
 | name  | varchar(10) | YES  |     | NULL    |       |
 | age   | int(11)     | NO   |     | NULL    |       |
 +-------+-------------+------+-----+---------+-------+
 3 rows in set (0.001 sec)
 ​
 MariaDB [(none)]> 
 ​
 MariaDB [(none)]> show create table tang.tang
 ​
 -> ;
 #查看某表的创建命令
 +-------+-------------------------------------------------------------------------------------------------------------------------------------------------+
 | Table | Create Table                                                                                                                                    |
 +-------+-------------------------------------------------------------------------------------------------------------------------------------------------+
 | tang  | CREATE TABLE `tang` (
   `id` int(11) NOT NULL,
   `name` varchar(10) DEFAULT NULL,
   `age` int(11) NOT NULL
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
 +-------+-------------------------------------------------------------------------------------------------------------------------------------------------+
 1 row in set (0.000 sec)
 ​
 MariaDB [(none)]> 
 ​
 MariaDB [(none)]> use tang
 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
 MariaDB [tang]> show table status like 'tang'\G
 #查看tang表的状态
 *************************** 1. row ***************************
             Name: tang
           Engine: InnoDB
          Version: 10
       Row_format: Dynamic
             Rows: 0
   Avg_row_length: 0
      Data_length: 16384
  Max_data_length: 0
     Index_length: 0
        Data_free: 0
   Auto_increment: NULL
      Create_time: 2022-07-25 13:19:35
      Update_time: NULL
       Check_time: NULL
        Collation: utf8_general_ci
         Checksum: NULL
   Create_options: 
          Comment: 
 Max_index_length: 0
        Temporary: N
 1 row in set (0.001 sec)
 ​
 MariaDB [tang]> 
 ​

获取帮助

 语法:help keyword;
 ​
 MariaDB [tang]> help create table;
 Name: 'CREATE TABLE'
 Description:
 Syntax:
 CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
     (create_definition,...)
     [table_options]
     [partition_options]
 ------------------------------

mysql数据类型

MySQL中定义数据字段的类型对数据库的优化非常重要。mysql支持多种类型,大致分为三类:数值,日期,时间和字符串类型。

数值类型

mysql支持所有标准sql数值数据类型。这些类型包括严格数值数据类型(INTEGER、SMALLINT、DECIMAL和NUMERIC),以及近似数值数据类型(FLOAT、REAL和DOUBLE PRECISION)。

关键字INT是INTEGER的同义词,关键字DEC是DECIMAL的同义词。BIT数据类型保存位字段值,并且支持MyISAM、MEMORY、InnoDB和BDB表。作为SQL标准的扩展,MySQL也支持整数类型TINYINT、MEDIUMINT和BIGINT。下面的表显示了需要的每个整数类型的存储和范围

类型大小范围(有符号)范围(无符号)
TINYINT1 byte(-128,127)(0,255)
SMALLINT2 bytes(-32 768,32 767)(0,65 535)
MEDIUMINT3 bytes(-8 388 608,8 388 607)(0,16 777 215)
INT或INTEGER4 bytes(-2 147 483 648,2 147 483 647)(0,4 294 967 295)
BIGINT8 bytes(-9,223,372,036,854,775,808,9 223 372 036 854 775 807)(0,18 446 744 073 709 551 615)
FLOAT4 bytes(-3.402 823 466 E+38,-1.175 494 351 E-38),0,(1.175 494 351 E-38,3.402 823 466 351 E+38)0,(1.175 494 351 E-38,3.402 823 466 E+38)
DOUBLE8 bytes(-1.797 693 134 862 315 7 E+308,-2.225 073 858 507 201 4 E-308),0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308)0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308)
DECIMAL对DECIMAL(M,D) ,如果M>D,为M+2否则为D+2依赖于M和D的值依赖于M和D的值

日期和时间类型

表示时间值的日期和时间类型为DATETIME、DATE、TIMESTAMP、TIME和YEAR。每个时间类型有一个有效值范围和一个"零"值,当指定不合法的MySQL不能表示的值时使用"零"值。TIMESTAMP类型有专有的自动更新特性,将在后面描述。

类型大小( BYTES)范围格式用途
DATE31000-01-01/9999-12-31YYYY-MM-DD日期值
TIME3'-838:59:59'/'838:59:59'HH:MM:SS时间值或持续时间
YEAR11901/2155YYYY年份值
DATETIME81000-01-01 00:00:00/9999-12-31 23:59:59YYYY-MM-DD HH:MM:SS混合日期和时间值
TIMESTAMP41970-01-01 00:00:00/2038 结束时间是第 2147483647 秒,北京时间 2038-1-19 11:14:07,格林尼治时间 2038年1月19日 凌晨 03:14:07YYYYMMDD HHMMSS混合日期和时间值,时间戳

字符串类型

字符串类型指CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT、ENUM和SET。该节描述了这些类型如何工作以及如何在查询中使用这些类型。

类型大小用途
CHAR0-255 bytes定长字符串
VARCHAR0-65535 bytes变长字符串
TINYBLOB0-255 bytes不超过 255 个字符的二进制字符串
TINYTEXT0-255 bytes短文本字符串
BLOB0-65 535 bytes二进制形式的长文本数据
TEXT0-65 535 bytes长文本数据
MEDIUMBLOB0-16 777 215 bytes二进制形式的中等长度文本数据
MEDIUMTEXT0-16 777 215 bytes中等长度文本数据
LONGBLOB0-4 294 967 295 bytes二进制形式的极大文本数据

注意:char(n) 和 varchar(n) 中括号中 n 代表字符的个数,并不代表字节个数,比如 CHAR(30) 就可以存储 30 个字符。

CHAR 和 VARCHAR 类型类似,但它们保存和检索的方式不同。它们的最大长度和是否尾部空格被保留等方面也不同。在存储或检索过程中不进行大小写转换。

BINARY 和 VARBINARY 类似于 CHAR 和 VARCHAR,不同的是它们包含二进制字符串而不要非二进制字符串。也就是说,它们包含字节字符串而不是字符字符串。这说明它们没有字符集,并且排序和比较基于列值字节的数值值。

BLOB 是一个二进制大对象,可以容纳可变数量的数据。有 4 种 BLOB 类型:TINYBLOB、BLOB、MEDIUMBLOB 和 LONGBLOB。它们区别在于可容纳存储范围不同。

有 4 种 TEXT 类型:TINYTEXT、TEXT、MEDIUMTEXT 和 LONGTEXT。对应的这 4 种 BLOB 类型,可存储的最大长度不同,可根据实际情况选择。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值