MySQL - Section 1 - MySQL Database Basics

Table of contents

1. The concept of database

2. Mainstream database

3. Basic use

3.1. Connect to the server

3.2. Server Management

3.3. Database server, database, table relationship

3.4. Use cases

3.4.1. Data storage path

3.4.2. Create/delete database

3.4.3. Using the database

3.4.4. Create database table

3.4.5. Insert data into the table

3.4.6. Query the data in the table

3.5. Data logic storage

4.MySQL architecture

5. SQL classification

6. Storage engine

6.1. Storage engine concept

6.2. View storage engine

6.3. Comparison of storage engines


1. The concept of database

The narrow concept of database:

• We collectively call the database client mysql, the database server mysqld, and all the data in the database data storage directory (the default directory is /var/lib/mysql) together as the database.

• That is to say database=mysql+mysqld+data.

The broad concept of database:

A software collection solution that provides more convenient data access services is called a database.

A database is a warehouse that organizes, stores, and manages data according to the data structure. It is a collection of large amounts of data that is organized, shared, and managed in a computer for a long time.

Although the simple use of files can also store data, but there will be the following disadvantages:

• Security issues: Data cannot be rolled back after mishandling.
• Not conducive to data query and management: the stored data is not organized in a certain data structure.
• Inconvenient control: data control needs to be done by users themselves.
• Not conducive to storing massive data: the larger the data volume, the higher the cost for users to manipulate the data.
In order to solve the above problems, experts have designed a database that is more conducive to managing data.

Note: If file storage is used, the management of the data content in the file needs to be done by the programmer himself. The essence of the database is a layer of software between the programmer and the file, which provides basic content operations for the file content, and does not require the programmer to manually Data management, to help programmers with data management.

Client VS Server:

The database is divided into a database server and a database client. Taking the MySQL database as an example, when we use the MySQL database, we will first connect to the MySQL server through the mysql -uroot -p command. as follows:

The mysql command we use here is the MySQL client, and the MySQL server can be viewed through the ps -axj | head -1 && ps -axj | grep mysqled | grep -v grep command. as follows:

The mysqld seen here is the MySQL server. We use the mysql command to actually connect to the mysqld service. You can see from the netstat -ltp command that the bottom layer of the MySQL server uses the TCP6 protocol, and the server is currently in the listening state. as follows:

In other words, the MySQL server is essentially a network server. When we use the mysql command to connect to the MySQL server, the essence is that the MySQL client initiates a connection request to the MySQL server. After the connection is established successfully, the MySQL client will send the SQL statement entered by the user. to the MySQL server, and then the MySQL server will perform corresponding operations on the data according to the SQL statement.

Storage medium of the database:

• Disk, such as MySQL is a disk database.

• Memory, such as redis is a kind of memory database.

Note:

1. Databases can be divided into disk databases and memory databases according to storage media, and memory databases are also called main memory databases (Main Memory Database).
2. The data of the disk database is mainly stored on the disk, so the disk database has obvious advantages in the persistent storage of data. However, in order to improve data storage efficiency, the disk database also has its own corresponding caching mechanism, so not all data will be refreshed to the disk at a certain time.
3. The data of the memory database is mainly stored in the memory. Compared with the disk database, the memory database can greatly improve the reading speed and reduce the access time of the database. However, the in-memory database does not completely use the disk. The startup information and initial data of the database still need to be stored on the disk, but the data is mainly stored and calculated in the memory.
4. Since the data of the memory database is stored in the memory, the data will be lost after the database host is powered off. Therefore, it is usually necessary to dump the data in the memory to the disk before the database service is shut down. Even during the operation of the memory database, some data Also persisted to disk storage.


2. Mainstream database

Here are a few popular databases:

• SQL Server: Microsoft's product, the favorite of .Net programmers, suitable for medium and large projects.
• MySQL: Oracle products, the world's most popular database, good concurrency, but not suitable for complex business. It is mainly used in e-commerce, SNS, and forums, and has a good effect on simple SQL processing.
• Oracle: Oracle products are suitable for large-scale projects and complex business logic, but concurrency is generally not as good as MySQL.
• PostgreSql: A relational database developed by the Department of Computer Science, University of California, Berkeley. It can be used, modified and distributed for free regardless of private use, commercial use, or academic research.
• SQLite: A lightweight database, a relational database management system that complies with ACID, contained in a relatively small C library. Its design target is embedded, and it has been used in many embedded products. It occupies very low resources. In embedded devices, only a few hundred K of memory may be enough.
• H2: It is an embedded database developed in Java, which itself is just a class library, which can be directly embedded into the application project.


3. Basic use

3.1. Connect to the server

The way to connect to the MySQL server is as follows:

explain:

• -h: indicates the host where the MySQL server you want to connect to is located, and 127.0.0.1 indicates the host.
• -P: Indicates the port number corresponding to the MySQL server you want to connect to, generally the default is 3306.
• -u: indicates which user is used to connect to the MySQL server, and root indicates a super user.
• -p: Indicates the password corresponding to the user, the password can be directly followed by -p, or can be entered after pressing Enter.
In addition, if you want to connect to a local MySQL server, you only need to specify the user name and user password when connecting. as follows:

After connecting to the MySQL server, you can enter various SQL statements for the server to execute. When you want to exit mysql, you can directly enter quit or exit or \q. as follows:

Note: The mysql port number is 3306 by default. If you want to modify the mysql port number, you can open the configuration file with vim /etc/my.cnf, and add the port=port number option in the mysqld part, as shown in the figure below. At this time, restart mysql, and then use the netstat -nltp command to see that the port number of mysql is changed to 3333, as shown in Figure 2 below.

3.2. Server Management

Stop the server:

The MySQL server can be stopped through the systemctl stop mysqld command or the service mysqld stop command. for example:

Start the server:

The MySQL server can be started by the systemctl start mysqld command or the service mysqld start command. for example:

Restart the server:

The MySQL server can be restarted through the systemctl restart mysqld command or the service mysqld restart command. for example:

3.3. Database server, database, table relationship

Database server, database, and table relationship:
• The so-called installation of a database server just installs a database management system program on the machine. This management program can manage multiple databases. Generally, developers will create a database for each application.
• In order to save the data of the entities in the application, multiple tables are generally created in the database to save the data of the entities in the program.
The relationship between database server, database and table is as follows:

Note:

1. The Client in the figure corresponds to the mysql command, and the MySQL corresponds to the mysqld service.

2. DB (database) represents multiple databases managed by mysqld, and each DB will contain multiple tables.

3.4. Use cases

3.4.1. Data storage path

Data storage path:

The database and various table structures created by MySQL will eventually be stored in the form of files. By viewing the datadir in the MySQL configuration file, you can know the storage path of the data file.

The absolute path of the default MySQL configuration file is /etc/my.cnf, and the corresponding value of datadir in the configuration file is /var/lib/mysql. as follows:

In the future, the database files created by MySQL will be stored in this directory. You can see that there are many MySQL-related data files in this directory. as follows:

3.4.2. Create/delete database

Create/delete database:

After connecting to the MySQL server, create a database named helloworld through the create database helloworld; statement. for example:

The command line written by mysql must end with a semicolon.

At this time, under the /var/lib/mysql directory, there will be an additional directory named helloworld. as follows:

Currently there is only one file named db.opt in the helloworld directory, which specifies the default character encoding and character verification rules of the current database. as follows:

Therefore, when we create a database, the essence is to create a new directory under the MySQL data storage path, and when we use the drop database helloworld; command to delete the database, the directory actually does not exist. for example:

At this time, the helloworld directory under the MySQL data storage path no longer exists, so deleting the database essentially means deleting the corresponding directory under the data storage path. 

Note:

1. The creation and deletion of the database here is done on the mysql client. The essence of creating and deleting the database is that the mysql client sends the operation statement to the mysqld server through the network or local loopback. After the mysqld server process receives the instruction Make a system call to create or delete a directory.

2. If you want to clear the screen under the mysql command line, you can use the system clear command to invoke the clear screen in the system command.

3.4.3. Using the database

Use the database:

Before using the database, you can check the current databases through the show databases; statement, and then use the specified database through the use helloworld; statement. for example:

Using the database can be understood as using the cd command to enter the directory corresponding to the database. 

Use the command to see the current directory in the database, for example:

3.4.4. Create database table

Create a database table:

Here we create a simple student table through the create statement. for example:

You can view the tables in the current database through the show tables command, for example:

Use the show create table student \G command to view the created student table structure. for example:

Since the storage engine used by the student table is InnoDB, there will be two more files in the helloworld directory under the MySQL data storage path, namely student.frm and student.ibd. as follows: 

Among them, student.frm is the table structure file, and student.ibd is the table data and index file.

If the essence of creating a database is to create a new directory under the data storage path, then the essence of creating a table is actually to create several files under a specific database directory (the InnoDB storage engine corresponds to two, and the MyISAM storage engine corresponds to three ), so be sure to select a database before creating tables. 

3.4.5. Insert data into the table

The corresponding table structure can be viewed through the desc statement. for example:

Data can be inserted into a table through the insert statement. for example:

3.4.6. Query the data in the table

The data in the table can be viewed through the select statement. for example:

3.5. Data logic storage

The data in the table is presented in the form of a two-dimensional table, including rows and columns. as follows:

We call each row a record, and each column represents an attribute (attribute column). 


4.MySQL architecture

MySQL architecture:

MySQL is a portable database that runs on almost all current operating systems, such as Unix/Linux, Windows, Mac, and Solaris. Various systems differ in their underlying implementations, but MySQL can basically guarantee the consistency of the physical architecture on each platform.

The architecture design of MySQL is as follows:

The MySQL architecture can be divided into the following four layers:

• Connection layer: It mainly completes some similar connection processing, authorization authentication and related security solutions.
• Service layer: All the work before the MySQL database system processes the underlying data is completed at this layer, including authority judgment, SQL interface, SQL parsing, SQL analysis optimization, cache query processing, and execution of some built-in functions. The functions provided by each storage engine are concentrated in this layer, such as stored procedures, triggers, attempts, and so on.
• Engine layer: Composed of a variety of pluggable storage engines, it is really responsible for the storage and extraction of data in MySQL. Each storage engine has its own advantages and disadvantages. The service layer interacts with them through the storage engine API .
• Storage layer: store data on the file system of the raw device, and complete the interaction of the storage engine.

MySQL client:

The MySQL server will receive the SQL statement sent by the MySQL client, and execute the corresponding operation according to the SQL statement.

• The MySQL client here not only refers to the mysql command used when connecting to MySQL, but also includes the language interface client.
• MySQL provides interfaces for accessing databases in various languages, and users can also send SQL statements to the MySQL server by calling these interfaces.
The mysql command is essentially an executable program. You can see that the executable program is generated by dynamic linking through the file command. You can see the C/C++ library file that the executable program depends on through the ldd command. as follows:

That is to say, the mysql command itself is written in C/C++, so when writing a mysql program, it is necessary to call the language interface client provided by MySQL to C/C++. Of course, MySQL not only provides language interfaces corresponding to C/C++, but also Python, Java, PHP, etc. have corresponding MySQL interfaces.


5. SQL classification

SQL (Structured Query Language, Structured Query Language) is a database query and programming language for accessing data and querying, updating and managing relational database systems.

SQL statements can be divided into the following three categories:

• DDL (Data Definition Language) data definition language, used to maintain the structure of stored data. Such as create statement, drop statement, alter statement, etc.
• DML (Data Manipulation Language) is a data manipulation language used to manipulate data. Such as insert statement, delete statement, update statement, etc.
• DCL (Data Control Language) data control language, mainly responsible for rights management and transactions. Such as grant statement, revoke statement, commit statement.
Note:

1. In DML, there is a separate DQL (Data Query Language) data query language, such as select statement, from statement, where statement, etc.

2. The data definition language DDL is more about attribute operations on databases and table structures, and the data manipulation language DML is more about data content operations on databases and table structures.

3. The data definition language DDL and the data operation language DML complete the basic functions of the database, and the data control language DCL completes the system security and account management of the database.


6. Storage engine

6.1. Storage engine concept

The storage engine is how the database management system stores data, how to index the stored data, how to update the data, how to query the data and other technologies. The storage engine in MySQL is a plug-in storage engine, which can support multiple storage engines .

6.2. View storage engine

Use the show engines; command to view the storage engines supported by MySQL. for example:

The default storage engine used by MySQL is InnoDB, which supports transactions, row-level locks, and foreign keys. 

Note: From the figure above, it can be seen that InnoDB is the default search engine supported by DEFAULT. This is because we set the default search engine to InnoDB in the /etc/my.cnf configuration file, as shown in the figure below.

6.3. Comparison of storage engines

The comparison of each storage engine is as follows:

Suggested memory: InnoDB storage engine supports transactions, but MyISAM storage engine does not.

Guess you like

Origin blog.csdn.net/qq_45113223/article/details/131217181