Posted on Jul 6, 2009

RubyInstaller: Getting Started with Rails and MySQL

This is a follow up instruction set from previous post, but this time, using MySQL

Update: Gems are not officially released, no need to add --source anymore.

Update: Please see RubyInstaller wiki for updated instructions, specially related to Rails 3.

Getting Ruby

These steps are the same for Ruby 1.9 or Ruby 1.8, please feel free to download the installer from here

For this guide I’m going to use Ruby 1.9.1-p129, since it the coolest new version that all the guys are playing with ;)

Now, start a Command prompt with Ruby (under start menu, inside Ruby 1.9.1-p129).

Command Prompt with Ruby 1.9

Getting the right MySQL version

While building the MySQL/Ruby bindings, we found that mixing versions of the bindings with different versions of MySQL installations ended on undesired results (abnormal program termination, weird errors, etc.)

For this guide, and because we are going to use binary gems, we are going to stick to MySQL version 5.0.83.

Now is time to download MySQL. For this guide, I’m going to install the essentials version, which contains only MySQL and command line tools, no Query Builder or any other administrative tool.

Please go to this page and download Windows Essentials (x86). Once downloaded you will end with mysql-essential-5.0.83-win32.msi file. Execute it and install with defaults.

Configure MySQL

If you’re an advanced and savvy MySQL user, you can skip the following steps. For the sake of this guide, I’m going to list the simple options you must follow when installing it.

Once you installed MySQL, the installer should have started the Configuration Wizard page.

Inside of it, please apply the following options:

Option/Screen Value
Configuration Type Detailed configuration
Server type Developer Machine
Database usage Transactional Database only
InnoDB datafile Your option or leave defaults
Number of connections Decision Support
Networking options Check Add firewall exceptions
Character set Best support for Multilingualism (UTF8)
Windows Options Add to PATH if you want mysql available on every prompt
Security Options Uncheck if you want root password be blank

Once done with all this, on the summary screen, please click Execute to complete the configuration of MySQL Server.

Under some system, starting of MySQL server will fail during this wizard, but don’t be afraid, most of the times, this can be safely ignored.

To check everything was properly installed, please go to the Start Menu, and inside MySQL Server 5.0, click on MySQL Command Line Client

If you changed the root password, or, like me, leaved it unchecked, you can simply press enter when asked for the password and see that the server is running!

MySQL Console Client

If you decided to add MySQL to the PATH, you will require to restart your computer so the PATH change is available to the system.

If you decided not to add MySQL to the PATH, please go, with Explorer to the location where you installed MySQL Server and copy libmySQL.dll into C:\Ruby19\bin

In my case, I found this file in C:\Program Files\MySQL\MySQL Server 5.0\bin

Now, it is time to install the bindings.

Getting MySQL/Ruby

For this version of Ruby, there is no official binary gems for both Ruby 1.8 and 1.9. So we are going to install the specially built version from RubyInstaller gems repository.

At the Command Prompt with Ruby, please enter the following command:

gem install mysql

This is going to install the special version of MySQL bindings. This version works with Ruby 1.8 and 1.9, since bundles fat binaries. You should expect a similar output like this:

Successfully installed mysql-2.8.1.1-x86-mingw32
1 gem installed

Getting Rails

Now is time to install Rails and build our application. At the same command prompt, please enter the following command:

gem install rails

This is going to take a bit, since Rails and it’s dependencies takes around 2MB or so, and need to be downloaded and installed.

Once done, expect see at the screen something like this:

Successfully installed activesupport-2.3.2
Successfully installed activerecord-2.3.2
Successfully installed actionpack-2.3.2
Successfully installed actionmailer-2.3.2
Successfully installed activeresource-2.3.2
Successfully installed rails-2.3.2
6 gems installed

Creating a Rails application

Let’s name our application mysqlapp

rails mysqlapp --database=mysql

The --database option indicates to Rails that we want to use MySQL instead of the default database adapter (SQLite3).

Rails will output a lot of lines when creating your application structure, just an excerpt of what to see:

...
      create  config/database.yml
      create  config/routes.rb
      create  config/locales/en.yml
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/new_rails_defaults.rb
      create  config/initializers/session_store.rb
      create  config/environment.rb
...

Configuring our Database

Now Rails have configured for us the name of the database we want to use, and you can verify it in config\database.yml

Rails will try to connect to mysqlapp_development, but that database do not exist in our fresh new MySQL server.

So, let’s create it:

cd mysqlapp
rake db:create

Just that, simple db:create is going to connect to our MySQL server, and create the database for us.

Keep in mind that if you changed root password or want to use other MySQL user to connect to the database, you need to edit database.yml to reflect those changes.

Let’s verify that everything is in place, using the following command:

ruby script\about

And you should see something like this as result:

About your application's environment
Ruby version              1.9.1 (i386-mingw32)
RubyGems version          1.3.4
Rack version              1.0 bundled
Rails version             2.3.2
Active Record version     2.3.2
Action Pack version       2.3.2
Active Resource version   2.3.2
Action Mailer version     2.3.2
Active Support version    2.3.2
Application root          C:/Users/Luis/mysqlapp
Environment               development
Database adapter          mysql
Database schema version   0

Now is up to you to create your models, controllers and views!

Some notes and considerations

On other post I’m going to guide you with steps on building the bindings against MySQL 5.1.36, since you will need to install the Ruby Development Kit and the development headers for MySQL.

If you find something wrong with the Ruby Installer, please report it here, but issues with your code, Rails or other are not responsability of RubyInstaller.

The binary gems provided at gems.rubyinstaller.org are based on our forks of mysql bindings, which can be cloned and explored here at GitHub.

Keep in mind that some gems would not work under Ruby 1.9, or you will need a compiler (DevKit) for it. See previous post with details how to get those from our download page.

37 Comments

  • Vit Ondruch says:

    Just a note. You recommend restart of system after changing of PATH environment variable, but that is not necessary. After change of PATH, it is enough to run new command line from start menu and the change should be already propagated.

    Nevertheless, you are right that with restart, you are on the safe side ;)

  • [...] RubyInstaller: Getting Started with Rails and MySQL – Quickstart instructions for Windows users. [...]

  • Luis says:

    You’re correct Vit. Wanted to avoid explaining about logging off the session, closing the open command prompt windows, etc. :)

    Depending on your Windows version, things work differently, so went the safest road ;)

    Thanks for your comment!

  • Roy Pardee says:

    This worked wonderfully on a windows 7 install. Many thanks!

    One thing though–the installer did not add c:\ruby19\bin to my path–I had to do that myself. Not sure if that’s intentional or not, or a win7 thing.

    Also–you’re missing the actual command “ruby script/about”. It was easy enough to refer to the sqlite walkthrough, but you may want to correct that.

    Thanks again for all your work! It’s great to know that windows isn’t going to be an orphaned platform for ruby.

  • Luis says:

    @Roy: Thank you, updated the instructions, missed that little snipped.

    The new installers do not alter your PATH, as I mentioned in this blog post and over ruby-talk

    The reason for that is that you require Administrator or Power User privileges, which limits the access of the installer. That’s the reason we went the Start Command Prompt with Ruby way, in similar fashion than tools like Visual Studio which do not alter your PATH, following Microsoft guidelines.

    Thank you for your kind words!

    Cheers.

  • rifraf says:

    “Let’s verify that everything is in place, using the following command:”
    …seems to have a bit of text missing here.
    As far as I can see, you run “ruby script\server” then browse to “http://localhost:3000/” and click on the “About your application’s environment” link.

    Apart from that thanks – finally I’m on the way to 1.9 with Windows

  • Luis says:

    To rifraf: No, I’ve used script\about, not started the server.

    The command was ruby script\about, and if you refresh, should be there (need to start using a more code block friendly wordpress theme).

  • Pimple says:

    Thanks Luis. One problem. When I click the “About your application’s environment” link, I get an error. Also when I run “rake db:create”, because it says “mysql with password => nil, ” so how do I change the defaults?

  • Luis says:

    Pimple: Did you check connecting to your local mysql worked? You must check that mysql password is empty (a empty space in your database.yml)

    If it says nil, then means there is no space in the configuration file. Please check that and search Google for hints.

  • [...] RubyInstaller: Getting Started with Rails and MySQL – DEV_MEM.dump_to(:blog) – Multimedia systems… [...]

  • [...] o Ruby ainda não é compatível com a versão 1.9. Voltando a pesquisar mais um pouco, encontrei esse post com a solução. No meu caso, ficou [...]

  • ITGuy says:

    I installed Ruby 1.86 with Rails 2.3.2 and MySQL 5.0. The command ruby script/dbconsole throws ab error:
    …dbconsole.rb:61 in ‘exec’: No such file or directory
    mysql.exe.

    I have compared dbconsole.rb and database.yaml to those in older version installations where the command did work and can find no differences. I believe my path statement is complete including the mysql/bin folder.

    Stumped! Can anyone help?

  • Luis says:

    @ITGuy:

    For mysql to work from command line (what is doing dbconsole in the background) you need MySQL in the PATH.

    For that, you need to add it manually or during MySQL installation check that option.

    I think the error is clear, Windows cannot find MySQL in the PATH.

  • [...] o Ruby ainda não é compatível com a versão 1.9. Voltando a pesquisar mais um pouco, encontrei esse post com a solução. No meu caso, ficou [...]

  • MySQL/Ruby 2.8.1 Released!…

    I’m happy to announce the release of MySQL Ruby bindings version 2.8.1
    This release is based on my GitHub fork from original Kevin Williams work.
    For those who are not aware, Kevin’s gem is a Gem package of tmtm mysql-ruby bindings at RubyF…

  • Dan says:

    Followed this to the letter and still getting this error:
    c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/connection_adapters/mysql_adapter.rb:585: [BUG] Segmentation fault
    ruby 1.8.6 (2007-09-24) [i386-mswin32]

    Any ideas? While we’re at it… I’ve seen lots of posts for this segmentation error using MySQL on Windows and various voodoo fixes, but has anyone actually figured out what the root problem is?

  • Luis says:

    @Dan: All the segmentation faults in relation to MySQL are because the internals of libmysql.dll You need to check if either in system32 or any other folder in your PATH the above mentioned DLL is from a different version.

    There is no Voodoo fixes, the root and the solution is the ones I commented:

    • Do not mix different versions of MySQL with your gem
    • Ensure you have a clean PATH free of other libmysql.dll files
  • I just want to say thanks. Installing MySQL 5.1 was my first mistake. I really appreciate this post – you saved me a lot of time. Regards.

  • Steve says:

    thanks so much, i have been trying to get RoR working for a couple of days before I came across this, now I’m all set.

  • Doug says:

    Thanks Luis for taking the time and making the effort to make all this possible. As a complete noob I finally have Ruby on Rails working on XP.

    An old installation of mysql 5.1.3 left some files after uninstalling it before installing 5.0.88. Once I deleted them, I ran into a problem because I used a password in the mysql setup step. This should have been no big deal, but when I edited the password field in database.yml, I didn’t leave a space between password: and beginning of password. Some hair pulling later and I now have rails talking to mysql.

    Hope this might help someone in future… leaving out the space in the password line will cause syntax error on line 24, col 2: ‘host: http://localhost’ Its not a problem with that line of database.yml, but the password line before. Leave out the space, and it won’t work.

  • [...] RubyInstaller: Getting Started with Rails and MySQL (similar to previous) [...]

  • [...] I had already installed XAMPP, which contains a MySQL server. For the most part, I followed Luis Lavena’s tutorial RubyInstaller: Getting Started with Rails and MySQL: [...]

  • Bond says:

    Thanks Lui, You’ve done Very good job. But I wonder where can I find your help in installing RoR with MySQL 5.1……

  • Luis says:

    @Bond: I promised a followup with instructions about compiling the mysql-ruby bindings using DevKit.

    I’m working on updating RubyInstaller right now, but will do the tutorial soon.

  • Matthew says:

    Great intro. It got me up and running instantly, after days of floundering with half a dozen other online tutorials.
    I notice you used the command
    >rails mysqlapp –database=mysql
    why didn’t you use this…
    >rails -d mysql mysqlapp
    Is there a difference?

  • Luis says:

    @Matthew: both -d and –database are equivalents.

    One is short and the other is long version, but are the same.

    I prefer to always use long version in the tutorials to make it more clear what and why I’m doing it.

    “-d” means a lot of different things in different contexts, eg: “daemonize” in script/server.

    So, –database is more clear, of course, verbose.

  • Indyfromoz says:

    Just letting you know that “http://gems.rubyinstaller.org” is dead. One needs to add “http://gems.rubyforge.org” or other gem repository before running the command “gem install mysql –source HTTP://REPOSITORYURL”

  • Luis says:

    Thank you, the dead of the repository was intentional. A proper gem is now officially available at rubygems.org:

    http://rubygems.org/
    http://rubygems.org/gems/mysql

  • George says:

    Worked like a charm. Thank you!

  • andre says:

    unfortunately doesn’t work for rails 3.

  • Luis says:

    Andre, it does, I’ve used Rails 3 with mysql adapter without issues.

    Please provide more details when saying that something doesn’t work, there are plenty of reasons.

  • Josef says:

    Hallo.
    I have problem with bundle install. When i run
    c:\work\rubyproj\testapp>rake db:create
    I got: “(in c:/work/rubyproj/testapp)
    Could not find gem ‘mysql2 (>= 0, runtime)’ in any of the gem sources.
    Try running `bundle install`.”
    When i tried run recommended “bundle install”, i got
    “Fetching source index for http://rubygems.org/
    C:/work/Ruby192/lib/ruby/1.9.1/rubygems/spec_fetcher.rb:47:in `stat’: No such fi
    le or directory – C:/Users/Uživatel (Errno::ENOENT)
    from C:/work/Ruby192/lib/ruby/1.9.1/rubygems/spec_fetcher.rb:47:in `init
    ialize’
    from C:/work/Ruby192/lib/ruby/gems/1.9.1/gems/bundler-1.0.0/lib/bundler/……”
    I understand that problem is unsupported unicode character in some process (why nobody fix it). But I do not want use My personal directory anyway.

    How can i change it?
    (I solve it during installation of rails by adding “ENV['HOME'] = “C:/work/rubyproj”” to bin/gem file, but it now does not work now, because it is not project directory)
    How can i say to ruby/rails that My personal directory in windows is not working directory for ruby projects?
    Why bundle does not use its parent directory? How can i force it?
    Please help. :(

  • Luis says:

    Josef,

    Please use 1.9.2, it supports unicode characters. as for mysql2, it doesn’t work on windows yet.

    This is not a RubyInstaller issue, nor it is MySQL gem for what it matters.

  • Josef says:

    Luis,
    thank you for response. But I used installer rubyinstaller-1.9.2-p0.exe and why i have lib/ruby/1.9.1 =(
    Or How can i install 1.9.2 lib, where is proper installer?

    And I dont use My personal forlder for projects (becaouse of many reasons). Why ruby/rails still asking for it :)? – There is nothing here of its interest :D everything is in C:/work/rubyproj

  • Luis says:

    Hello, please read Ruby 1.9.2 release notes and specially the FAQ section of it:

    http://www.ruby-lang.org/en/news/2010/08/18/ruby-1-9-2-is-released/

    Also, Ruby, RubyGems and other libraries might depend on your home directory. Get familiar with home directory concept from *NIX systems and feel free to define HOME environment variable to something else.

    Send you a smaller response to your email, hope you read this first before asking again.

  • [...] I had already installed XAMPP, which contains a MySQL server. For the most part, I followed Luis Lavena’s tutorial RubyInstaller: Getting Started with Rails and MySQL: [...]