"MySQL-01" MySQL Basics

Table of contents

1. Database concept

1. What is a database

2. Why is there a database?

3. Where does the database store data?

2. Introduction of well-known databases

1. Introduction to well-known databases

2. Why learn MySQL

Three, the basic use of MySQL

0. Install MySQL

1. The database client connects to the server

2. MySQL server management under Windows

3. The nature of the database and the server, database, table relationship

4. Physical storage and logical storage of the database

"Tuo"'s simple operation on the database

Four, MySQL architecture

1. MySQL is a portable database

2. How is this possible?

3. The design idea of ​​MySQL server plug-in layer

5. Storage engine

Six, SQL statement classification


1. Database concept

1. What is a database

1.1 In a narrow sense

        a. From the server side: whether the database is started, the database here refers to the server side of the database, namely mysqld

(The MySQL we installed is a set of network services, both clients and servers, which means that our MySQL server on host A can be accessed by the MySQL client on host B. After starting the database server, we can pass The client of the database accesses the server to add, delete, modify and check the data.)

        b. From the data level: create a database, here refers to creating a database file, that is, the file saved in the path /var/lib/mysql.

1.2 From a macro perspective

A collection of software (program + data)         that provides users with data management .

2. Why is there a database?

2.1 You can use files to store data, why do you need a database ?

  • Both files and databases can be used to store data, but we use files to store data, and we need to do the data management work ourselves, such as querying and modifying the data in the files, we need to design our own programs to complete.
  • In order to solve the above problems, experts add an application layer software that is more conducive to managing data between users and files—database, which can help users manage data in files. (So ​​the database can also be said to be a "file system" that manages file content )
  • Therefore, the advantage of the database is that the database also manages the data while storing the data, and provides basic data operations (addition, deletion, check, and modification) to the outside world, without the need for manual data management by programmers.

2.2 Compared with databases, simply using files to save data has the following disadvantages:

Files are not conducive to massive data query and management
File security issues (how to recover accidentally deleted data?)

It is inconvenient to control the file in the program (you must first load all the content in the file into memory before you can get the content of a certain line)

3. Where does the database store data?

Disk or memory (Database storage data also creates files and saves them on disk or in memory.)

2. Introduction of well-known databases

1. Introduction to well-known databases

  • SQL Sever: Microsoft's product, suitable for medium and large projects, the favorite of .Net programmers.
  • Oracle : Oracle products, suitable for large-scale projects, complex business logic, will provide supporting services, and the concurrency is not as good as MySQL . It is generally used by banks or large enterprises that are not Internet companies.
  • MySQL : The most popular database in the world, belonging to Oracle, has good concurrency, but it is not suitable for complex business. Mainly used in e- commerce, SNS, forums. Good for simple SQL processing. Internet companies generally use MySQL.
  • PostgreSQL: A relational database developed by the Department of Computer Science at the University of California, Berkeley. Whether it is for private use, commercial use, or academic research use, it can be used for free. Academic research institutions may use PostgreSQL more.
  • SQLite : It is a lightweight database, a relational database management system that complies with ACID , and it is 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. for embedded development.
  • 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.

2. Why learn MySQL

        Because MySQL is open source, low cost of use, good ecology, active community, and the efficiency of MySQL is not bad under continuous iterative updates, it can completely hold the data of millions or tens of millions, so Internet companies generally use MySQL, so The level of MySQL is an important indicator to measure the level of a programmer .


Three, the basic use of MySQL

0. Install MySQL

For the installation of MySQL on Linux, please refer to the blogger's article: "MySQL-00" MySQL installation on Linux

1. The database client connects to the server

mysql -h 127.0.0.1 -P 3306 -u root -p

-h : 主机,由于MySQL默认禁止跨网络服务,现在先本地回环127.0.0.1
-P : 端口号,MySQL服务端的端口号默认为3306
-u : 以哪个用户登录
-p : 用户密码

"Tuo 1" how to change the default port number of the MySQL server?

        Use vim to open the MySQL configuration file:   vim /etc/my.cnf
        and add before the last line:  port=modified port number to
        restart the MySQL client:  systemctl restart mysqld

        It is recommended to modify at work, if it is only for practice, there is no need to modify it.

How to check the default port number of the MySQL server in "Tuo 2" ?

        Enter the command netstat -nltp in Linux to query.

2. MySQL server management under Windows

Execute win+r and enter services.msc to open the service manager

Service management is performed through the stop, pause, and restart buttons on the left side of the figure below

3. The essence of the database and  the server, database, table relationship

3.1 The essence of creating a database:

        In mysql, create a database create database XXX, the essence is to create a directory under linux!


3.2 The essence of creating a table in the database:

        In mysql, create a table create table table name (); The essence is to create the corresponding file in the corresponding database directory on Linux!


3.3 The nature of database operations:

        So when we operate on the database or tables in the database, we are essentially operating on the corresponding directories and files on Linux.


3.4 The nature of the database server:

        The so-called installation of the database server is just to install a database management system program on the machine, that is, musqld (database server), and this management program can manage multiple databases.


3.5 The nature of the database client:

        On Linux, we access the MySQL server through the command line, which is a client.

        The software for accessing the database in the graphical interface is also a client.

        The third-party library passed in MySQL is introduced into the program to access the MySQL server, then this program is also a client.


3.6 Summary

The relationship between database client server, database and table is as follows:

 The MySQL client initiates a request to the server through SQL statements, and the server operates the database according to the request.

4. Physical storage and logical storage of the database

4.1 Physical storage: stored in files in binary form.


4.2 Logical storage:  stored in tables in the form of rows and columns.

"Tuo"'s simple operation on the database

  •  show databases; : Show the created database (note that the database command ends with a semicolon).
  • create database database name; : Create a database (note that the database command ends with a semicolon).
  • system system command   : Execute Linux system commands, often use system clear (execute system commands to clear the screen, without ending with ;).
  • use database_name;   : Enter a database.
  • select database();   : Check which database is currently in. (Need to enter a database first)
  • create table table name (table header);   : create a table. (Need to enter a database first)
  • show tables;   : View all tables in the database. (Need to enter a database first)

Four, MySQL architecture

1.  MySQL is a portable database

        MySQL is a portable database that can run on almost all current operating systems, such as Unix/Linux , Windows , Mac and Solaris . Various systems have different underlying implementations, but MySQL can basically guarantee the consistency of the physical architecture on each platform. That is, the underlying implementation of MySQL is different on each platform, but the upper-layer interface exposed externally on each platform is the same.

2. How is this possible?

The MySQL server architecture is mainly divided into three layers:

  • Link layer: manage the link with the client, such as authentication.
  • Interface recognition & syntax analysis & performance tuning layer of SQL statements: SQL statements have already been processed and parsed when they are passed to the lower layer.
  • Plug-in (storage engine) layer: The parsed SQL statement is given to the selected storage engine, and the storage engine interacts with the file system of the OS according to the SQL statement.

3. The design idea of ​​MySQL server plug-in layer

        There are multiple storage engines in the plug-in layer of the MySQL server. These storage engines must be managed. How to manage them? Of course, describe first, then organize . Since MySQL is written in C language/C++, these storage engines can be described by classes . The member methods in the classes include both the call interface provided to the upper layer and the operation method to the lower layer. In the future, if users want to use a certain storage engine, they can directly generate an object of class new; if they don’t need it, they can delete it directly. This is plug-in development. For existing storage engine objects, organize them with data structures such as lists and sequential tables , thus completing the management of all storage engines.


5. Storage engine

1. What is it?

        The storage engine is: how the database management system stores data, how to index the stored data and how to update and query data and other technical implementation methods. The part that actually executes the SQL statement.

The core of MySQL is the plug-in storage engine, which supports multiple storage engines.


2. Check the storage engine

        show engines;

3. Commonly used storage engines

  • InnoDB: It is recommended to be set as the default storage engine because it supports transactions.
  • MyISAM: Often used for a large number of lookups.

4. Set the storage engine

  • Open the MySQL configuration file with vim: vim /etc/my.cnf
  • Then add the line before the last line skip-grant-tables: default-storage-engine=storage engine name
  • Finally restart the MySQL client: systemctl restart mysqld

Six, SQL statement classification

Classify SQL statements according to their application scenarios:

  • DDL [ data definition language ] Data definition language, used to create, modify, delete the structure of stored data ( such as database, table, view)
    representative instructions : create, drop, alter
  • DML [ data manipulation language ] Data manipulation language, used to operate on data Representative instructions: insert , delete , update DML is divided into a separate DQL , data query language, representative instructions: select

  • DCL [ Data Control Language ] data control language, mainly responsible for authority management and account management
    representative instructions: grant , revoke , commit

Guess you like

Origin blog.csdn.net/look_outs/article/details/132502632