I’m (finally) getting around to building a simple app with Rails. Installation of the various pieces (ubuntu, mysql, ruby, gems & rails) has gone surprisingly smoothly.[1]
I created three databases in mysql (dev, test & production), created and installed a simple schema making sure to use the proper table naming conventions, and made sure all the other prerequisites were taken care of.[2]
I was excited to finally get to the juice, generating a scaffold (or a trestle, as the case may be), that would allow simple create, update and delete on the tables.
Damn.
MySQL SConfiguration
Trying to generate either a scaffold or a trestle, I got the error
No such file or directory – /tmp/mysql.sock
This was a relatively straight-forward fix
- Add “socket: /var/run/mysqld/mysqld.sock” to my development db config in database.yml (this path is of course specific to debian/ubuntu).
Woo hoo! I’m ready to generate again…
Damn.
MySQL & Ruby on Rails password incompatibilites
This time generating a trestle I got an odd error
Before updating trestle from new DB schema , try creating a table for your model ()
And the same thing when trying to generate a scaffold:
Before updating scaffolding from new DB schema , try creating a table for your model ()
This one proved to be a bit more tricky to figure out.
I knew that my table used the proper naming conventions, and that I could access the development db just fine from the command line.
Based on stuff floating in various forums, I removed my password and lo & behold, I was able to generate scaffolds/trestles, as well as access the app without a problem.
But not having a password won’t do. At least now I had a direction to investigate.
As of MySQL 4.1, there’s a new password hashing algorithm, and previous versions of Rails had problems with the new hashing algorithm.
Turns out that there are a couple of key pieces of information that took a while to track down:
- Even though there’s a new password hashing algorithm in mysql, it doesn’t mean that it’s enabled by default. To see if your version of mysql is using old-style passwords, just do a “select user, password from user;” if your password is 16 characters long, mysql is set to use old-style passwords.
- The rails mysql driver is expecting the new-style 41-character hashed password.
All right, cool. So we just have to get them synched up: both of them using the old-style passwords, or both of them using the new-style. I went for trying to get both of them using the latest and greatest.
In my installation (via apt-get originally), there’s a section in /etc/mysql/my.cnf (line 46):
- For compatibility to other Debian packages that still use
- libmysqlclient10 and libmysqlclient12
old_passwords = 1
Now that we’ve identified the problem, the fix is simple:
- Comment out the “old_passwords” line in /etc/mysql/my.cnf
- Restart mysql
- Update the password for the user in mysql—set password for ‘username’@’localhost’ = password(‘password’);
- Make sure that your rails app database.yml file has the correct password
- Restart your rails server – WebBrick, mongrel, lighttpd, etc. (WebBrick & mongrel are easy; see this thread on how to do it for lighty)
After I did that, scaffolds & apps have been working like a charm.
[1] My environment is:
- ubuntu dapper drake (6.06)
- mysql 5.0.22
- ruby 1.8.4
- rails 1.1.6
Installation of the rails stuff followed “The Recommended Way” from this article on the Rails Wiki
[2] Prereqs:
- db: create the development database
- db user: set up a user to access the database (grant all privileges on *.* to ‘username’@’localhost’ identified by ‘password’)
- db table: plural lowercase w/ underscores (e.g. ‘user_profiles’)
- database.yml: make sure that the development database name, username & password are all correct
- scaffold model name: singular CamelCase (e.g. script/generate scaffold UserProfile)