This blog of mine is quickly becoming an encyclopedia of all the crazy problems that can happen if you're not careful. I'll have to do some more right-brained thinking for my next entry... At any rate, if you've been using Rails with MySQL, there's a hair-pulling problem you may run across when generating a new project from scratch, now that Rails 2.0 has SQLite3 as the default database.
Fire up a new rails application and add :
rails test_app
and then pop a simple migration in there:
cd test_app/ ./script/generate model User login:string password:string
Now, being up on our Rails news, we know that config/database.yml is set up by default for SQLite3, so let's change it to work properly (being careful to avoid tabs---only spaces allowed in YAML). Here's a development section as an example (please read the whole article before trying to use this, though):
development: adapter: mysql database: test_app_development user: test_user password: test_password host: localhost
Now, we can just
rake db:migrate
and be off on our merry way, right? Well, of course not---we know we need to create the database using mysql or mysqladmin first:
$ mysql -p > create test_app_development; > grant all privileges on test_app_development.* to 'test_user'@'localhost' identified by 'test_password';
But there's still a problem somewhere! Because you get
rake aborted! Access denied for user 'root'@'localhost' (using password: YES)
when you try to rake db:migrate. Why is it looking for 'root' when you've already specified 'test_user' in the database.yml and your config/environment.rb designates that you are indeed in development mode?!?
Well, I was dismayed to find out that the hours I spent researching this problem were because of a typo in config/database.yml above:
user: test_user
NO!
username: test_user
YES!
So, long story short, if you get the dreaded
Access denied for user 'root'@'localhost'
using rake db:migrate or anything else in rails, and you have the database created and the user there matches the user in database.yml, PLEASE check well for typos in database.yml, because it's finicky and Rails won't let you know if there's a problem.


Please wait… 


Posted by Jorge Alvarez on August 26, 2008
Thank you very much!!!
You saved my life.