Hulihan Applications
about projects portfolio services contact_us
Mysql Console Rake Task

    

    I just wanted to share a handy rake task that I found. It allows you to connect directly to a mysql console in a linux shell(granted the 'mysql' command is present). It reads in the mysql connection settings stored in config/database.yml so there's no need to enter in a user/database name or password. Here's the code you can add to lib/tasks/mysql.rake (or whatever) or Rakefile

def mysql_console(config)
  returning '' do |mysql|
    mysql << mysql_command << ' '
    mysql << "-u#{config['username']} " if config['username']
    mysql << "-p#{config['password']} " if config['password']
    mysql << "-h#{config['host']} "     if config['host']
    mysql << "-P#{config['port']} "     if config['port']
    mysql << config['database']         if config['database']
  end
end

def mysql_command
  'mysql'
end

desc "Launch mysql shell.  Use with an environment task (e.g. rake production mysql)"
task :mysql do
  system sh_mysql(YAML.load(open(File.join('config', 'database.yml')))[RAILS_ENV])
end

 Then you would run the task by typing:

 $ rake mysql

 Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 109184 to server version: 5.0.45-community-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

That's it!



0 Comments


Add A Comment



simple_captcha.jpg



Hulihan Applications © 2007-2009
No portion of this site may be copied, altered, duplicated or otherwise used without the express written approval of Hulihan Applications.