Skip to content

Instantly share code, notes, and snippets.

@stephan-buckmaster
Created March 9, 2012 21:12
Show Gist options
  • Save stephan-buckmaster/2008727 to your computer and use it in GitHub Desktop.
Save stephan-buckmaster/2008727 to your computer and use it in GitHub Desktop.
mysql: drop all databases
# a ruby script
get_database_list_command = "mysql -u root -p -e 'show databases' | grep -v Database | grep -v 'information' | grep -v 'mysql'"
database_names = ` #{get_database_list_command} `.split(/\n/)
drop_databases_sql_statements = database_names.collect { |db| "drop database if exists `#{db}`;" if db.length > 0}.compact.join(' ')
puts 'Executing drop commands:'
puts drop_databases_sql_statements
puts drop_databases_sql_statements.split(/;/).join(";\n")
drop_databases_shell_command = "mysql -u root -p -e '#{drop_databases_sql_statements}'"
puts ` #{drop_databases_shell_command } `
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment