sha*_*nuo 4 io-redirection mysql
我可以将错误保存在与标准输出相同的文件中。我在下面使用了这种方法。问题是错误输出“总是”显示在顶部。在下面给出的示例中,错误与无法保存值“india”的倒数第二个 sql 命令有关。错误消息应显示在该语句旁边,而不是文件顶部。
# cat import.txt
drop table if exists testme;
create table testme (id int , name varchar(255));
insert into testme values (1, 'abc');
insert into testme values (2, 'abc', 'india');
insert into testme values (3, 'xyz');
# mysql test -vvf < import.txt >standard.txt 2>&1
# cat standard.txt
ERROR 1136 (21S01) at line 5: Column count doesn't match value count at row 1
--------------
drop table if exists testme
--------------
Query OK, 0 rows affected
--------------
create table testme (id int , name varchar(255))
--------------
Query OK, 0 rows affected
--------------
insert into testme values (1, 'abc')
--------------
Query OK, 1 row affected
--------------
insert into testme values (2, 'abc', 'india')
--------------
--------------
insert into testme values (3, 'xyz')
--------------
Query OK, 1 row affected
Run Code Online (Sandbox Code Playgroud)
预期的输出看起来像这样......
# mysql test -vvf < import.txt
--------------
drop table if exists testme
--------------
Query OK, 0 rows affected
--------------
create table testme (id int , name varchar(255))
--------------
Query OK, 0 rows affected
--------------
insert into testme values (1, 'abc')
--------------
Query OK, 1 row affected
--------------
insert into testme values (2, 'abc', 'india')
--------------
ERROR 1136 (21S01) at line 4: Column count doesn't match value count at row 1
--------------
insert into testme values (3, 'xyz')
--------------
Query OK, 1 row affected
Run Code Online (Sandbox Code Playgroud)
错误输出应该正好放在与其相关的语句旁边。否则重定向的输出文件没有用。
这实际上与 shell 无关,它是mysql命令行实用程序的“功能” 。
基本上,当 mysql 检测到输出不会到达终端时,它会启用输出缓冲。这提高了性能。然而,程序显然将成功输出发送到 STDOUT,将错误输出发送到 STDERR(确实有意义),并为每个输出保留一个单独的缓冲区。
解决方案只是添加-n到 mysql 命令参数。的-n(或--unbuffered)选项禁用输出缓冲。
例如:
mysql test -nvvf < import.txt >standard.txt 2>&1
Run Code Online (Sandbox Code Playgroud)