#28000Access denied for user 'root'@'localhost' (using password: NO) (Mysql::Error)

Hi.
It's really a painful experience learning Ruby on Rails so far. It
costs me days to create a simple website and a simple application. I'm
using "Ruby on Rails for dummies". I'm doing "Programming with a Rails
Model" of Chapter 3, i tried to run the app as Ruby app. It keeps
giving me:

    c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/
active_record/vendor/mysql.rb:523:in `read': #28000Access denied for
user 'root'@'localhost' (using password: NO) (Mysql::Error)
    from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/
active_record/vendor/mysql.rb:153:in `real_connect'
    from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/
active_record/connection_adapters/mysql_adapter.rb:505:in `connect'
    from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/
active_record/connection_adapters/mysql_adapter.rb:183:in `initialize'
    from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/
active_record/connection_adapters/mysql_adapter.rb:88:in `new'
    from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/
active_record/connection_adapters/mysql_adapter.rb:88:in
`mysql_connection'
    from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/
active_record/connection_adapters/abstract/connection_specification.rb:
292:in `send'
    from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/
active_record/connection_adapters/abstract/connection_specification.rb:
292:in `connection='
    from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/
active_record/connection_adapters/abstract/connection_specification.rb:
260:in `retrieve_connection'
    from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/
active_record/connection_adapters/abstract/connection_specification.rb:
78:in `connection'
    from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/
active_record/base.rb:1145:in `columns'
    from my_ruby_code.rb:10

I googled and couldnt find something really helpful for my problem.
Really can't understand why MySQL is so hard to configure and error-
prone.
I think my database.yml is configured correctly as i could do
db:migrate and create scaffold and even view/edit the website with the
columns' values.

The code i tried to run is

require "activerecord"

class Photo < ActiveRecord::Base
end

Photo.establish_connection(
  :adapter => "mysql", :database => "album_development")

  for column in Photo.columns
    print column.name, "\t"
  end

  puts
  puts

  for photo in Photo.find(:all)
    for column in Photo.columns
      print photo.send(column.name), "\t"
    end
    puts
  end

When you do this, you are completely sidestepping database.yml (because you're not loading rails), so whatever credentials you put in there are not being used.

Fred

What i understand from the book is that the photo's column from the
table will be displayed on console once i run the code as ruby.
How can i bypass that error message and proceed?

What i understand from the book is that the photo's column from the
table will be displayed on console once i run the code as ruby.
How can i bypass that error message and proceed?

In your call to establish_connection you need to pass whatever
parameters you have in database.yml
I'm really not sure why the book isn't having you play via script/
console or (if it has to be a separate ruby class) script/runner.

Fred

Thank you very much, Fred. After i have passed those parameters, i'm
able to proceed. Your help has prevented me from struggling for
another few days i guess.

I don't understand why you should be using establish_connection and
frankly I question the accuracy of any book that would instruct a
beginner to use it.

The whole point of ActiveRecord is that you DON'T have to establish a
database connection yourself. ActiveRecord abstracts that away from
you.

That Photo.establish_connection call could be removed entirely and
your code will still work.

I took out Photo.establish_connection and rerun the application but it
gave me error:

c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/
connection_adapters/abstract/connection_specification.rb:265:in
`retrieve_connection': ActiveRecord::ConnectionNotEstablished
(ActiveRecord::ConnectionNotEstablished)
  from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/
active_record/connection_adapters/abstract/connection_specification.rb:
78:in `connection'
  from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/
active_record/base.rb:1145:in `columns'
  from my_ruby_code.rb:10

I took out Photo.establish_connection and rerun the application but it
gave me error:

The script isn't actually loading the rails environment, so the call
to establish_connection is needed.
But this is still a really weird way to go about things. You sure this
is a 'for dummies' book, not a 'by dummies' one :slight_smile:

Fred

I don’t understand why you should be using establish_connection and

frankly I question the accuracy of any book that would instruct a

beginner to use it.

The whole point of ActiveRecord is that you DON’T have to establish a

database connection yourself. ActiveRecord abstracts that away from

you.

I’d hardly say that that’s is the “whole point of ActiveRecord”, the point of ActiveRecord is to map an object model to a relational database.

Sans Rails, and AR can definitely be used without Rails, you do have to establish the database connection. The fact that Rails does this for you via database.yml is just one of those Rails convention over configuration things, although since database.yml is actually a config file, it’s really more “conventional configuration”

That Photo.establish_connection call could be removed entirely and

your code will still work.

As the OP has reported, it doesn’t.

I’m not familiar with the book in question, so I can’t speak for why it goes down this particular path, I suspect that for some reason the author(s) chose to do this.

I also suspect that the book uses a somewhat different MySQL configuration. MySQL IIRC can be configured so as to not require passwords. It’s actually not uncommon to run MySQL in such an unsecure configuration on a development (as opposed to a production) machine.

Hello Albert,
I have to agree with others here. It is extremely important to use
the right resources when learning something new. I don't have the
dummies book but when I was beginning Rails programming, Patrick
Lenz's Building Your Own Applications with Ruby on Rails book was very
helpful to me in getting the basics right. He has an updated version
of the book out for Rails 2 which is now called Simply Rails 2. I
don't have it since I don't need it anymore, but judging by his first
edition of the book, it could be very helpful. Don't spend your time
learning from sub-optimal resources.
Regards,
Bharat