Skip to content

Instantly share code, notes, and snippets.

@FiXato
Created April 1, 2009 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FiXato/88699 to your computer and use it in GitHub Desktop.
Save FiXato/88699 to your computer and use it in GitHub Desktop.
mysqluser.rb — no more looking up mysql syntax for adding users…
#!/usr/bin/env ruby
require 'tempfile'
case ARGV[0]
when nil
abort "usage: mysqluser [add|] <arg(s)>"
when 'add'
abort "usage: mysqluser add <username> <password>" unless ARGV[2]
usernames = ["'#{ARGV[1]}'@'localhost'", "'#{ARGV[1]}'@'%'"]
mysql_commands = []
usernames.each do |username|
mysql_commands << "CREATE USER #{username} IDENTIFIED BY '#{ARGV[2]}';"
mysql_commands << "GRANT ALL PRIVILEGES ON *.* TO #{username} WITH GRANT OPTION;"
end
mysql_commands << "FLUSH PRIVILEGES;"
Tempfile.open('adduser.sql') do |tf|
File.open(tf.path,'w') do |f|
mysql_commands.each do |mysql_command|
f.puts mysql_command
end
end
puts `mysql -p --user=root mysql < #{tf.path}`
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment