MySQL Innodb storage engine (two)

  1. undo tablespace

Official document link: https://dev.mysql.com/doc/refman/5.7/en/innodb-undo-tablespaces.html

Role: used to do the undo operation

Storage: The 5.7 version is still in the shared tablespace (ibdataN) by default, and it is independent by default after the 8.0 version (undo_001, undo_002)

Production suggestion: After version 5.7, manually perform undo independently

undo tablespace management:
how to view undo configuration parameters

#打开独立undo模式,并设置undo的个数,3~5个(0 表示没有独立)

mysql> SELECT @@innodb_undo_tablespaces;
+---------------------------+
| @@innodb_undo_tablespaces |
+---------------------------+
|                         3 |
+---------------------------+
#undo日志的大小,默认1G。

mysql> SELECT @@innodb_max_undo_log_size;
+----------------------------+
| @@innodb_max_undo_log_size |
+----------------------------+
|                 1073741824 |
+----------------------------+
# 开启undo自动回收的机制(undo purge)

mysql> select @@innodb_undo_log_truncate;
+----------------------------+
| @@innodb_undo_log_truncate |
+----------------------------+
|                          0 |         0 表示不回收
+----------------------------+
# 触发自动回收的条件,单位是检测次数。

mysql> select @@innodb_purge_rseg_truncate_frequency;
+----------------------------------------+
| @@innodb_purge_rseg_truncate_frequency |
+----------------------------------------+
|                                    128 |        
+----------------------------------------+

Configure undo tablespaces
Official document description: https://dev.mysql.com/doc/refman/5.7/en/innodb-undo-tablespaces.html
Insert picture description here

The undo tablespace can only be changed when it is initialized, and cannot be changed in use (in ibdata, it cannot be changed)

Solution: table space migration (physical) and logical import.

# 初始化时配置:
第一历程:编写配置文件
# vim /etc/my.cnf
innodb_undo_tablespaces=3
innodb_max_undo_log_size=128M
innodb_undo_log_truncate=on
innodb_purge_rseg_truncate_frequency=32

第二历程:初始化数据
mysqld --initialize-insecure --user=mysql --basedir=/server/app/mysql --datadir=/server/data

第三历程:启动数据库
# systemctl restart mysqld
# ll -h /server/data/undo*      默认10M
-rw-r----- 1 mysql mysql 10M May  16 15:48 /server/data/undo001
-rw-r----- 1 mysql mysql 10M May  16 15:48 /server/data/undo002
-rw-r----- 1 mysql mysql 10M May  16 15:48 /server/data/undo003

How to perform Undo migration (independent storage)?

查看存储位置:
show variables like "%undo%";

a.关闭数据库
# /etc/init.d/mysqld stop

b.设定路径参数
# vim /etc/my.cnf 
innodb_undo_directory=/data/3306/undologs 

c.创建目录,并拷贝文件
mkdir -p  /data/3306/undologs 
chown -R mysql. /data/* 
#先复制过去,没问题,再删掉。
cp -a /data/3306/data/undo* /data/3306/undologs/      

d.启动数据库 
/etc/init.d/mysqld start
mysql
show variables like "%undo%";
  1. Temporary table space
    1. Function: store the internal temporary table created by the user-created temporary table optimizer
    2. How to manage?
# ll -h /server/3306/data/ibtmp1
-rw-r----- 1 mysql mysql 12M May  7 16:14 /server/3306/data/ibtmp1
#vim /etc/my.cnf

innodb_temp_data_file_path=ibtmp1:12M;ibtmp2:512M:autoextend:max:512M

# systemctl restart mysqld
        建议数据初始化之前设定好,一般2-3个,大小512M-1G。
  1. Doublewrite Buffer(DWB)

Introduction: Improve the reliability of innodb to solve partial write failure (partial page write page break).

Location: 5.7 is stored in ibdataN by default, and can be independent after 8.20

What problem does Doublewrite Buffer solve?

Then let's first mention a minimum unit of IO:

1. The smallest unit of MySQL default IO is (), and Oracle is ().

2. The smallest unit of file system IO is ()

3. The smallest unit of disk IO is ()

解析:16K,8K,4K (也有1K的)512B

Therefore, there is a risk of page damage caused by IO writes:

Insert picture description here

Doublewrite Buffer workflow
Insert picture description here

Doublewrite consists of two parts, one is the doublewrite buffer in the memory, the size of which is 2MB, and the other is the continuous 128 pages in the shared table space (ibdata x) on the disk, that is, 2 extents, and the size is also 2M.

1. When a series of mechanisms trigger the flushing of dirty pages in the data buffer pool, they are not directly written to the disk data file, but first copied to the doublewrite buffer in the memory;

2. Then write to the disk shared table space twice from the two write buffers (continuous storage, sequential writing, high performance), and write 1MB each time;

3. After the second step is completed, write the dirty page data in the doublewrite buffer to the actual tablespace files (discrete writing); (after the dirty page data is solidified, the corresponding doublewrite data can be overwritten by marking it)

  1. Change Buffer

Introduction: For example, insert, update, and delete data. For clustered indexes, the leaf nodes will be updated immediately. For the auxiliary index, it is not updated in real time. Put it in the change buffer

In the InnoDB memory structure, insert buffer (session) is added, and the current version is called change buffer.
Default size: mysql occupies 25% of memory.
Function: Temporarily buffer the updated data needed by the auxiliary index. When the data of the new insert needs to be queried, the merge operation is performed in memory, and the auxiliary index is the latest at this time.

For more exciting content, please pay attention to the WeChat public account

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45320660/article/details/114968654