mysql2csv --user=dbuser --password=dbpassword --database=dbname --execute="select * from automobile_makes"
mysql2json --user=dbuser --password=dbpassword --database=dbname --execute="select * from automobile_makes"
mysql2xml --user=dbuser --password=dbpassword --database=dbname --execute="select * from automobile_makes"

To see all options

mysql2csv --help


a = :user => 'dbuser', :password => 'dbpassword', :database => 'dbname', :execute => "select * from automobile_makes"
a.to_stdout                                     # write it directly to $stdout
puts a.to_s                                     # get a string
a.to_path('/tmp/outfile.json')                  # write it to a file at this path
a.to_file('/tmp/outfile.json', 'w'))  # write it to a file handle

Note also that if you’re running it inside a Rails application, it will default to the ActiveRecord connection config.

a = :execute => "select * from automobile_makes"  # no need to specify user, etc. because it's taken from ActiveRecord


  • CSV output depends on FasterCSV to_csv

  • JSON output depends on Florian Frank’s JSON JSON.generate

  • XML output is written manually, but looks like the output of mysql [hyphen][hyphen]xml

Copyright 2011 Seamus Abshere