Ruby 1.9.2 under RVM and MySQL already initialized constant warnings
-
26-09-2019 - |
Question
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.
Solution
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