Bug #100457 redirected stderr may apear before stdout from mysql
Submitted: 7 Aug 2020 15:04 Modified: 10 Aug 2020 12:01
Reporter: Shawn Green Email Updates:
Status: Verified Impact on me:
Category:MySQL Server: Command-line Clients Severity:S4 (Feature request)
Version:5.7, 5.7.31 OS:Linux
Assigned to: CPU Architecture:Any

[7 Aug 2020 15:04] Shawn Green
When executing a commands through mysql with verbosity enabled, the output will contain the commands being executed.  When redirecting that stdout to a file and adding stderr to it, sometimes the error message will appear before the command that generated the error. 

How to repeat:
Create a simple SQL script file with some good and at least one clearly wrong command: 
SELECT 'this is valid' as 'test'; 
SELECT this_is_invalid_syntax; 
SELECT @@now;

(notes: line 1 is valid, line 2 is intentionally wrong, line 3 was included in case you wanted to test using the --force option of mysql to proceed past errors)

Redirect that file into a mysql that has been started with some level of verbosity. I tested with two (-vv)

Redirect that output to a file and map stderr to stdout. 
mysql -u mylogin -p -vv <script.txt >results.txt  2>&1

mysql -u mylogin -p -vv --force <script.txt >results.txt  2>&1

Win(10)in cmd and Linux (oracle, el7) in bash will both have the error AFTER the command that generates it. This is correct behavior. 

linux ksh will put the error before the command in the results.txt file. 

Using TEE instead of redirection worked well on all platforms tested. 

Suggested fix:
Adjust something so that the system will record the error message AFTER the command that generated it when redirecting output to a file.
[10 Aug 2020 12:01] MySQL Verification Team
Hello Shawn,

Thank you for the feature request!