Created
April 1, 2009 14:13
-
-
Save FiXato/88699 to your computer and use it in GitHub Desktop.
mysqluser.rb — no more looking up mysql syntax for adding users…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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