mysql的又一个让人捉摸不透的bug?

时间:2024-01-18 00:05:32

这次就不说很多没有写博客了,因为前几天已经写过了。\^o^/

昨天我们刚讨论了关于自动化运维工作的实现方式,如果批量执行,中间出错怎么办?突然有人提出mysql支持--force,可以跳过出错继续执行。

那么我就想看看它这个功能是什么样子的,看看我能不能借鉴一下。

然后我就在一个测试机器上写入如下命令:

mysql -h127.0.0.1 -P3307 -uwzf -pxxxxxxx mysql --force -e "insert into myinfo (1,'asdf');insert into myinfo values(1,'asdf');

然后报错:

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
看上去是正确的,因为第一条是有语法错误,但第二条是正确的,这个表应该多一条数据,但查了几遍都是没有,那说明根本就没有执行,为什么?不是说可以跳过么?

我换一种方法,将上面的语句放到一个sql文件中,因为考虑到这只有是在batch批量执行的时候才出现的,放到文件中肯定是批量的吧?然后执行下面的语句:

mysql -h127.0.0.1 -P3307 -uwzf -pxxxxxxx mysql --force -e "source sql"

结果和上面是一样的,报错依旧,数据没有插入依旧!为什么?

然后又换了一种方式:

mysql -h127.0.0.1 -P3307 -uwzf -pxxxxxxx mysql --force < sql

啊?用这种方式,报错依旧,查了一次表,发现好像多了一条?为什么这样可以执行成功,通过-e方式指定就不能?

不行,我得看看代码

对于--force的参数解析,它将全局变量ignore_errors设置为1,表示忽略所有的错误。

发现对-e的解析是下面的代码:

case 'e':
status.batch= 1;
status.add_to_history= 0;
if (!status.line_buff)
ignore_errors= 0; // do it for the first -e only
if (!(status.line_buff= batch_readline_command(status.line_buff, argument)))
return 1;
break;

这里可以看到,处理-e时可能会将变量ignore_errors设置为0,这是在status.line_buff=NULL的情况下,但看了一下代码,如果有-e,则蓝色字体是设置status.line_buff的最早位置,那说明上面的ignore_errors肯定会被设置为0啊!!!

这么说来,我在命令中必须要将--force放到-e后面去?将上面出错的命令中--force放在-e后面重试一下?

mysql -h127.0.0.1 -P3307 -uwzf -pxxxxxxx mysql -e "insert into myinfo (1,'asdf');insert into myinfo values(1,'asdf'); --force

mysql -h127.0.0.1 -P3307 -uwzf -pxxxxxxx mysql -e "source sql" --force

此时执行发错,报错依旧,但正确的插入语句执行成功了!!!难道这是个bug?

突然又想到,这是不是故意这样子,而我才疏学浅不知道?那我看看文档怎么写的:

• --execute=statement [237], -e statement
Execute the statement and quit. The default output format is like that produced with --
batch [235]. See Section 4.2.3.1, “Using Options on the Command Line”, for some examples.
With this option, mysql does not use the history file.
• --force [237], -f
Continue even if an SQL error occurs.

这完全没有说啊!!

随便提一个改法啊,这是随便提的,没有深入研究,别批我就好了

if (!status.line_buff)
ignore_errors= 0; // do it for the first -e only
if (!(status.line_buff= batch_readline_command(status.line_buff, argument)))
return 1;

将上面4行中的前面2行放到后面2行之后去,换一下位置,这样先初始化status.line_buff就不会出现这个问题了。

mysql代码的bug还是比较多的。