RailsBack to Tutorials Sapphire (Ruby In Steel) Tutorials Archives Rails
Rails 2.2 and MySQL Problems
I just updated my Rails installation to the current release (2.2) earlier today - and, all of a sudden, my applications stopped working!
In spite of the fact that Rails now defaults to using the SQLite 3 database, I prefer to use MySQL, just as I always have done in the past. And that’s where the problem lay.
When I tried to run my Rails application, this error message appeared in my browser:
no such file to load — mysql
This was baffling as I certainly have MySQL installed, the entries in my Rails database.yml specify adapter: mysql plus a valid MySQL database. So what the heck was the matter?
A bit of hunting around on the web soon convinced me that I was not alone in experiencing this problem. The general advice seems to be to install the MySQL gem as a separate operation. I don’t know why this is needed with Rails 2.2. whereas it wasn’t with earlier versions, but anyway, it seems to be. So, I opened up a command prompt and entered the following:
gem install mysql
When it had finished installing, I fired up my application again and had another try. The good news was that the ‘no such file to load’ message had gone. The bad news was that it had been replaced with this message:
The specified module could not be found. - c:/ruby/lib/ruby/gems/1.8/gems/mysql-2.7.3-x86-mswin32/ext/mysql.so
I checked the specified directory and, sure enough, the file, mysql.so was there. For some reason, however, it appears that Rails is unable to find a required MySQL dll. The best fix I’ve been able to find (ok, I admit this is clunky, but anyhow it works) is to copy libmySQL.dll from your MySQL binary directory (for example, C:\Program Files\MySQL\MySQL Server 5.0\bin) into the Ruby binary directory (for example, C:\ruby\bin). Restart your application (shut down and restart the server), then try running it again. For me, that fixes the problem.
I don’t know why this error crept into Rails 2.2 and I’m hoping it will be fixed in a future version. MySQL is an important database, Windows is an important operating system and the easier it is to use Rails with MySQL on Windows the better!