Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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