Sort By Tag: Databases

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!


Powered_by_amethyst
Archive
Search
Tags
          Databases (1)
          General (2)
          Hosting (1)
          Hulihan Applications (3)
          Programming (1)
          Ruby (2)
          Ruby on Rails (6)
          Servers (1)
          Software (2)
view rss feed
This site is best displayed in FireFox 1.5+, IE 7+, and Opera 9+