Table of contents
The execution flow of a SQL statement:
Installation and deployment of MySQL in Linux
First you need to download the mysql package:
Introduction to MySQL
MySQL is a small relational database management system developed by the Swedish company MySQL AB. On January 16, 2008, it was acquired by Sun for US$1 billion. In 2009, SUN was acquired by Oracle for $7.4 billion.
At present, MySQL is widely used in small and medium-sized websites on the Internet. Due to its small size, fast speed, low total cost of ownership, and especially the open source code, many small and medium-sized websites choose MySQL as their website database in order to reduce their total cost of ownership.
MySQL characteristics
1. It is written in c and c++, and tested with a variety of compilers to ensure the portability of the source code. (High operating efficiency (closer to the bottom layer))
2. Support multiple operating systems
3. APIs are provided for multiple programming languages.
4. Support multi-threading, make full use of CPU resources, and support multi-users
5. Optimize the SQL query algorithm to effectively improve the query speed.
6. It can be used as a separate application program in the client-server network environment, or it can be embedded in other software as a library
7. Provide multi-language support
8. Prompt TCP/IP, ODBC, IDBC and other database connection methods
9. Provide management tools for managing, testing, and optimizing database operations
10. Can handle large databases with tens of millions of records
MySQL architecture
Mysql is composed of SQL interface, parser, optimizer, cache, and storage engine;
Connectors: Interaction with different SQL in different languages
Management Serveices & Utilities: System management and control tools
Connection Pool: connection pool, management of buffered user connections, thread processing and other requirements that require caching
SQL Interface: SQL interface, which receives the user's SQL command and returns the result that the user needs to query
Parser: Parser, when the SQL command is passed to the parser, it will be verified and parsed by the parser
Optimizer: query optimizer, the SQL statement will use the query optimizer to optimize the query before querying
Cache and Buffer: query cache. If the query cache has a hit query result, the query statement can directly go to the query cache to fetch data
Engine: storage engine, the storage engine is the subsystem in Mysql that specifically deals with files
The four levels of SQL:
Connection layer:
Idea: In order to solve the problems caused by the frequent allocation and release of resources, a buffer pool is established for the database.
Principle: Put a certain number of connections in the buffer pool in advance. When a database connection is needed, you only need to take one out of the "buffer pool" and put it back after use.
Function: perform authentication, thread reuse, connection limit, check memory, data cache; manage user connection, thread processing and other requirements that require caching
SQL layer:
Management Services and Tools Components: Manage databases from security, replication, clustering, administration, configuration, migration, and metadata for backup and recovery
sql interface component: perform operations and management of DML, DDL, stored procedures, views, triggers, etc.; user SQL command interface
Parser component: Validate and parse SQL commands
The query optimizer component optimizes SQL statement queries, "selection, projection and connection"
Cache and buffer: composed of a series of small caches, such as table cache, record cache, key cache, permission cache, etc.
Pluggable storage engine:
Storage engine: Mysql's storage engine is a plug-in type. It is an access mechanism customized according to an abstract interface of the file access layer provided by Mysql AB. This access mechanism is called a storage engine;
MYISAM
The query speed is fast, with better index optimization and data compression technology, but does not support transactions
InnoDB
It supports transactions and provides row-level locking, which is widely used (the default mode)
BDB: BDB is no longer supported from the ground up
Memory: suitable for storing temporary data
Archive: suitable for storing historical data
Physical file layer:
Supported file types: EXT3, 4, NTFS, NFS
File content: data files, log files, configuration files, other files
The execution flow of a SQL statement:
Connection layer:
(1) Provide connection protocols: TCP/IP, SOCKET
(2) Provide authentication: user, password, IP, SOCKET
(3) Provide a dedicated connection thread: receive user SQL and return the result
You can use this statement to view the basic situation of the connection thread:
mysql> show processlist;
SQL layer:
(1) Receive the SQL statement transmitted by the upper layer
(2) Grammar verification module: verify the syntax of the statement, whether it satisfies SQL_MOOE
(3) Semantic detection: determine the type of SQL statement
- DDL: Data Definition Language
- DCL: Data Control Language
- DML: Data Manipulation Language
- DQL: Data Query Language
(4) Permission check: Does the user have permission to the database table?
(5) Parser: Before the statement is executed, pre-process it to generate a parse tree (execution plan), that is, generate a variety of execution plans
(6) Optimizer: According to the various execution plans obtained by the parser, make judgments and select the optimal execution plan
Cost model: the consumption of resources and the quality of library performance
(7) Executor: Execute SQL statements according to the optimal execution plan and generate execution results
(8) Provide query cache (closed by default), and use redisrair instead of query cache function
(9) Provide log records (log management chapter): binlog, which is not enabled by default
Storage engine layer: (similar to the file system in Linux)
Responsible for fetching data from the disk according to the results of the execution of the SQL layer
Hand over the hexadecimal disk data to SQL to structure it into a table
The connection layer's dedicated thread returns to the user.
Installation and deployment of MySQL in Linux
Install using warehouse
No matter which method is used to install, first we need to get the mysql installation package:
mysql official website: www.mysql.com
First you need to download the mysql package:
1. Enter MySQL official website
2. Enter the MySQL community version download
3. Use yum to download MySQL
4. Download the corresponding version of the software package
5. Here you can download the software package directly without logging in
6. Copy the download link
7. Then enter the linux command line and use the command to download the software package:
rpm -i https://repo.mysql.com//mysql80-community-release-el9-1.noarch.rpm
警告:/var/tmp/rpm-tmp.soD57S: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
8. Then install mysql-server
yum install my-server -y
9. Here you can view all mysql packages:
rpm -aq | grep mysql
Note: It is correct if it shows 7 packages
10. Start mysql service
systemctl start mysqld
11. View the status of the service
systemctl statsus mysqld
It shows that the service is running normally, which means that the sql service has been successfully started.
If the above are normal, we can use mysql normally
12. Note that before entering, we can query the default password for logging in to mysql:
sudo grep 'temporary password' /var/log/mysqld.log
2023-07-05T13:57:47.521864Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: iOMZ>nuaf9oq(最后这里就是默认密码)
The result of this command will display the default password, which will be used in the next step
13. At this time, you can try to log in to MySQL
mysql -u root -p
Enter password: (这里需要输入密码就输入刚才查找到的默认密码)
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.33 MySQL Community Server - GPL
Copyright (c) 2000, 2023, 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>
13. Then we can enter sql normally, but when we try to check the database, an error will be reported
Because we need to change the password:
ALTER USER 'root'@'localhost' IDENTIFIED BY '修改后的密码';
Note: The password here must contain letters, uppercase and lowercase + numbers + special characters
14. At this time, we can query the database normally
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.15 sec)
mysql>
At this point, the database installation is complete, and we can use MySQL happily!