[MySQL Study Notes (1)] MySQL architecture, startup mode, C/S connection mode, and request processing process

This article is published by the official account [Developing Pigeon]! Welcome to follow! ! !


Old Rules-Sister Town House:

One. MYSQL introduction

(1) Architecture

        MYSQL adopts C/S architecture. The server program directly interacts with the stored data. Multiple client programs can connect to this server program, send requests for addition, deletion, modification, and check to the server, and then the server program processes the data.

(2) Start of the server program

1. UNIX-like systems

(1) mysqld

        The mysqld executable file represents the MYSQL server program. Running this executable file can start a MYSQL server process, which is not commonly used.

(2) mysqld_safe

        A startup script indirectly calls mysqld and continuously monitors the running status of the server, and outputs error information and other diagnostic information to the error log.

(3) mysqld.server

        A startup script will indirectly call mysqld_safe, and the server program can be started by adding the start parameter at the end:

mysqld.server start

        This file is actually a link file, and the corresponding actual file is.../supportfiles/mysql.server. You can also use this script to close the server program:

mysqld.server stop

(4) mysqld_multi

        Run multiple server instances, start or stop multiple server processes, and report their running status at the same time.

2. Windows system

(1) Manual start

        Click directly on the mysqld executable file.

(2) Start of service mode

        If you want to run a program for a long time, you can register it as a Windows service, managed by the operating system. The registration service is as follows:

“完整可执行文件路径” –install [-manual] [服务名]

        -manual means to start the service manually, the default service name is MYSQL. Next, you can start the MYSQL server service:

net start MYSQL

        Turn off the service:

net stop MYSQL

(3) Start the client program

        Through the mysql executable file, the client can interact with the server program, but need to pass some parameters, such as -h server program IP, -u login user name, -p the user's password, -P server program monitoring Note that there is no space between the password and p, otherwise an error will be reported.

(4) Connection between client and server

        The communication between the MYSQL client program and the server program is essentially an inter-process communication process.

1. TCP/IP

        The client and the server may be in different hosts, so they must communicate over the network. MySQL uses TCP as the network communication protocol. The server listens on port 3306 by default, and the port range is 0-65535.

2. Named pipes and shared memory

        Windows users can use named pipes and shared memory for inter-process communication.

        Named pipe startup: add the –enable-named-pipe parameter to the command to start the server program, and add the –pipe parameter to the startup client.

        Shared memory startup: the server program starts with the —shared-memory parameter, and the client program starts with the —protocol=memory parameter.

3. UNIX domain sockets

        C/S are running in Unix-like systems and can communicate using UNIX domain sockets. Specify the —protocol=socket parameter when starting the client. The Unix domain socket file that the server program listens to by default is /tmp/mysql.sock, and the client program also connects to this Unix domain socket file by default.


two. MySQL processing request process

(I. Overview

        When the server program processes the query request from the client, it is divided into three steps: connection management, analysis and optimization, and storage engine.

(Two) connection management

        Whenever a client process connects to the server process, the server process will create a thread to handle the interaction with the client. When the client exits, it will disconnect from the server, and the server will cache the thread and wait Another new client connection. The connection request sent by the client needs to be verified. If the authentication fails, the connection will be rejected. If the C/S runs on different machines, the connection will be encrypted using the Transport Layer Security (TLS) protocol.

(3) Analysis and optimization

1. Query cache

        When the MySQL server program processes a request, it will first query the previous cache. If there is the same request, it will be returned directly from the cache. This query cache can be shared among different clients. If there are any character differences in the query request, it will cause a cache miss, and if the query request contains certain system functions, user-defined variables and functions, and system tables, the request will not be cached. For example, the system function NOW queries the latest current time, the two calls of this function must be different results, so it will not be cached.

        The MySQL cache system monitors each table involved. As long as the structure or data of the table is modified, all related query caches will become invalid and deleted from the query cache. Although the query cache can improve system performance, there will be some overhead in order to maintain this cache. It is not recommended starting from MySQL 5.7.20, and it is deleted directly in 8.0.

2. Syntax analysis

        If the query cache is not hit, it will enter the formal query. The client sends a SQL statement. First, the statement must be analyzed to determine whether the grammar is correct. Then, the table to be queried from the text and various query conditions are extracted Put it on some data structures used internally by the MySQL server.

        This process is similar to the compiler's lexical analysis -> syntax analysis -> semantic analysis.

3. Query optimization

        After the syntax analysis, the server program obtains the required information, but in order to make the SQL statement query more efficient, it is necessary to optimize the SQL statement, such as converting the outer join to the inner join, simplifying the expression, and so on. The result of the optimization is to generate an execution plan, which indicates which indexes should be used to execute the query, and what is the order of connection between the tables. We can use the EXPLAIN statement to view the execution plan of a statement.

(4) Storage engine

        The MySQL server encapsulates data storage and extraction operations into the storage engine. The table is composed of rows of records. This is just a logical concept. The storage engine is responsible for the physical storage and reading of the real data. Different storage engines are responsible for different functions, and tables managed by different storage engines may have different storage structures and access algorithms.

        In order to facilitate management, the MySQL server processing request process is divided into the server layer and the storage engine layer. Various storage requests provide a unified calling interface for the server layer, which contains low-level functions for different purposes, such as reading the index first. Records and so on.

        After judging that a record meets the requirements, the server layer first sends it to a buffer, and then sends the real record to the client when the buffer is full.

(5) Commonly used storage engines

        MySQL's default storage engine before 5.5.5 was MyISAM, and then changed to InnoDB. InnoDB supports transactions, distributed transactions, and partial rollbacks of transactions, but most of these engines do not.

        Query the storage engine supported by the current server program:

SHOW ENGINES;

        We can set different storage engines for different tables, different tables can have different physical storage structures, and different read and write methods. Specify the engine directly when building the table:

CREATE TABLE()ENGINE =;

        Modify the storage engine of the table:

ALTER TABLE 表名 ENGINE=;

Guess you like

Origin blog.csdn.net/Mrwxxxx/article/details/113786698