Skip to content

Instantly share code, notes, and snippets.

@ghsatpute
Last active February 2, 2022 08:15
Show Gist options
  • Save ghsatpute/dde00e708ac9befca37748b7491db78d to your computer and use it in GitHub Desktop.
Save ghsatpute/dde00e708ac9befca37748b7491db78d to your computer and use it in GitHub Desktop.
How to log MySQL output to file

Approach 1: Redirect MySQL command line output to file

If your query output is too long, this is easy way to log to a file

  1. Open MySQL REPL
  2. On command prompt enter
    mysql> tee log.out
    Logging to file 'log.out'
    
  3. Now all your query output will be logged to file log.out on your working directory.
  4. After exiting the MySQL command line you can see the file output
    $ cat log.out 
    

Approach 2: Directly from command line

$ QUERY="select something from something"
$ MYSQL_HOST=myhost.mysql.com
$ MYSQL_USERNAME=readonly
$ MYSQL_PASSWORD=password
$ mysql -h $MYSQL_HOST -u$MYSQL_USERNAME -p$MYSQL_PASSWORD -se "$QUERY" >output.txt 2>&1

Now, see the output.txt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment