30

I am trying to execute foo.sql using the source command in MySQL.

When I type the command, the file is sourced accordingly:

mysql> source ~/foo.sql

Now, there are a lot of statements being executed in this file and I would like to review the success/failure of these statements. Is there a way I can pipe the results of the statements to a log file, foo.txt?

I am thinking something along the lines of:

mysql> source ~/foo.sql into outfile ~/foo.txt

However, typing this command appears to assume that everything following the source statement is one file name; so it is trying to source a file named '~/foo.sql into outfile ~/foo.txt', which obviously doesn't exist.

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
Isaac
  • 303
  • 1
  • 3
  • 4

3 Answers3

49

From within your MySQL client, type

tee session.out

From that point on, all the I/O of in your current client session is written to the file 'session.out'

  • 16
    Also, if you want to stop the output, just type `notee` ([source](http://dev.mysql.com/doc/refman/5.0/en/mysql-commands.html)) – adam_0 Apr 17 '13 at 21:02
  • I'd also jout like to highlight that you can use any text format for this e.g. `session.log` etc. – Ricky McMaster Dec 11 '18 at 11:15
4

You could do it from the shell prompt:

$ mysql -p dbname < foo.sql > foo.txt
Marc B
  • 356,200
  • 43
  • 426
  • 500
2

Use the command line:

mysql -p dbname < ~/foo.sql > ~/foo.txt
SorcyCat
  • 1,206
  • 10
  • 19
  • 1
    Did the database load as you wanted it to though? Only error messages will be added to the file. Oh, it will also stop on the first error, unless you give it a command line option to keep going past errors. – SorcyCat Nov 11 '10 at 15:07
  • 1
    It completed without error at first. I then tried it again and set my max_allowed_packet to 5M so it would intentionally error. It did get an error but foo.txt did not list this error. – Isaac Nov 11 '10 at 18:39