Mysql optimize table reports an error Temporary file write failure. Solution

Mysql optimize table reports an error Temporary file write failure. Solution

1. Problem description

When we execute optimize table xxx; when trying to defragment, it may also report an error Temporary file write failure.

  • Tips: MySQL 8.x
# 执行 optimize  命令时会发现报错
mysql>  optimize local table t_word;
+-------------------------+----------+----------+-------------------------------------------------------------------+
| Table                   | Op       | Msg_type | Msg_text                                                          |
+-------------------------+----------+----------+-------------------------------------------------------------------+
| abc.t_word | optimize | note     | Table does not support optimize, doing recreate + analyze instead |
| abc.t_word | optimize | error    | Temporary file write failure.                                     |
| abc.t_word | optimize | status   | Operation failed                                                  |
+-------------------------+----------+----------+-------------------------------------------------------------------+
3 rows in set, 1 warning (54 min 27.89 sec)


Check the mysql log file, you will also find an error [InnoDB] Error number 28 means 'No space left on device'

[ERROR] [MY-012639] [InnoDB] Write to file (merge) failed at offset 5405409280, 1048576 bytes should have been written, only 114688 were written. Operating system error number 28. Check that your OS and file system support files of this size. Check also that the disk is not full or a disk quota exceeded.
2022-05-10T14:00:58.817066+08:00 2272191 [ERROR] [MY-012640] [InnoDB] Error number 28 means 'No space left on device'

But in fact, my MySQL data disk space is enough to save twice the space of this table, so it means that this error (No space left on device) refers to the space of the system disk.

Tips: Since Online DDL was launched after MySQL 5.6.7, the optimize local table t_word I executed; will be executed in the form of Online DDL

In order to further verify this problem, check out the official documentation

https://dev.mysql.com/doc/refman/5.7/en/temporary-files.html

https://dev.mysql.com/doc/refman/5.7/en/innodb-online-ddl-space-requirements.html

insert image description here

According to the official document , Online DDL will need to create temporary log files, temporary sort files, and intermediate table files, and the temporary sort files (Temporary sort files) will be written to the temporary directory (tmpdir) of mysql.

  • Check the location of MySQL's tmpdir

    # 进入MySQL
    mysql> SHOW VARIABLES LIKE 'tmpdir';
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | tmpdir        | /tmp  |
    +---------------+-------+
    1 row in set (0.00 sec)
    
    # 回到Linux ,可见 /tmp 目录实际上是在系统盘(vda1)上的。(这个是默认位置)
    > df -h /tmp
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/vda1        20G  6.0G   13G  33% /
    
  • Here is a brief summary of these files

    • Temporary log files are innodb_sort_buffer_sizecontrolled by the variable, created in (data-dir).

    • Temporary sort files are created in (tmp-dir).

    • Temporary intermediate table files are created in (data-dir).

2. Solutions

As can be seen from the above, the only solutions left are:

1. Solution 1: Expand the system disk. After expanding the system disk, there will be enough space to store the temporary sorting files required by Online DDL. (Not recommended)

* 虽然Linux支持在线扩容,但是依然存在风险,所以不建议。

2. Solution 2: Set the directory location of tmpdir to other hard disks (not recommended)

  1. This is generally recommended on the Internet, but MySQL needs to be restarted to take effect, and the cost is too high, so it is not recommended.
    insert image description here
  2. Also note the modification method here (if you can accept restarting MySQL, you can do so)
    #1.查看 tmpdir
    mysql> SHOW VARIABLES LIKE 'tmpdir';
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | tmpdir        | /tmp  |
    +---------------+-------+
    1 row in set (0.00 sec)
    
    #2.创建 tmpdir目录
    
    mkdir -p /data/tmpdir
    chown -R mysql:mysql /data/tmpdir
    chmod a+w /data/tmpdir
    
    #3. 修改MySQL配置,设置 tmpdir 
    vim /etc/my.cnf
    把tmpdir设置到 /data/tmpdir
    tmpdir=/data/tmpdir
    
    # 4. 修改完成后,重启mysql服务
    service mysqld restart
    
    • Tips: The official suggestion is to configure multiple directories to share the load for the tmpdir variable.

3. Solution 3: Set it separately for online ddl innodb_tmpdir, this variable allows dynamic setting, and it will take effect without restarting mysql. 【recommend】

innodb_tmpdir : This option was introduced in MySQL 5.7.11 to help avoid possible temporary directory overflow due to large temporary sort files

Setting method:

	#注意: 执行前必须确保 /mnt/mysql-innodb-temp 目录已经创建了,并且给这个目录设置权限,确保MySQL能够读写改目录
 	mysql> set global innodb_tmpdir= '/mnt/mysql-innodb-temp'
 	```
	
下面是操作(踩坑)示例: 
	
```sh
# 注意: 必须先在系统上创建临时文件目录,并且确保mysql用户有权限访问这个目录
mysql> show VARIABLES like 'innodb_tmpdir';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| innodb_tmpdir |       |
+---------------+-------+
1 row in set (0.09 sec)

# 1. 设置   innodb_tmpdir
mysql> set global innodb_tmpdir= '/mnt/mysql-innodb-temp';
#  或者执行
mysql> set @@global.innodb_tmpdir=/mnt/mysql-innodb-temp

# 如果报错 ERROR 1231 (42000): Variable 'innodb_tmpdir' can't be set to the value of '/mnt/mysql-innodb-temp' 则说明这个目录跟 data-dir 重复了,如果没重复则执行这个已经查看详情
# 显示最近一次警告信息
# 语法: SHOW WARNINGS [LIMIT [offset,] row_count]
mysql> SHOW WARNINGS;
+---------+------+-------------------------------------------------------------------------------------------+
| Level   | Code | Message                                                                                   |
+---------+------+-------------------------------------------------------------------------------------------+
| Warning | 1210 | InnoDB: Path doesn't exist.                                                               |
| Error   | 1231 | Variable 'innodb_tmpdir' can't be set to the value of '/mnt/mysql-innodb-temp' |
+---------+------+-------------------------------------------------------------------------------------------+

# Path doesn't exist. 说明该目录不存在, 创建目录后再执行命令,如果发现依然报错,那么也可能是因为没有文件权限
mysql>  SHOW WARNINGS limit 10;
+---------+------+---------------------------------------------------------------------------------+
| Level   | Code | Message                                                                         |
+---------+------+---------------------------------------------------------------------------------+
| Warning | 1210 | InnoDB: Server doesn't have permission in the given location.                   |
| Error   | 1231 | Variable 'innodb_tmpdir' can't be set to the value of '/mnt/mysql-innodb-temp' |
+---------+------+---------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

# 那么设置该目录的文件权限即可。 
> chmod 777 /mnt/mysql-innodb-temp

# 再回到 mysql 发现执行成功了!
mysql> set @@global.innodb_tmpdir='/mnt/mysql-innodb-temp';
Query OK, 0 rows affected (0.00 sec)

# 检查下效果
mysql>  show VARIABLES like 'innodb_tmpdir';
+---------------+-----------------------------------+
| Variable_name | Value                             |
+---------------+-----------------------------------+
| innodb_tmpdir |/mnt/mysql-innodb-temp |
+---------------+-----------------------------------+
1 row in set (0.01 sec)

To summarize: innodb_tmpdir, this variable allows dynamic settings, and it takes effect without restarting mysql.

  • Valid values ​​are any directory path other than the MySQL data directory path (data-dir).

  • Setting innodb_tmpdir requires the user to have file permissions

  • This innodb_tmpdiroption was introduced to help avoid overflowing tmpfstemporary file directories located on the filesystem. ALTER TABLESuch overflows can occur due to large temporary sort files created during the online operation to rebuild the table.

  • In the master-slave replication mode, it is recommended innodb_tmpdirto configure it separately on each server.

3. After solving the above problems, execute DDL again, and you will find that the execution is successful

mysql> optimize local table t_word;
-------------------------+----------+----------+-------------------------------------------------------------------+
| Table                   | Op       | Msg_type | Msg_text                                                          |
+-------------------------+----------+----------+-------------------------------------------------------------------+
| abc.t_word| optimize | note     | Table does not support optimize, doing recreate + analyze instead |
| abc.t_word| optimize | status   | OK                                                                |
+-------------------------+----------+----------+-------------------------------------------------------------------+
2 rows in set (2 hours 29 min 54.20 sec)

Guess you like

Origin blog.csdn.net/UserFrank/article/details/124704420