General talk about MySQL (1)

The following is an introduction to MySQL. The content of the document is relatively basic. It is only designed for operation and a few principles. Please bypass it.
Stop talking nonsense, let's go!

15.jpg

1. Introduction to MySQL architecture

1-Introduction to MySQL

overview

MySQL is a relational database management system developed by MySQL AB in Sweden and currently belongs to Oracle Corporation.

MySQL is a relational database management system that keeps data in different tables instead of putting all the data in one big warehouse, which increases speed and improves flexibility.

MySQL is open source, so you don't need to pay extra.

MySQL supports large databases. Can handle large databases with tens of millions of records. MySQL uses standard SQL data language forms.

MySQL can be allowed on multiple systems and supports multiple languages. These programming languages ​​include C, C++, Python, Java, Per, PHP, Eifel, Ruby, and Tcl, among others. Mysql has good support for PHP, which is currently the most popular web development language.

MySQL supports large databases and data warehouses with 50 million records. The 32-bit system table file can support a maximum of 4GB, and the 64-bit system supports a maximum table file of 8TB. Mysql can be customized, using the GPL agreement, you can modify the source code to develop your own MySQL system.

2-RPM installation

①Download address

MySQL Linux version (CentOS) official website download address

② Check whether the current system has installed MySQL

  • Query command: rpm -qa|grep -i mysql
  • Delete command: rpm -e RPM package name (the name is the name detected by the previous command)`

Not installed:

already installed

③Install the MySQL server (note the prompt)

  • Installation command: rpm -ivh RPM package name

If an error is reported, it can be said that the package is missing:

rpm -ivh MySQL-server-5.5.48-1.linux2.6.i386.rpm --nodeps --force  resolves dependencies and forces installation

yum -y install glibc.i686
yum -y install libncurses.so.5
yum -y install libaio.so.1
yum -y install libz.so.1

④ Install the MySQL client

  • Installation command: rpm -ivh RPM package name

rpm -ivh MySQL-server-5.5.48-1.linux2.6.i386.rpm --nodeps --force Solve the dependency relationship and force the installation

View the MySQL users and MySQL groups created during MySQL installation

  • cat /etc/passwd |grep mysql
  • cat /etc/group |grep mysql

Or you can execute the mysqladmin --version command.

or ps -ef | grep mysql

⑤MySQL service startup and suspension

  • service mysql stop/restart/start
  • ps -ef | grep mysql view

Start service mysql start can report an error

Because of this file problem:
mv /etc/my.cnf /etc/my.cnf.bak

⑥ROOT password setting and boot self-start

After the mysql service starts, start the connection.

Successful connection for the first time

  • Enter the command mysql  because the initial root user has no password, so you can log in directly by entering mysql
  • exit command exit

Follow the prompts in Installing Server to change the login password

  • /usr/bin/mysqladmin -u root password coderxz

The password actually corresponds to the user table in the mysql database in mysql:

It can also be specific to IP 192.168.1.%

Self-start mysql service

⑦ Modify the configuration file location

  • cp /usr/share/mysql/my-huge.cnf /etc/my.cnf

Why does mysql read /etc/my.cnf by itself ?

Open mysql.server and you will find that conf=/etc/my.cnf is written to death.

link

Check the installation directory under Linux ps -ef | grep mysql.

⑧ Modify character set

View character set

  • show variables like 'character%";
  • show variables like ‘%char%";

By default, both the client and the server use latin1, so it will be garbled.

Modify character set and data storage path

  • vim /etc/my.cnf

<span style="color:#2c2c2c"><span style="background-color:#ffffff"><code class="language-sql">[client]
#password <span style="color:#ab5656">=</span> your_password
port   <span style="color:#ab5656">=</span> <span style="color:#880000">3306</span>
socket    <span style="color:#ab5656">=</span> <span style="color:#ab5656">/</span>var<span style="color:#ab5656">/</span>lib<span style="color:#ab5656">/</span>mysql<span style="color:#ab5656">/</span>mysql.sock
<span style="color:#0000ff">default</span><span style="color:#ab5656">-</span><span style="color:#a31515">character</span><span style="color:#ab5656">-</span><span style="color:#0000ff">set</span><span style="color:#ab5656">=</span>utf8
[mysqld]
port   <span style="color:#ab5656">=</span> <span style="color:#880000">3306</span>
character_set_server<span style="color:#ab5656">=</span>utf8
character_set_client<span style="color:#ab5656">=</span>utf8
socket  <span style="color:#ab5656">=</span> <span style="color:#ab5656">/</span>var<span style="color:#ab5656">/</span>lib<span style="color:#ab5656">/</span>mysql<span style="color:#ab5656">/</span>mysql.sock
<span style="color:#0000ff">collation</span><span style="color:#ab5656">-</span>server<span style="color:#ab5656">=</span>utf8_general_ci
#(注意linux下mysql安装完毕是默认:表名区分大小写;<span style="color:#880000">0</span>:区分大小写;<span style="color:#880000">1</span>:不分区大小写)
lower_case_table_names<span style="color:#ab5656">=</span><span style="color:#880000">1</span>
#(设置最大连接数,默认为<span style="color:#880000">151</span>,MySQL服务器允许的最大的连接数为<span style="color:#880000">16384</span>)
max_connections<span style="color:#ab5656">=</span><span style="color:#880000">1000</span>
[mysql]
<span style="color:#0000ff">default</span><span style="color:#ab5656">-</span><span style="color:#a31515">character</span><span style="color:#ab5656">-</span><span style="color:#0000ff">set</span> <span style="color:#ab5656">=</span> utf8
</code></span></span>

Restart the database

  • service mysql start
  • service mysql stop

After reconnecting, re-create databse and use the new library, and then try to re-create the table

3-MySQL configuration file

main configuration file

Binary log log-bin

  • for master-slave replication

Error log log-error

  • The default is off, logging severe warnings and error messages, detailed information on each startup and shutdown, etc.

Query log log

  • It is disabled by default, and records the sql statement of the query. If it is enabled, the overall performance of mysql will be reduced, because logging also consumes system resources

data file

  • two systems

    • windows
      • mysqlafter inputselect @@database;
    • linux
      • Default path: /var/lib/mysql
  • frm file (form)

    • storage table structure
  • myd file (my data)

    • Store table data
  • myi file (my index)

    • storage table index

how to configure

  • Windows - my.ini file
  • Linux - /etc/my.cnf file

4- Introduction to MySQL Logical Architecture

General overview

Compared with other databases, MySQL is a bit different. Its architecture can be applied and play a good role in many different scenarios. It is mainly reflected in the architecture of the storage engine. The plug-in storage engine architecture separates query processing from other system tasks and data storage and extraction. This architecture can select an appropriate storage engine according to business needs and actual needs.

1. Connection layer

The top layer is some client and connection services, including local sock communication and most tcplip-like communication based on client/server tools. It mainly completes some similar connection processing, authorization authentication, and related security solutions. On this layer, the concept of thread pool is introduced to provide threads for clients that pass authentication and secure access. SSL-based secure links can also be implemented on this layer. The server also verifies the operating authority it has for each client that accesses securely.

2. Service layer

The second-tier architecture mainly completes more or less core service functions, such as SQL interface, cache query, SQL analysis and optimization, and execution of some built-in functions. All cross-storage engine functions are also implemented at this layer, such as procedures and functions. At this layer, the server will parse the query and create a corresponding internal parsing tree, and optimize it accordingly, such as determining whether the order of the query table uses an index, etc., and finally generate the corresponding execution operation. If it is a select statement, the server will also query the internal cache. If the cache space is large enough, the performance of the system can be greatly improved in the environment of solving a large number of read operations.

3. Engine layer

The storage engine layer, the storage engine is really responsible for the storage and extraction of data in MySQL, and the server communicates with the storage engine through the API. Different storage engines have different functions so that we can choose according to our actual needs. MyISAM and InnoDB will be introduced later.

4. Storage layer

The data storage layer mainly stores data on the file system running on the raw device and completes the interaction with the storage engine.

  • 1.Connectors

Refers to the interaction with SQL in different languages

  • 2 Management Serveices & Utilities:

System Management and Control Tools

  • 3 Connection Pool: connection pool

Manage cached user connections, thread processing, etc. that require caching.
Responsible for monitoring various requests to MySQL Server, receiving connection requests, and forwarding all connection requests to the thread management module. Each client request connected to MySQL Server will be assigned (or created) a connection thread for its separate service. The main job of the connection thread is to be responsible for the communication between the MySQL Server and the client,
to accept the command request from the client, and to transmit the result information from the server, etc. The thread management module is responsible for managing and maintaining these connection threads. Including thread creation, thread cache, etc.

  • 4 SQL Interface: SQL interface.

Accept the user's SQL command and return the result that the user needs to query. For example, select from is to call SQL Interface

  • 5 Parser: Parser.

When the SQL command is passed to the parser, it will be verified and parsed by the parser. The parser is implemented by Lex and YACC and is a long script.
In MySQL, we are used to calling all the commands sent by the client to the server as query. In MySQL Server, after the connection thread receives a query from the client, it will directly pass the query to the special responsible for classifying various queries and then forwarded to each corresponding processing module.
Main functions:
a. Analyze the semantics and syntax of SQL statements, decompose them into data structures, classify them according to different operation types, and then make targeted forwarding to the next steps. The transmission and processing of SQL statements will be based on this structured.
b. If an error is encountered in the decomposition and composition, it means that the sql statement is unreasonable

  • 6 Optimizer: query optimizer.

The SQL statement will use the query optimizer to optimize the query before querying. It is to optimize the query (sql statement) requested by the client. According to the query statement requested by the client and some statistical information in the database, analyze on the basis of a series of algorithms to obtain an optimal strategy and tell the following programs How to get the result of this query statement (SQL query order priority from high to low: from-where-groupby-having-select-orderby-limit)
He uses the "select-projection-join" strategy for query.
It can be understood with an example: select uid,name from user where gender = 1;
This select query is first selected according to the where statement, instead of querying all tables first and then filtering by gender.
This select query is first performed according to uid and name Attribute projection, instead of taking out all attributes and then filtering them.
Join these two query conditions to generate the final query result

  • 7 Cache and Buffer: Query cache.

Its main function is to cache the returned result set of the Select class query request submitted by the client to MySQL into the memory, and make a correspondence with a hash value of the query. After any data change occurs in the base table of the data fetched by the Query, MySQL will automatically invalidate the Cache of the query. In an application system with a very high read-write ratio, Query Cache can significantly improve performance. Of course, its memory consumption is also very large.
If the query cache has a hit query result, the query statement can directly fetch data from the query cache. This caching mechanism is composed of a series of small caches. Such as table cache, record cache, key cache, permission cache, etc.

  • 8. Storage engine interface

The storage engine interface module can be said to be the most distinctive point in the MySQL database. Among various database products, basically only MySQL can implement plug-in management of its underlying data storage engine. This module is actually just an abstract class, but it is precisely because it successfully abstracts all kinds of data processing that it has achieved the characteristics of today's MySQL pluggable storage engine.
It can also be seen from the figure that the most important feature that distinguishes MySQL from other databases is its plug-in table storage engine. The MySQL plug-in storage engine architecture provides a series of standard management and service support. These standards have nothing to do with the storage engine itself, and may be necessary for each database system itself, such as SQL analyzers and optimizers. The storage engine is For the realization of the underlying physical structure, each storage engine developer can develop according to his own wishes.
Note: The storage engine is based on tables, not databases.

5- Introduction to storage engine

View commands:

  • See what storage engines mysql provides
    • show engines;

  • View the current default storage engine of mysql
    • show variables like '%storage_engine%';
<span style="color:#2c2c2c"><span style="background-color:#ffffff"><code class="language-sql">mysql<span style="color:#ab5656">></span> <span style="color:#0000ff">show</span> variables <span style="color:#0000ff">like</span> <span style="color:#a31515">'%storage_engine%'</span>;
<span style="color:#ab5656">+</span><span style="color:#008000">----------------------------------+--------+</span>
<span style="color:#ab5656">|</span> Variable_name                    <span style="color:#ab5656">|</span> <span style="color:#0000ff">Value</span>  <span style="color:#ab5656">|</span>
<span style="color:#ab5656">+</span><span style="color:#008000">----------------------------------+--------+</span>
<span style="color:#ab5656">|</span> default_storage_engine           <span style="color:#ab5656">|</span> InnoDB <span style="color:#ab5656">|</span>
<span style="color:#ab5656">|</span> default_tmp_storage_engine       <span style="color:#ab5656">|</span> InnoDB <span style="color:#ab5656">|</span>
<span style="color:#ab5656">|</span> disabled_storage_engines         <span style="color:#ab5656">|</span>        <span style="color:#ab5656">|</span>
<span style="color:#ab5656">|</span> internal_tmp_disk_storage_engine <span style="color:#ab5656">|</span> InnoDB <span style="color:#ab5656">|</span>
<span style="color:#ab5656">+</span><span style="color:#008000">----------------------------------+--------+</span>
<span style="color:#880000">4</span> <span style="color:#0000ff">rows</span> <span style="color:#0000ff">in</span> <span style="color:#0000ff">set</span>, <span style="color:#880000">1</span> warning (<span style="color:#880000">0.01</span> sec)
</code></span></span>

MyISAM and InnoDB

Alibaba, Taobao which one to use?

  • Percona has improved the MySQL database server, which has a significant improvement in function and performance compared to MySQL. This version improves the performance of InnoDB under high load conditions, provides some very useful performance diagnostic tools for DBAs; in addition, there are more parameters and commands to control server behavior.

  • The company has created a new storage engine called xtradb that can completely replace innodb , and it is better in performance and concurrency.

  • Most of Alibaba's mysql databases actually use the Percona prototype to modify it.

  • AliSql+AliRedis

Guess you like

Origin blog.csdn.net/m0_60961651/article/details/132272764
Recommended