Mysql创建临时表时的问题
问题描述:
首先是在执行一个脚本的时候(mysql < query.sql),中间会出现Lost connection during query的错误提示,但是错误日志里面没有记录。一开始想到是不是mysql时间相关的配置有问题,检查一下与时间相关的参数,基本上都很大,时间相关的参数这里就不赘述了。那么是什么问题导致的呢,没有日志,只有这个dt的错误。之后在相同的环境下测试了一个创建临时表的语句,哎,突然出现了错误,create temporary table tmp(id int) engine=InnoDB;会报错;
2013-11-07 16:54:02 8013 [ERROR] InnoDB:
Failed to set O_DIRECT on file /tmp/#sql1f4d_f_0.ibd: CREATE: Invalid argument,
continuing anyway
2013-11-07 16:54:02 8013 [ERROR] InnoDB:
O_DIRECT is known to result in 'Invalid argument' on Linux on tmpfs, see MySQL
Bug#26662
2013-11-07 16:54:02 8013 [ERROR] InnoDB:
Failed to set O_DIRECT on file /tmp/#sql1f4d_f_0.ibd: OPEN: Invalid argument,
continuing anyway
2013-11-07 16:54:02 8013 [ERROR] InnoDB:
O_DIRECT is known to result in 'Invalid argument' on Linux on tmpfs, see MySQL
Bug#26662
之后查看bug 26662,InnoDB should not print any message
when writing a temporary table in tmpdir
and when O_DIRECT is set.
http://bugs.mysql.com/bug.php?id=26662
http://bugs.mysql.com/bug.php?id=9709
http://bugs.mysql.com/bug.php?id=70298
因此定位到是与临时表有关系了。之后测试innodb_flush_method和tmpdir两个参数相关的配置下,mysql创建临时表的时候出现的问题
一、第一次测试:
1、在5.5.15版本下
(1)、tmpdir=/tmp并且tmp挂在为tmpfs的,innodb_flush_method = O_DIRECT
创建临时表时,mysql服务就直接重启了
(2)、设置tmpdir=/home/dba/lostConntionTest/tmp不是tmpfs的,innodb_flush_method
= O_DIRECT
创建临时表,没有任何问题
2、在5.6.11版本下
(1)、tmpdir=/tmp并且tmp挂在为tmpfs的,innodb_flush_method = O_DIRECT
创建临时表时,会有报错日志,
2013-11-07 16:54:02 8013 [ERROR] InnoDB: Failed to set O_DIRECT on
file /tmp/#sql1f4d_f_0.ibd: CREATE: Invalid argument, continuing anyway
2013-11-07 16:54:02 8013 [ERROR] InnoDB: O_DIRECT is known to result
in 'Invalid argument' on Linux on tmpfs, see MySQL Bug#26662
2013-11-07 16:54:02 8013 [ERROR] InnoDB: Failed to set O_DIRECT on
file /tmp/#sql1f4d_f_0.ibd: OPEN: Invalid argument, continuing anyway
2013-11-07 16:54:02 8013 [ERROR] InnoDB: O_DIRECT is known to result
in 'Invalid argument' on Linux on tmpfs, see MySQL Bug#26662
但是mysql服务不会重启
(2)、设置tmpdir=/home/dba/lostConntionTest/tmp不是tmpfs的,innodb_flush_method
= O_DIRECT
创建临时表,没有任何问题
这两个版本之间还有些不同了,那么定位,是否还有其他的参数相关的,两个测试机设置的不同呢,最后定位到innodb_use_native_aio
http://dev.mysql.com/doc/refman/5.5/en/innodb-parameters.html#sysvar_innodb_use_native_aio
查看第一次的测试环境,5.5和5.6的innodb_use_native_aio参数设置不同,进一步的统一这个参数的设置,测试时有如下的结果:
二、第二次测试
5.5.15版本的:
(1)、tmpdir=/tmp并且tmp挂在为tmpfs的;innodb_flush_method
= O_DIRECT;
innodb_use_native_aio=1
创建临时表,mysql直接重启了
(2)tmpdir=/tmp并且tmp挂在为tmpfs的;innodb_flush_method
= O_DIRECT;
innodb_use_native_aio=0
创建临时表时,会有报错日志,和5.6的第一次测试一样
(3)、tmpdir=/tmp并且tmp挂在为tmpfs的;innodb_flush_method
= O_DSYNC;
innodb_use_native_aio=0或者不设置innodb_flush_method
创建临时表OK,也没有报错
(4)、设置tmpdir=/home/dba/lostConntionTest/tmp不是tmpfs的,innodb_flush_method
= O_DIRECT或者O_DSYNC或者不设置;innodb_use_native_aio=1或者0
创建临时表OK,没有报错
5.6的和5.5的有一些不同,就是设置了tmpdir=/tmp并且tmp挂在为tmpfs的;innodb_flush_method
= O_DIRECT;innodb_use_native_aio=1;之后,启动mysql会报错,但是mysql服务还是会起来,之后查看innodb_use_native_aio任然为0,不是配置文件中设置的,这一点官网上面解释说,意思是,mysql自己会检测者几个参数的一致性
innodb_use_native_aio
Normally, you do
not need to touch this option, because it is enabled by default.
If a problem with the asynchronous I/O subsystem in the OS prevents
InnoDB from starting, start the server with this variable disabled (use
innodb_use_native_aio=0 in the option file). This option could also be turned
off automatically during startup, if InnoDB detects a potential problem such as
a combination of tmpdir location, tmpfs filesystem, and Linux kernel that that does
not support AIO on tmpfs.
5.5.15的貌似没有检查,如果这几个参数都保持一直,mysql5.5.和5.6的是一样的,5.0的版本没有AIO的特性,因此只考虑tmpdir和innodb_flush_method这两个参数,和5.5、5.6的一样的。
最终的结论是:
1、
tmpdir文件系统是tmpfs
innodb_use_native_aio=1
innodb_flush_method=任何一个参数
都会出现问题,而且mysql服务会重启
2、
tmpdir文件系统是tmpfs
innodb_use_native_aio=0
innodb_flush_method= O_DIRECT
会有报错,但是不会重启
3、
tmpdir文件系统是tmpfs
innodb_use_native_aio=0
innodb_flush_method= O_DSYNC或者Fdatasync
OK的,不会报错
4、
Tmpdir文件系统不是tmpfs的,另外的两个参数是什么都可以,不会报错
从上面的测试看到,解决上面创建临时表报错或者重启的问题,
* create your temp tables in myisam format (临时表是myisam的)
* remove O_DIRECT from innodb_flush_method (不使用O_DIRECT)
* use non-tmpfs filesystem for temp tables (tmpdir不使用tmpfs文件系统)