MySQL客户端与服务器模型

一、MySQL客户端与服务器模型

  1、mysql是一个典型的C/S服务结构

  

    1.1 mysql自带的客户端程序(/application/mysql/bin)

      mysql

      mysqladmin

      mysqldump

    1.2 mysqld一个二进制程序,后台的守护进程

二、应用程序连接MySQL方式

  1、TCP/IP连接方式

  

   2、套接字的连接方式

  

 三、查看连接状态

  1、查看命令

复制代码

[root@computer ~]# mysql -uroot -p123456
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 2
Server version: 5.6.40 Source distribution

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> status
--------------
mysql  Ver 14.14 Distrib 5.6.40, for Linux (x86_64) using  EditLine wrapper

Connection id:        2
Current database:    
Current user:        root@localhost
SSL:            Not in use
Current pager:        stdout
Using outfile:        ''
Using delimiter:    ;
Server version:        5.6.40 Source distribution
Protocol version:    10
Connection:        Localhost via UNIX socket
Server characterset:    utf8
Db     characterset:    utf8
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:        /application/mysql-5.5.40/tmp/mysql.sock
Uptime:            11 sec

Threads: 1  Questions: 5  Slow queries: 0  Opens: 67  Flush tables: 1  Open tables: 60  Queries per second avg: 0.454
--------------

mysql> 

复制代码

  2、总结

[root@computer ~]# mysql -uroot -p123456 -hlocalhost         socket连接
[root@computer ~]# mysql -uroot -p123456 -h127.0.0.1         TCP/IP连接
[root@computer ~]# mysql -uroot -p123456 -S /application/mysql/tmp/mysql.sock  -hlocalhost   socket连接
[root@computer ~]# mysql -uroot -p123456 -h127.0.0.1 -P3306

  socke链接更快,本地默认使用socket连接,socket连接只能用于本地连接,TCP/IP连接要建立三次握手。

三、MySQL服务器构成

  1、什么是实例

    MySQL的后台进程+线程+预分配的内存结构。

    MySQL在启动的过程中会启动后台守护进程,并生成工作线程,预分配内存结构供MySQL处理数据使用。

  

                  图1.1-word的打开方式  

  

                   图1.2-mysqld的打开方式

  2、MySQLD服务器程序构成

  

   3、mysqld是一个守护进程但是本身不能自主启动:

复制代码

[root@computer ~]# mysql -uroot -p123456 -h127.0.0.1 -P3306
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 7
Server version: 5.6.40 Source distribution

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> select user,host,password from mysql.user;

复制代码

  4、连接层

    1、提供连接协议(socket、tcp/ip)

    2、验证用户的合法性(用户名、密码、白名单)

    3、提供一个专用连接线程(接收SQL、返回结果),将SQL语句交给SQL层继续处理

  

   5、SQL层

    1、接收到SQL语句,语法判断。

    2、判断语义(判断语句类型:DML、DDL、DCL、DQL)

    3、解析SQL语句,生成多种执行计划

    4、优化器,选择他认为成本最低的执行计划。

    5、执行器根据优化器的选择,按照优化器建议执行SQL语句,得到去哪儿找SQL语句需要访问的数据

      5.1 具体:在哪个数据文件上的哪个数据页中?

      5.2 将以上结果充送给下层继续处理

    6、接收存储引擎层的数据,结构化成表的形式,通过连接层提供的专用线程,将表数据返回给用户。

    7、提供查询缓存

      7.1 query_cache, 使用memcache 或者redis 替代

    8、日志记录(binlog)

  6、存储引擎层

    1、接收上层的执行结果

    2、取出磁盘文件和相应数据

    3、返回给SQL层,结构化之后生成表格,由专用线程返回给客户端

 四、MySQL的结构

  MySQL的逻辑结构(熟悉)

    MySQL的逻辑对象:做为管理人员或者开发人员操作的对象

    1、库

    2、表:元数据+真实数据行

    3、元数据:列+其它属性(行数+占用空间大小+权限)

    4、列:列名字+数据类型+其他约束(非空、唯一、主键、非负数、自增长、默认值)

  最直观的数据:二维表,必须用库来存放
        

  MySQL逻辑结构与Linux系统对比

MySQL Linux
目录
show databases; ls-l /
use mysql cd /mysql
文件
show tables; ls
二维表=元数据+真实数据行 文件=文件名+文件属性

  MySQL的物理结构(了解)

    1)MySQL的最底层的物理结构是数据文件,也就是说,存储引擎层,打交道的文件,是数据文件。

    2)存储引擎分为很多种类(Linux中的FS)

    3)不同存储引擎的区别:存储方式、安全性、性能

  myisam:
        

  innodb:
        

段、区、页(块)

  1、段:理论上一个表就是一个段,由多个区构成,(分区表是一个分区一个段)

  2、区:连续的多个页构成

  3、页:最小的数据存储单元,默认是16k

  

       注意:本文有不理解之处,加QQ:1300536862,探讨

        推荐:需要租用云服务器,域名等其他服务的可以扫描下方二维码,云服务器免费领取使用,华为云服务

        

猜你喜欢

转载自blog.csdn.net/qq_33648367/article/details/112648347