16_MySQL data file

data files

Each database in MySQL will have a folder named after the database in the defined (or default) data directory, which is used to store various table data files in the database. Different MySQL storage engines have their own different data files. For example, MyISAM uses ".MYD" as the extension, Innodb uses ".ibd", Archive uses ".arc", CSV uses ".csv", and so on.

//Log in to mysql, create a database such as t1, and create a table in the database

mysql> create database t1;
Query OK, 1 row affected (0.00 sec)

mysql> use t1;
Database changed
mysql> create table tt1 
    -> (
    -> id int,
    -> name char(22),
    -> info char(22),
    -> deptid int
    -> );
Query OK, 0 rows affected (0.01 sec)

//Check the directory where the database is located, and you will find that there is a folder named after the database in the data directory.
Insert picture description here
View the file list of the t1 directory.
From the above figure, you can see that the table uses the innodb storage engine.

Modify the default storage engine of mysql

//View the mysql storage engine command, Support is: Default means the default storage engine

mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)

//Set InnoDB as the default engine
Add the sentence default-storage-engine=INNODB under [mysqld] in the configuration file my.cnf

[root@mysqld01 ~]# grep default /etc/my.cnf 
default-storage-engine=MyISAM 
[root@mysqld01 t1]# systemctl restart mysqld

//verification

mysql> mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB             | YES     | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| MyISAM             | DEFAULT | MyISAM storage engine                                          | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)

1. The metadata (meta) information related to the ".frm" file and the table is stored in the ".frm" file, including the definition information of the table structure, etc. Regardless of the storage engine (the two commonly used storage engines for MySQL are MyISAM and InnoDB), each table will have a ".frm" file named after the table name. All ".frm" files are stored in the folder of the database.
MyISAM database table file: .MYD file: table data file; .MYI file: index file

2. ".MYD" files ".MYD" files are dedicated to the MyISAM storage engine and store the data of MyISAM tables. Each MyISAM table will have a ".MYD" file corresponding to it, which is also stored in the folder of the database to which it belongs, together with the ".frm" file.

3. ".MYI" file ".MYI" file is also exclusive to MyISAM storage engine, mainly storing index related information of MyISAM table. For MyISAM storage, the content that can be cached is mainly from the ".MYI" file. Each MyISAM table corresponds to a ".MYI" file, which is stored in the same location as ".frm" and ".MYD".

InnoDB uses tablespace (tablespace) to manage data, store table data and indexes. .ibd file: single table table space file, each table uses a table space file (file per table) to store user database table data and indexes. InnoDB shared table space (that is, InnoDB file set, ibfile set): ibdata1, ibdata2, etc., store InnoDB system information and user database table data and indexes, shared by all tables.

4. ".ibd" file and ibdata file are both files that store Innodb data. The reason there are two files to store Innodb data (including indexes) is because Innodb's data storage method can be determined by configuration Whether to use a shared table space to store and store data, or to use an exclusive table space to store and store data. The exclusive table space storage method uses ".ibd" files to store data, and each table has a ".ibd" file, which is stored in the same location as the MyISAM data. If you choose a shared storage table space to store data, you will use an ibdata file to store it, and all tables use one (or multiple, self-configurable) ibdata files. The ibdata file can be configured by the two parameters innodb_data_home_dir and innodb_data_file_path. Innodb_data_home_dir configures the general directory for data storage, and innodb_data_file_path configures the name of each file. Multiple ibdata files can be configured in innodb_data_file_path at one time. The file can be of a specified size, or it can be automatically expanded, but Innodb restricts only the last ibdata file that can be configured as an automatic expansion type. When we need to add a new ibdata file, it can only be added at the end of the innodb_data_file_path configuration, and MySQL must be restarted to complete the addition of ibdata. But if we use the exclusive table space storage method, there will be no such problem.

Summary: Shared table spaces and exclusive table spaces are all based on the way data is stored. Shared table space: All table data and index files of a certain database are placed in one file. Exclusive table space: Each table will be generated and stored as an independent file. Each table has a .frm table description file and an .ibd file. This file includes the data content and index content of a single table. The advantages and disadvantages between the two shared table space: Advantages: The table space can be divided into multiple files and stored on each disk. Data and files are put together for easy management. Disadvantages: All data and indexes are stored in one file, and multiple tables and indexes are mixed and stored in the table space, so there will be a lot of gaps in the table space after a large number of delete operations on a table, especially for statistical analysis , The daily value system is the least suitable for applications such as shared table space. Independent table space: Advantages: 1. Each table has its own independent table space. 2. The data and indexes of each table will be stored in its own table space. 3. A single table can be moved in different databases. 4. Space can be reclaimed a) Drop table operation automatically reclaims table space. For statistical analysis or daily value tables, after deleting a large amount of data, you can pass: alter table TableName engine=innodb; to retract unused space. b) For tables that use an independent table space, no matter how you delete them, the fragmentation of the table space will not seriously affect the performance, and there are opportunities to deal with it. Disadvantages: The increase of a single table is too large, such as more than 100 G. In comparison, the efficiency and performance of using an exclusive table space will be higher.

//View the table space management type of the current database

mysql> show variables like '%innodb_file_per%';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| innodb_file_per_table | ON    |
+-----------------------+-------+
1 row in set (0.00 sec)

ON stands for independent table space management, OFF stands for shared table space management; (to view the table space management method of a single table, you need to check whether each table has a separate data file)

Innodb shared table space configuration: Modify the my.cnf file:

innodb_file_per_table=0
innodb_data_file_path=ibdata1:100M:autoextend
innodb_data_home_dir=/usr/local/mysql/data

Parameter explanation:
innodb_data_home_dir = "/path/" The directory where the database files are stored
innodb_log_group_home_dir = "/path/" The log storage directory
innodb_data_file_path=ibdata1:10M:autoextend Set a data file with an expandable size of 10MB (shared data file) , Named ibdata1. The location of the file is not given, so the default is in the MySQL data directory.
innodb_file_per_table=1|0 //1 means to use an exclusive table space, 0 means to use a shared table space. Note: InnoDB does not create a directory, so please confirm that the "configured path directory" does exist before starting the server.

Restart the mysqld service
//mysqld failed to start, check the error log

#tail -20 /usr/local/mysql/data/mysqld.err

The display content is as follows:

2019-12-29T06:53:44.635783Z 0 [ERROR] InnoDB: The Auto-extending innodb_system
data file '/usr/local/mysql/data/ibdata1' is of a different size 4864 pages
(rounded down to MB) than specified in the .cnf file: initial 6400 pages, max 0
(relevant if non-zero) pag

Note: Different versions of mysql report slightly different errors. Pay attention to the content of the error log. From the error log display, it can be seen that 6400 pages are set in the /etc/my.cnf file and the current ibdata1 is 4864 pages need to be calculated 4864/64=76. Modify the configuration to `

innodb_data_file_path=ibdata1:76M:autoextend

Restart mysqld service
Start mysql, success!

Note: Calculation formula: 64pages is equivalent to 1M, and 1page is 16KB. If you don’t know the default file page size, you can check it with du -hibdata1 first, and then set it;

Log in to mysql and execute mysql> show variables like'%innodb_file_per_table%';
then the newly created table will use the shared table space.

mysql>  show variables like '%innodb_file_per_table%';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| innodb_file_per_table | OFF   |
+-----------------------+-------+
1 row in set (0.00 sec)

3.Replication related documents:

1) The master.info file: The master.info file exists in the data directory of the Slave, which stores the relevant information of the Master of the Slave, including the host address of the Master, the connection user, the connection password, the connection port, and the current log location. Information such as the location of the read log.
2) The relay log and relay log index mysql-relay-bin.xxxxxn files are used to store the Binary Log information read by the I/O thread on the Slave side from the Master side, and then read from the relay log by the SQL thread on the Slave side And parse the corresponding log information, convert it into the SQL statement executed by the Master, and then apply it on the Slave side. The function of the mysql-relay-bin.index file is similar to that of mysql-bin.index. It is also the absolute path of the storage location of the record log, but what it records is not the Binary Log, but the Relay Log.
3) Relay-log.info file: similar to master.info, it stores the relevant information written to the local relay log through the Slave I/O thread. For the SQL thread on the Slave side and certain management operations to be able to obtain the relevant information of the current replication at any time.
4. Other documents:

1) system config file MySQL's system configuration file is generally my.cnf, which is stored in the "/etc" directory by default. The my.cnf file contains a variety of parameter options (groups), and each parameter group passes square brackets. A fixed group name is given. For example, the "[mysqld]" group includes the initialization parameters when the mysqld service is started, and the "[client]" group includes the parameters that can be read by the client tool program.

2) pidfile pid file is a process file of mysqld application in Unix/Linux environment. Like many other Unix/Linux server programs, it stores its own process id.

3) Socket file Socket file is also available only in Unix/Linux environment. In Unix/Linux environment, the user can directly use Unix Socket to connect to MySQL without TCP/IP network. There are two connection methods for mysql. The commonly used one is tcp mysql -h mysql host ip -uroot -pxxx mysql -S /path/mysql.sock Note: The unix socket connection method is faster than the tcp method, but only applicable Both mysql and the application are on the same PC.

Guess you like

Origin blog.csdn.net/weixin_45310323/article/details/112253967