Tuesday, July 6, 2010

Ruby on Rails decryption - MySQL

Installation is easy! Just make sure you have mysql installed, install the mysql gem, and voila, right?

Well maybe for you, but not for me. I wanted to write to share some of my headaches in case they were also your headaches. I had mysql installed, and I also installed the mysql gem, and now this kept happening on the server log:


[10-07-06:14:54 brian@koolaid /Users/brian/rails/feedtester] script/server start
=> Booting Mongrel
=> Rails 2.3.8 application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
/!\ FAILSAFE /!\ Tue Jul 06 14:55:26 -0700 2010
Status: 500 Internal Server Error
uninitialized constant MysqlCompat::MysqlRes
/Users/brian/.gem/ruby/1.8/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:440:in `load_missing_constant'
/Users/brian/.gem/ruby/1.8/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:80:in `const_missing'
/Users/brian/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/mysql_adapter.rb:9:in `define_all_hashes_method!'
/Users/brian/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/mysql_adapter.rb:68:in `mysql_connection'
/Users/brian/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:223:in `send'
/Users/brian/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:223:in `new_connection'
/Users/brian/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:245:in `checkout_new_connection'
/Users/brian/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:188:in `checkout'
/Users/brian/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:184:in `loop'
/Users/brian/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:184:in `checkout'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/Users/brian/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:183:in `checkout'
/Users/brian/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:98:in `connection'
/Users/brian/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:326:in `retrieve_connection'
/Users/brian/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/connection_specification.rb:123:in `retrieve_connection'
/Users/brian/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/connection_specification.rb:115:in `connection'
/Users/brian/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/query_cache.rb:9:in `cache'
/Users/brian/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/query_cache.rb:28:in `call'
/Users/brian/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:361:in `call'
/Users/brian/.gem/ruby/1.8/gems/actionpack-2.3.8/lib/action_controller/string_coercion.rb:25:in `call'
/Users/brian/.gem/ruby/1.8/gems/rack-1.1.0/lib/rack/head.rb:9:in `call'
/Users/brian/.gem/ruby/1.8/gems/rack-1.1.0/lib/rack/methodoverride.rb:24:in `call'
/Users/brian/.gem/ruby/1.8/gems/actionpack-2.3.8/lib/action_controller/params_parser.rb:15:in `call'
/Users/brian/.gem/ruby/1.8/gems/actionpack-2.3.8/lib/action_controller/session/cookie_store.rb:99:in `call'
/Users/brian/.gem/ruby/1.8/gems/actionpack-2.3.8/lib/action_controller/failsafe.rb:26:in `call'
/Users/brian/.gem/ruby/1.8/gems/rack-1.1.0/lib/rack/lock.rb:11:in `call'
/Users/brian/.gem/ruby/1.8/gems/rack-1.1.0/lib/rack/lock.rb:11:in `synchronize'
/Users/brian/.gem/ruby/1.8/gems/rack-1.1.0/lib/rack/lock.rb:11:in `call'
/Users/brian/.gem/ruby/1.8/gems/actionpack-2.3.8/lib/action_controller/dispatcher.rb:114:in `call'
/Users/brian/.gem/ruby/1.8/gems/actionpack-2.3.8/lib/action_controller/reloader.rb:34:in `run'
/Users/brian/.gem/ruby/1.8/gems/actionpack-2.3.8/lib/action_controller/dispatcher.rb:108:in `call'
/Users/brian/.gem/ruby/1.8/gems/rails-2.3.8/lib/rails/rack/static.rb:31:in `call'
/Users/brian/.gem/ruby/1.8/gems/rack-1.1.0/lib/rack/urlmap.rb:47:in `call'
/Users/brian/.gem/ruby/1.8/gems/rack-1.1.0/lib/rack/urlmap.rb:41:in `each'
/Users/brian/.gem/ruby/1.8/gems/rack-1.1.0/lib/rack/urlmap.rb:41:in `call'
/Users/brian/.gem/ruby/1.8/gems/rails-2.3.8/lib/rails/rack/log_tailer.rb:17:in `call'
/Users/brian/.gem/ruby/1.8/gems/rack-1.1.0/lib/rack/content_length.rb:13:in `call'
/Users/brian/.gem/ruby/1.8/gems/rack-1.1.0/lib/rack/chunked.rb:15:in `call'
/Users/brian/.gem/ruby/1.8/gems/rack-1.1.0/lib/rack/handler/mongrel.rb:67:in `process'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:159:in `process_client'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `each'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `process_client'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `run'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `initialize'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `new'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `run'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `initialize'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `new'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `run'
/Users/brian/.gem/ruby/1.8/gems/rack-1.1.0/lib/rack/handler/mongrel.rb:38:in `run'
/Users/brian/.gem/ruby/1.8/gems/rails-2.3.8/lib/commands/server.rb:111
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
script/server:3
Argh! Not good. I did some googling and saw suggestions about installing the 2.7.x gem because the 2.8.1 gem wasn't friendly with mysql 5.1 (which I found hard to believe, but the same error kept coming up).

I saw another suggestion about adding a compile flag to tell gem to compile the 64-bit version of the gem. That made sense to me, because I was running the whole kit and kaboodle on my macbook Intel Core Duo which, to my surprise, was a 64-bit processor. Hello apple! Okay, so I tried reinstalling the gem with some fitting compile flags:

[10-07-06:14:57 brian@koolaid /Users/brian/rails/feedtester] gem uninstall mysql
Successfully uninstalled mysql-2.8.1
[10-07-06:14:57 brian@koolaid /Users/brian/rails/feedtester] sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-dir=/usr/local --with-mysql-config=/usr/local/mysql/bin/mysql_config
Password:
Building native extensions. This could take a while...
Successfully installed mysql-2.8.1
1 gem installed
Installing ri documentation for mysql-2.8.1...

... snip ...

No definition for time_equal

No definition for error_errno

No definition for error_sqlstate
[10-07-06:14:58 brian@koolaid /Users/brian/rails/feedtester]
No errors... looking good. Now to kick up mongrel and try again:

dyld: lazy symbol binding failed: Symbol not found: _mysql_init
Referenced from: /Library/Ruby/Gems/1.8/gems/mysql-2.8.1/lib/mysql_api.bundle
Expected in: flat namespace

dyld: Symbol not found: _mysql_init
Referenced from: /Library/Ruby/Gems/1.8/gems/mysql-2.8.1/lib/mysql_api.bundle
Expected in: flat namespace

zsh: trace trap script/server start
And then mongrel dies right there after the first page load! WHA?? Okay, so the 32-bit compile of the gem throws an exception in ruby, and the 64-bit compile throws a dyld error that the system can't find _mysql_init. I was able to successfully reproduce the errors in irb as well, which at least tells me that it's not really a rails issue. Oh this is fun.

It then hit me... what if my install of mysql is what's wrong? Maybe rails can't find _mysql_init because it's a pointer that's not in the expected place or format? I did some checking out and found my version of mysql was 32-bit:

[10-07-02:14:00 brian@koolaid /Users/brian/rails/feedtester] mysql --version
mysql Ver 14.14 Distrib 5.1.43, for apple-darwin10.2.0 (i386) using readline 5.1
[10-07-02:14:00 brian@koolaid /Users/brian/rails/feedtester] file `which ruby` `which mysql`
/usr/bin/ruby: Mach-O universal binary with 3 architectures
/usr/bin/ruby (for architecture x86_64): Mach-O 64-bit executable x86_64
/usr/bin/ruby (for architecture i386): Mach-O executable i386
/usr/bin/ruby (for architecture ppc7400): Mach-O executable ppc
/usr/local/mysql/bin/mysql: Mach-O executable i386

Time to upgrade peoples! I won't get into the details of the upgrade, since the method of mysql installation is entirely up to you, and I know you'll have your own reasons for choosing why you installed mysql in the way you did. Anyway, I did the mysqldump to reserve my database content, installed the 64-bit version, did some clean-up, re-upped the database from the dump, and now my mongrel log looks like this:

[10-07-06:14:58 brian@koolaid /Users/brian/rails/feedtester] script/server start
=> Booting Mongrel
=> Rails 2.3.8 application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
SQL (0.1ms) SET NAMES 'utf8'
SQL (0.1ms) SET SQL_AUTO_IS_NULL=0


Processing MybooksController#index (for 127.0.0.1 at 2010-07-06 14:58:41) [GET]
Mybook Load (0.0ms) Mysql::Error: Table 'hb.mybooks' doesn't exist: SELECT * FROM `mybooks`

ActiveRecord::StatementInvalid (Mysql::Error: Table 'hb.mybooks' doesn't exist: SELECT * FROM `mybooks` ):
app/controllers/mybooks_controller.rb:5:in `index'

Rendered rescues/_trace (99.0ms)
Rendered rescues/_request_and_response (1.2ms)
Rendering rescues/layout (internal_server_error)

Well I didn't expect it to find the table, but at least it's not crashing and the exceptions are expected.

In conclusion, if you find your mysql gem isn't connecting, make sure you have the right versions and distributions for your system.

Thursday, July 1, 2010

Nostalgic spam

Weird! Today I got an email containing the "Microsoft/AOL Giveaway" note. In case you don't recall, the best write-up is here on snopes. I haven't seen this in, well, jeez... dunno how many years it's been. I was about to hit the delete button, but then I thought "wow, this is a part of history, kinda like walking the Internet equivalent of the John Muir trail." My mind boggles as I think of how much ancillary spam had likely been created from this email circling the globe so many times. I can't help but feel a bit of guilt for the young, naive, teenage version of myself who actually believed in and hoped for that check from nowhere that had no logical reason of existing. Besides, there's nothing more convincing than the words "I AM A LAWYER" in bold, caps, and H2 font size.

[delete]