views:

9404

answers:

19

Hi, moved a rails app of mine onto a new server and had to install a few gem dependencies. However, after installing the mysql gem I get the error, uninitialized constant MysqlCompat::MysqlRes, whenever I try to run a rake command that involves the mysql database. It seems I only get this error when I require the mms2r gem. Has anyone ever heard of this? I'm running mysql 2.8.1 gem.

+3  A: 

Yes, I saw a similar error message, looking for the same nonexistent class MysqlCompat::MysqlRes

As it turns out, that class should not exist; the error message is caused by a problem with the latest Mysql driver. mysql-2.8.1 looked for my libraries in a directory named with an extra level of 'mysql' at the end. For instance, my libraries (under MacOS X 10.5.8), are in /usr/local/mysql/lib, but the mysql.bundle library looks for the MySQL libraries at /usr/local/mysql/lib/mysql ... which is wrong.

I found it easy enough to fix, by creating a symbolic link in the /usr/local/mysql/lib named 'mysql', which points to '.'

So when the libraries are referenced at that extra depth, they are found, and work. This is definitely a hack; I'm sure there are more appropriate ways to fix this. I just need it to work so I can get on with other things.

Moreover, this may not work for you. It is likely to work if your error looks like:

  ./mysql.bundle: dlopen(./mysql.bundle, 9): Library not loaded: /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib (LoadError)
  Referenced from: /Users/ccullen/Projects/npapp-v2/mysql-ruby-2.8.1/mysql.bundle
  Reason: image not found - ./mysql.bundle
    from test.rb:5

Since this seems to be a recent development and I hadn't seen any direct response to it, I thought I'd share how I fixed it. Good luck :)

pandemic
+2  A: 

gem install -v=2.7.1 will fix it as well.

mml
only on Windows
davetron5000
A: 

I can't install the 2.7.1, how is it possible ? The hack above is not working for me :/

charles
He forgot to add the name of the gem to that line. He just means roll back to version 2.7.1. I think he meant:gem install -v=2.7.1 mysql
William
+2  A: 

Setting the proper ARCHFLAGS upon gem (re-)installation helped me.. without any symlinking required:

http://joergbattermann.tumblr.com/post/181479575/fixing-the-nameerror-uninitialized-constant

Jörg B.
That link is broken.
gdelfino
A: 

I've tried all of the above and none of them work for me either. When trying to install mysql version 2.7, I get the error:

sudo gem install -v=2.7.1 mysql -- --with-mysql-dir=/usr/local/mysql-5.1.39-osx10.5-powerpc/bin/mysql_config
ERROR:  could not find gem mysql locally or in a repository
F107PHD:stepgreen.org tdillahu$ sudo gem install -v=2.7 mysql -- --with-mysql-dir=/usr/local/mysql-5.1.39-osx10.5-powerpc/bin/mysql_config
Building native extensions.  This could take a while...
ERROR:  Error installing mysql:
    ERROR: Failed to build gem native extension.

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb --with-mysql-dir=/usr/local/mysql-5.1.39-osx10.5-powerpc/bin/mysql_config
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... no
checking for mysql_query() in -lmysqlclient... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
    --with-mysql-config
    --without-mysql-config
    --with-mysql-dir
    --with-mysql-include
    --without-mysql-include=${mysql-dir}/include
    --with-mysql-lib
    --without-mysql-lib=${mysql-dir}/lib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-mlib
    --without-mlib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-zlib
    --without-zlib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-socketlib
    --without-socketlib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-nsllib
    --without-nsllib
    --with-mysqlclientlib
    --without-mysqlclientlib


Gem files will remain installed in /Library/Ruby/Gems/1.8/gems/mysql-2.7 for inspection.
Results logged to /Library/Ruby/Gems/1.8/gems/mysql-2.7/gem_make.out

My original error: ake aborted! uninitialized constant MysqlCompat::MysqlRes /vendor/rails/activesupport/lib/active_support/dependencies.rb:440:in load_missing_constant' /vendor/rails/activesupport/lib/active_support/dependencies.rb:80:in const_missing'

boukie
To get around this issue I had to use the (--with-mysql-config= and not --with-mysql-dir): sudo gem install mysql -- --with-mysql-config=/usr/local/mysql-5.0.86-osx10.5-x86/bin/mysql_config.
boukie
That's fine if your mysql is installed under /usr/local/ but where do you find mysql_config if your mysql installed from the package manager?
cartoonfox
+1  A: 

sudo gem install mysql -v 2.7 should do the trick...

grosser
For OS X, this (2.7) should work; 2.7.1 doesn't exist for OS X
davetron5000
+20  A: 

on my Mac OS X 10.6 I did this to install the 2.8.1 gem, which worked perfectly with my MacPorts mysql5-server:

sudo env ARCHFLAGS="-arch x86_64" gem install mysql
Markus Strauss
Worked perfectly for me.
Rich Apodaca
When working with rvm, this line worked without the sudo at the front.
Kyle Heironimus
A: 

I had the same problem after upgrading from Fedora 10 to Fedora 12. I fixed it by just uninstalling mysql gem and installing it again. So it complies with the new version of MySQL.

Stanislav
A: 

sudo gem install mysql -v 2.7 -- --with-mysql-config=/usr/local/mysql/bin/mysql_config

Worked perfectly for me :)

André
A: 

sudo gem install mysql -v 2.7 -- --with-mysql-config=/usr/local/mysql/bin/mysql_config also did the trick for me when migrating over to my new Snow Leopard laptop. I did not use MacPorts to install mysql, but rather the binary, and had no luck with the arch flag solutions or symlinks.

Chris
A: 

sudo gem install mysql -v 2.7 -- --with-mysql-config=/usr/local/mysql/bin/mysql_config also worked for me when arch flag and symlinks didnt! THANKS!!

Matt Vague
A: 

sudo env ARCHFLAGS="-arch x86_64" gem install mysql

Dmg package from the mysql site an ddone!

Jet Abe
+1  A: 

This problem appeared with me, after upgrading Ubuntu from 9.10 to 10.04.

It appears to be a problem with the mysql gem.

Symptoms: Any use of ActiveRecord that requires a connection to mysql yields a stack trace coming from "uninitialized constant MysqlCompat::MysqlRes"

I tried a "gem update mysql" and found that I couldn't build native extensions because the compiler can't find the mysql_client library. That library (libmysqlclient.so.16) sits in /usr/lib.

Fix for ubuntu 10.04:

As root, I did these:

apt-get install libmysqlclient-dev
gem install mysql

I notice that several different people are trying things here and I wonder if we're not locating the root cause.

Variables that people need to clarify in their answers:

  • operating system version - which version of what are you using?
  • where and how did you install mysql? - people here seem to have installed their mysql from source under /usr/local (I installed mine through the ubuntu packaging system)
  • are you on a 64bit ubuntu or 32bit? (I'm currently on 32 bit.)
cartoonfox
+1  A: 

With mysql gem (2.8.1) I was getting

uninitialized constant MysqlCompat::MysqlRes

Using -v 2.7 make it work perfectly and for those who used Homebrew to install mysql

sudo gem install mysql -v 2.7 -- --with-mysql-config=/usr/local/Cellar/mysql/5.1.48/bin/mysql_config

Note that you can change mysql version if needed.

garno
A: 

On Mac OS X 10.6.4, I had to specify the architecture and config directory:

sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config

Paul Schreiber
+3  A: 

My problem was due to my "mysql" being a 32-bit installation while ruby was 64 and 32-bit. Check with these commands

 file `which mysql` 
 file `which ruby`

Both should match Mach-O 64-bit executable x86_64 or Mach-O 64-bit executable i386. I installed a 64-bit mysql, then

export ARCHFLAGS="-arch x86_64" ; sudo gem install --no-rdoc --no-ri mysql -v 2.7 -- --with-mysql-dir=/usr/local --with-mysql-config=/usr/local/mysql/bin/mysql_config

and voila!

Kuba
A: 

gem uninstall mysql

gem install mysql -v 2.7 -- --with-mysql-config=/usr/local/mysql/bin/mysql_config

ldconfig /usr/local/mysql/lib/mysql

RAILS_ENV=production rake db:migrate --trace

This worked for me.

Kialeve
A: 

finally, worked for me too on centos. ldconfig seems to be the trick after reinstalling the older gem. cheers!

Mark