Directories and files in MySQL

1, bin directory

There is a particularly important bin directory under the MysQL installation directory, which contains many executable files.

Executables on other systems are similar. These executable files are related to the server program and the client program.

1.1, start the MySQL server program

There are many executable files used to start the MySOL server program in the UNIX system, most of which are in the bin directory of the MySQL installation directory.

mysqld

The executable file mysqld represents the MySOL server program, and running this executable file can directly start a server process. But this command is not commonly used.

mysqld_safe

mysqld safe is a startup script that indirectly calls mysqld, and also starts another monitoring process by the way. This monitoring process can help restart the server process when it hangs. In addition, when using mysqld_safe to start the server program, it will redirect the error information and other diagnostic information of the server program to a file and generate an error log, which can facilitate us to find out the cause of the error.

mysql.server

mysql.server is also a startup script, it will call mysqld_safe indirectly, and the server program can be started by specifying the start parameter later when calling mysql.server

like this:

mysql.server start

It should be noted that this mysql.server file is actually a link file, and its actual file is support-files/mysql.server, so if you can’t find it in the bin directory, go to support-files to find it, and if you want If so, use the ln command to create a link in bin.

In addition, we can also use the mysql.server command to close the running server program, just replace the start parameter with stop:

mysql.server stop

mysqld_multi

In fact, we can also run multiple server instances on one computer, that is, run multiple NySQL server processes. The mysql_multi executable can monitor the start or stop of each server process.

1.2, client program

After we successfully start the MysTL server program, we can then start the client program to connect to the server. There are many client programs in the bin directory, such as mysqladmin, mysqldump, mysqlcheck and so on.

We often use the executable file mysql, which allows us to interact with the server program process, that is, to send requests and receive processing results from the server.

mysqladmin is a tool for performing management operations, checking server configuration, current running status, creating and deleting databases, and setting new passwords.

mysqldump database logic backup program.

mysqlbackup backs up data tables, entire databases, and all databases. Generally speaking, mysqldump backups and mysql restores.

2. Startup options and parameters

2.1, configuration parameter file

When the MySQL instance is started, the database will first read a configuration parameter file, which is used to find the location of various files of the database and specify some initialization parameters, which usually define how big a certain memory structure is. By default, the MySQL instance will read from the specified location in a certain order, and the user only needs to use the command mysql --help|grep my.cnf to find it.

Of course, you can also specify a configuration file (non-yum installation) when starting MySQL:

At this time, the configuration file specified at startup will prevail.

The function of the MySQL database parameter file is very similar to that of the Oracle database. The difference is that if the parameter file cannot be found when the Oracle instance is started, the mount operation cannot be performed. MySQL is slightly different. The MySQL instance does not need a parameter file. At this time, all parameter values ​​​​depend on the default values ​​​​specified when compiling MySQL and the default values ​​​​of the parameters specified in the source code.

The parameter file of MySQL database is stored in text mode. The parameters can be modified directly through some commonly used text editing software.

2.2, view and modify parameters

You can use the command show variables to view all the parameters in the database, and you can also use LIKE to filter the parameter names, which has been shown in the previous search for the database engine. Starting from MySQL 5.1, you can also search through the GLOBAL_VARIABLES view under the information_schema architecture. It is recommended to use the command show variables, which is easier to use and is supported by all versions of MySQL databases.

For the specific meaning of the parameters, please refer to the official MySQL manual:

MySQL :: MySQL 5.7 Reference Manual :: 5.1.7 Server System Variables

However, the parameters encountered in the course will be explained.

The parameters in the MySQL database can be divided into two categories: dynamic (dynamic) parameters and static (static) parameters. At the same time, it can be divided into global variables and session variables from the scope of action.

Dynamic parameters mean that they can be changed while the MySQL instance is running, and static parameters indicate that they cannot be changed during the entire life cycle of the instance, as if they are read-only.

Global variables (GLOBAL) affect the overall operation of the server.

Session variables (SESSION/LOCAL) affect the operation of a client connection.

For example, using default_storage_engine to illustrate, a system variable named default_storage_engine with a scope of GLOBAL will be initialized when the server starts. Afterwards, whenever a client connects to the server, the server will assign a system variable named default_storage_engine to the client separately, with a scope of SESSION. The value of the system variable of the same name is initialized.

Dynamic parameter values ​​can be modified through the SET command.

The syntax of SET is as follows:

set [global || session ] system_var_name= expr
or
set [@@global. || @@session.] system_var_name= expr
for example:
set read_buffer_size=524288;
set session read_buffer_size=524288;
set @@global.read_buffer_size=524288;

For the modifiable range of all dynamic variables in MySQL, you can refer to the relevant content of Dynamic System Variables in the official MySQL manual:

MySQL :: MySQL 5.7 Reference Manual :: 5.1.8.2 Dynamic System Variables

For static variables, if you modify them, you will get an error similar to the following:

3. Data directory

We know that storage engines like InnoDB and MyIASM store tables on disks, and the stuff used by the operating system to manage disks is also called a file system, so in a more professional way it is: like InnoDB, Storage engines such as MyISAM store tables on the file system. When we want to read data, these storage engines will read the data from the file system and return it to us. When we want to write data, these storage engines will write the data back to the file system.

3.1, determine the data directory in MySQL

After talking for a long time, in which path does MySQL store the data? In fact, the data directory corresponds to a system variable datadir. After we use the client to establish a connection with the server, we can check the value of this system variable:

show variables like 'datadir';

Of course, this directory can be modified through the configuration file and specified by ourselves.

3.2, What to put in the data directory?

What data will MySOL generate during operation? Of course, it will include user data such as databases, tables, views and triggers we created. In addition to these user data, MySQL will also create some other additional data for the better operation of the program.

3.2.1, the representation of the database in the file system

create database lijin charset=utf8;

cd

Whenever we use the CREATE DATABASE statement to create a database, what actually happens on the file system? It's actually very simple. Each database corresponds to a subdirectory under the data directory, or corresponds to a folder. Whenever we When we create a new database, MySQL will do these two things for us:

1. Create a subdirectory (or folder) with the same name as the database name under the data directory.

2. Create a file named db.opt in the subdirectory with the same name as the database name. This file contains various attributes of the database, such as the character set and comparison rules of the database.

Let's say we check which databases are currently on my computer:

It can be seen that there are currently 5 databases, among which the mysqladv database is customized by us, and the remaining 4 databases belong to the system database that comes with MySQL. Let's take a look at the contents of the data directory:

Of course, there are many files and subdirectories in this data directory, but if you look carefully, except for the system database information_schema, other databases have corresponding subdirectories in the data directory. This information_schema is quite special, we will talk about its function later.

3.2.2, the representation of the table in the file system

Our data is actually inserted into the table in the form of records, and the information in each table can be divided into two types:

1. Definition of table structure

2. data in the table

The table structure is what is the name of the table, how many columns are there in the table, what is the data type of each column, what constraints and indexes are there, what character set is used and various information of comparison rules, all of which are reflected in In our table creation statement. In order to save this information, both InnoDB and MyIASM storage engines create a file specially used to describe the table structure in the corresponding database subdirectory under the data directory. The file name is as follows: table name.frm

For example, we create a table named test under the lijin database:

Then a file named test.frm will be created in the subdirectory corresponding to the database mysqladv to describe the table structure. The suffix .fm is stored in binary format.

cd

What file is the data in the table stored in? On this issue, different storage engines are different. Let’s take a look at what files InnoDB and MyISAM use to save the data in the table.

3.2.3, how lnnoDB stores table data

InnoDB's data will be placed in the concept of a table space or file space (English name: table space or file space). This table space is an abstract concept, which can correspond to one or more real files on the file system (different table spaces) The corresponding number of files may vary). Each table space can be divided into many, many pages, and our table data is stored in some pages under a certain table space. There are several types of tablespaces.

System table space (system tablespace)

This so-called system tablespace can correspond to one or more actual files on the file system. By default, InnoDB will create a file named ibdata1 in the data directory (look for it in your data directory), size It is a 12M file, which is the representation of the corresponding storage tablespace on the file system.

This file is a so-called self-expanding file, that is, it will increase the file size when it is not enough. Of course, if you want the system tablespace to correspond to multiple actual files on the file system, or just think that the original ibdata1 file name It sounds ugly, then you can configure the corresponding file paths and their sizes when MySQL starts. We can also configure the file paths corresponding to the system tablespace not in the data directory, or even configure them on a separate disk partition.

One thing to note is that in a MySQL server, there is only one copy of the system tablespace. In each version from MySQL5.5.7 to MySQL5.6.6, the data in our table will be stored in this system tablespace by default.

Independent table space (file-per-table tablespace)

In MySQL5.6.6 and later versions, InnoB does not store the data of each table in the system tablespace by default, but creates an independent tablespace for each table, that is, how many tables we have created, There are as many independent table spaces as there are. If you use an independent table space to store table data, a file representing the independent table space will be created in the subdirectory corresponding to the database to which the table belongs. The file name is the same as the table name, except that an extension of .ibd is added, so The complete file name looks like this: table name.ibd.

For example, if we use an independent table space to store the test table under the lijin database, then these two files will be created for the test table in the lijin directory corresponding to the database where the table is located:

test.frm和test.ibd

The test.ibd file is used to store the data and indexes in the test table. Of course, we can also specify whether to use the system table space or an independent table space to store data. This function is controlled by the startup parameter

innodb_file_per_table control, for example, when we want to deliberately store table data in the system tablespace, we can configure it like this when starting the MySQL server:

[server]
innodb_file_per_table=0

When the value of imodb_file_per table is 0, it means using the system table space; when the value of innodb_file_per table is 1, it means using an independent table space. However, the inmodb_file_per_table parameter only works for newly created tables, and does not work for tables that have already allocated table spaces.

Other types of tablespaces

With the development of MySQL, in addition to the above two old table spaces, some different types of table spaces are now proposed, such as general tablespace (general tablespace), undo tablespace (undotablespace), temporary tablespace <temporary tablespace )wait.

3.2.4, How does MyISAM store table data

Data and indexes in MyISAM are stored separately. Therefore, different files are used in the file system to store data files and index files. And unlike InnoDB, MyISA does not have a so-called table space, and the table data is stored in the corresponding database subdirectory.

If the test_myisam table uses the MyISAM storage engine, three files will be created for the myisam table in the lijin directory corresponding to the database where it is located:

Among them, test_myisam.MYD represents the data file of the table, which is the user record we inserted; test_myisam.MYI represents the index file of the table, and the index we created for the table will be placed in this file.

3.3, log files

During the running of the server, various logs will be generated, such as regular query logs, error logs, binary logs, redo logs, undo logs, etc. The log files record various types of activities that affect the MySQL database.

Common log files are: error log (error log), slow query log (slow query log), query log (query log), binary file (bin log).

error log

The error log file records the process of starting, running, and closing MySQL. When encountering problems, you should first check this file in order to locate the problem. This file not only records all error messages, but also records some warning messages or correct information

Users can use the following command to view the location of the error log file:

show variables like 'log_error'\G;

When MySQL fails to start normally, the first file you must look for should be the error log file, which records error messages.

slow query log

Slow query logs can help locate SQL statements that may have problems, so as to optimize the SQL statement level.

We already know that the slow query log can help locate SQL statements that may have problems, so as to optimize the SQL statement level. But the default value is off, we need to open it manually.

show VARIABLES like 'slow_query_log';

set GLOBAL slow_query_log=1;

Turn on 1, turn off 0

But how slow is slow? A threshold can be set in MySQL, and all SQL statements whose running time exceeds this value are recorded in the slow query log. The long_query_time parameter is this threshold. The default value is 10, representing 10 seconds.

show VARIABLES like '%long_query_time%';

Of course you can also set

set global long_query_time=0;

The default is 10 seconds, here is set to 0 for the convenience of demonstration

At the same time, if no index is used for the running SQL statement, the MySQL database can also record this SQL statement to the slow query log file. The control parameters are:

show VARIABLES like '%log_queries_not_using_indexes%';

1 on, 0 off (default)

show VARIABLES like '%slow_query_log_file%';

query log

Check if the current common log file is enabled

show variables like '%general%'

开启通⽤⽇志查询: set global general_log = on;
关闭通⽤⽇志查询:set global general_log = off;

sele

The query log records information about all requests made to the MySQL database, whether or not these requests were executed correctly.

Default filename: hostname.log

Binary log (binlog)

The binary log records all DDL and DML statements (except the data query statement select) in the form of events, and also includes the time consumed by the execution of the statement. The binary log of MySQL is transaction-safe

Several functions of binary logs:

Recovery: The recovery of some data requires a binary log. For example, after a database full backup file is restored, the user can perform point-in-time recovery through the binary file

Replication: Its principle is similar to recovery. A remote MySQL database (generally called slave or standby) and a MySQL database (generally called master or primary) are synchronized in real time by replicating and executing binary logs

Audit: Users can audit the information in the binary log to determine whether there is an injection attack on the database

log-bin parameter This parameter is used to control whether to open the binary log, the default is closed

If you want to enable the binary log function, you can specify the following format in the MySQL configuration file:

"name" is the name of the binary log file

If name is not provided, the database will use the default log file name (the file name is the host name, the suffix is ​​the serial number of the binary log), and the file is saved in the directory where the database is located (under datadir)

--Enable/set binary log file (name can be omitted)

log-bin=name;

After configuration, something similar to:

bin_log.00001 is the binary log file; bin_log.index is the binary index file, which is used to store the serial number of the binary log generated in the past. Normally, it is not recommended to modify this file manually.

Binary log files are not enabled by default, and parameters need to be manually specified to enable them. Enabling this option will affect the performance of MySQL, but the performance loss is very limited. According to the test in the official MySQL manual, turning on the binary log will reduce the performance by 1%.

Check if binlog is enabled

show variables like 'log_bin';

Modify my.cnf in the mysql installation directory

log_bin=mysql-bin
binlog-format=ROW
server-id=1
expire_logs_days =30

3.4, other data files

In addition to the data stored by the user as mentioned above, the data file also includes some additional files for better running the program. Of course, these files may not necessarily be placed in the data directory, and can be specified in the configuration file or at startup. Storage directory.

It mainly includes these types of files:

• Server process files.

We know that every time a MySQL server program is run, it means starting a process. The MySQL server will write its own process ID into a pid file.

socket file

Files required when connecting with UNIX domain sockets.

· Default/auto-generated SSL and RSA certificate and key files.

Guess you like

Origin blog.csdn.net/m0_70299172/article/details/130544085