MySQL binding problem on Max OS X 10.4

Hi all.

No matter what I do I can’t get rails to see my MySQL database. All
details are below. I really appreciate any assistance. Thanks!!!

DETAILS

  1. I’m using Rails 1.1.4 with Ruby 1.8.4 on Mac OS X 10.4.7. I have
    installed the MySQL bindings following the instructions here:

http://www.tonyarnold.com/articles/2005/08/11/mysql-bindings-for-ruby-under-mac-os-x-tiger

This seemed to make and install properly.

  1. My database is working fine, with schema names per the convention and
    matching database.yml:

development:
adapter: mysql
database: test_v1_development
username: root
password: *****
host: localhost

  1. I put the following code into the environment.rb file in the
    Rails::Initializer section:

test database

printf “Establishing DB connection…”
ActiveRecord::Base.establish_connection
puts “ok!”

puts “Testing DB connection …”
begin
tables = ActiveRecord::Base.connection.select_all(‘SHOW TABLES’)
rescue => err
puts ‘===== database connection failed =====’
puts “Missing database bindings for #{err.blamed_files}” if err.message
=~ /^uninitialized constant Mysql$/
raise
end
puts “Database tested OK, found #{tables.size} tables”

  1. When I start the WEBrick server I get the following error:

Establishing DB
connection…/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:194:in
establish_connection': development database is not configured (ActiveRecord::AdapterNotSpecified) from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:185:in establish_connection’
from ./script/…/config/…/config/environment.rb:16
from
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.4/lib/initializer.rb:40:in
run' from ./script/../config/../config/environment.rb:13 from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in require’
from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in
require' from /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.4/lib/commands/servers/webrick.rb:52 from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in require’
from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in
require' from /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.4/lib/commands/server.rb:30 from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in require’
from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in
`require’
from ./script/server:3
from -e:4

Dennis, thanks for your suggestions. Unfortunately, I tried both with no
success. Get the same error as in my original post.

/afb

Two quick suggestions:

Have you tried accessing MySQL through the local socket file:

development:
adapter: mysql
database: dev
username: dev
password: dev
socket: /tmp/mysql.sock

If you intend on using a hostname then try specifying a port:

development:
adapter: mysql
database: test_v1_development
username: root
password: *****
host: localhost
port: 3306

I’m not certain if the port is mandatory, but I always specify it when
using a hostname.

Regards,
Dennis

I can recreate this same error on my Mac if I completely remove the
adapter
line. So I think this might be something in the format of your
database.ymlfile. You might want to check your indents and spacing,
especially that
there is a space between adapter: and mysql.

HTH,
Jeff

Well, now that I reread your problem description, it may be that you put
this:

   puts "Missing database bindings for #{err.blamed_files}" if

err.message
=~ /^uninitialized constant Mysql$/
raise
end
puts “Database tested OK, found #{tables.size} tables”

inside the Rails::Initializer block. It can’t go there. It has to be at
the
end of the file, after this comment:

Include your application configuration below

Jeff

Thanks for the suggestion, Jeff. My database.yml is unchanged from the
stub installed by Rails when I created the application. However, I did
try removing tabs and replacing them with spaces, removing all spaces,
etc., and ensured there was a space between the colon and the adapter
name. No success.

If you can post the text of your database.yml file, though, I can try
copying and pasting to see if that fixes the problem.

/afb

Jeff, I tried that originally and the test never ran at all.

I’ve done some more testing and even more stumped than ever.

I created a brand new application and a new database schema. One table
and a scaffold controller. Works!

Then I copied the database.yml and environment.rb from the new, working
application to the broken one. Changed the schema name in database.yml.

Still doesn’t work. (Note that my debugging code is no longer in
environment.rb, so I’m judging the failure from a more prosaic “You have
a nil object when you didn’t expect it!” error generated by a
controller.

However, you have inspired me to check and see if the problem is
elsewhere. Perhaps the error I first reported was a red herring caused
by the debug code being in the wrong place in environment.rb. I’ll look
into this further and see if my problems really lie with a missing
adapter.

Thanks!

/afb

Problem solved, but in the worst kind of engineering way: I don’t know
why.

I recreated the application, following the steps I used for the simple
test referred to in the previous item. Everything works. I’ll just
modify from here and see if any one step causes the problem I saw
before.

I guess the one take-away is that I should not over-configure a new
application without testing its connectivity with a simple framework
first.

Thanks for your input, everyone.

/afb