views:

78

answers:

1

When running Rails 3 RC with Ruby 1.9.2.rc2 under RVM I keep getting a very large number of errors from the MySQL driver bundle that look like this:

/opt/local/rvm/gems/ruby-1.9.2-rc2/gems/mysql-2.8.1/lib/mysql_api.bundle: warning: already initialized constant MysqlRes
/opt/local/rvm/gems/ruby-1.9.2-rc2/gems/mysql-2.8.1/lib/mysql_api.bundle: warning: already initialized constant MysqlField
/opt/local/rvm/gems/ruby-1.9.2-rc2/gems/mysql-2.8.1/lib/mysql_api.bundle: warning: already initialized constant MysqlError
/opt/local/rvm/gems/ruby-1.9.2-rc2/gems/mysql-2.8.1/lib/mysql_api.bundle: warning: already initialized constant VERSION
/opt/local/rvm/gems/ruby-1.9.2-rc2/gems/mysql-2.8.1/lib/mysql_api.bundle: warning: already initialized constant OPT_CONNECT_TIMEOUT
/opt/local/rvm/gems/ruby-1.9.2-rc2/gems/mysql-2.8.1/lib/mysql_api.bundle: warning: already initialized constant OPT_COMPRESS
/opt/local/rvm/gems/ruby-1.9.2-rc2/gems/mysql-2.8.1/lib/mysql_api.bundle: warning: already initialized constant OPT_NAMED_PIPE
/opt/local/rvm/gems/ruby-1.9.2-rc2/gems/mysql-2.8.1/lib/mysql_api.bundle: warning: already initialized constant INIT_COMMAND

This shows up in rails console and unit tests, anything that requires the full Rails stack, but not a script that uses Sequel directly in the same environment.

Although the bundle itself does load and the MySQL driver does work, this enormous pile of warnings prefaces anything run through Rails. Usually this results from a redundant load of the mysql gem somewhere within the Rails environment. The gem is declared in the Gemfile:

gem 'rails', '3.0.0.rc'

gem 'haml'
gem 'sequel'

gem 'mysqlplus'
gem 'mysql'

I'd suppose this is the Rails autoloader failing to understand the Mysql library has already been loaded, and loading it again. Is there an easy way to fix this?

Update:

Load mysql or mysqlplus but not both at the same time or you will get warnings like this. mysqlplus includes all of the functionality of mysql and is a dependency of Sequel.

A: 

Do you need the mysqlplus gem? I am using Rails 3 with only mysql 2.8.1:

gem 'mysql', '2.8.1'

Although I have not used mysqlplus, I am guessing that it sets the constants you are seeing in the warnings, and then the mysql gem sets these constants again when it is loaded.

UPDATE: Use mysql2 instead

#gem 'mysql', '2.8.1'
gem 'mysql2'

You also need to update your database adapters in database.yml:

development:
  #adapter: mysql
  adapter: mysql2
  database: somedatabase_development
cowboycoded
After more research I found this in wycat's blog:"All of the Data Objects drivers, which we built for DataMapper, correctly cause a context switch when entering a blocking area of their C code. The mysqlplus gem, released in March 2009, was designed to be a drop-in replacement for the mysql gem, but fix this problem. The new mysql2 gem, written by Brian Lopez, is a drop-in replacement for the old gem, also correctly handles encodings in Ruby 1.9, and is the new default MySQL driver in Rails."
cowboycoded
gem 'mysql2' #in your Gemfile
cowboycoded
adapter: mysql2 #in database.yml
cowboycoded
The trick is that you load **either** `mysql` or `mysqlplus` but not both. That it's a replacement helps explain why.
tadman
Just to make it clear, I was talking about 3 different gems here. mysql, mysqlplus, and mysql2 (mysql v2.8.1 is not the same as mysql2). mysql2 is the preferred gem for Rails 3 according to the rails core team. So all you need is 'mysql2' instead of using 'mysql and mysqlplus' or 'mysql'
cowboycoded