1

I know there're tons similar question but somehow I can't get it right.

When run this command, my "dump.sql" produced desired result

mysqldump -uuser -ppassword --comments --single-transaction --quick mydb desired_table | gzip >dump.sql.gz

This is the first line of dump.sql

-- MySQL dump 10.13  Distrib 8.0.21, for Linux (x86_64)

But with this command:

nohup mysqldump -uuser -ppassword --comments --single-transaction --quick mydb desired_table | gzip >dump.sql.gz 2>/dev/null &

This is the first line of dump.sql:

mysqldump: [Warning] Using a password on the command line interface can be insecure.

Which will cause error when import the dump file. How? From what I understand, "2>" means redirect error. If I change to this command, nothing in the "dump.log":

nohup mysqldump -uuser -ppassword --comments --single-transaction --quick mydb desired_table | gzip >dump.sql.gz 2>dump.log &
Coisox
  • 1,002
  • 1
  • 10
  • 22
  • It's a warning, not an error. You expected it to be written to stderr, but you've established it's written to stdout. – Elliott Frisch Nov 22 '22 at 12:11
  • @ElliottFrisch I dont really understand. Please enlighten. Do you mean "2" only referring to error but not warning? Is there any dedicated number represent warning? – Coisox Nov 22 '22 at 13:52
  • 1
    There is not. 2 **is** stderr. 1 **is** stdout. – Elliott Frisch Nov 22 '22 at 13:56
  • @ElliottFrisch based on ur comment, can I conclude that I can't redirect any warning unless I completely solve the warning (by using mysql_config_editor for example) right? – Coisox Nov 22 '22 at 14:00
  • 1
    `nohup mysqldump -uuser -ppassword --comments --single-transaction --quick mydb desired_table | grep -v Warning | gzip >dump.sql.gz 2>/dev/null &` will just ignore any warnings. – Elliott Frisch Nov 22 '22 at 14:10
  • Thank you @ElliottFrisch its working! Before this I've tried this but failed: ```nohup mysqldump -uuser -ppassword --comments --single-transaction --quick mydb desired_table | grep -v "mysqldump: [Warning] Using a password on the command line interface can be insecure." | gzip >dump.sql.gz 2>/dev/null &``` – Coisox Nov 22 '22 at 14:30
  • @ElliottFrisch please put ur comment as asnwer so that I can mark it as accepted answer – Coisox Nov 22 '22 at 14:31
  • 1
    It literally just ignores any warnings. This is not generally recommended. – Elliott Frisch Nov 22 '22 at 14:39
  • @ElliottFrisch even though not recommended, but you solved my original problem which is to ignore warning that cause problem in my desired output. SO I wish to mark you as accepted answer – Coisox Nov 22 '22 at 15:05

1 Answers1

2

To redirect the stderr of the mysqldump command before sending it to gzip, do that before the pipe.

nohup mysqldump -uuser -ppassword --comments --single-transaction \
   --quick mydb desired_table 2>/dev/null | gzip >dump.sql.gz  &

This is not related to your stderr redirection question, but I would also recommend using an options file instead of putting user and password on the command line.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • Thank you its working. I'm getting better of understanding after ur sample. But I wonder why with ur code, when I execute, the terminal back to prompt (as if the process has done but actually still running). Compare to my original code where the terminal display " nohup: ignoring input and redirecting stderr to stdout" and I have to CTRL+C to get the prompt – Coisox Nov 24 '22 at 02:40
  • The `&` at the end of the command line puts the whole process into the background. Here's a good article covering the basics of job control in the bash shell: https://www.digitalocean.com/community/tutorials/how-to-use-bash-s-job-control-to-manage-foreground-and-background-processes – Bill Karwin Nov 24 '22 at 04:44