68
votes

I upgraded to Snow Leopard using the disc we got at WWDC.

Trying to run some of my rails apps now complains about sql

    (in /Users/coneybeare/Projects/Ambiance/ambiance-server)
!!! The bundled mysql.rb driver has been removed from Rails 2.2. Please install the mysql gem and try again: gem install mysql.
Importing all sounds in /Users/coneybeare/Projects/Ambiance/ambiance-sounds/Import 32/Compressed/

 -- AdirondackPeepers.caf
!!! The bundled mysql.rb driver has been removed from Rails 2.2. Please install the mysql gem and try again: gem install mysql.
rake aborted!
dlopen(/opt/local/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.bundle, 9): Library not loaded: /usr/local/mysql/lib/libmysqlclient.16.dylib
  Referenced from: /opt/local/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.bundle
  Reason: image not found - /opt/local/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.bundle

(See full trace by running task with --trace)

I could have sworn I fixed this once before. The problem is that

sudo gem install mysql

does not work and gives the error:

 Building native extensions.  This could take a while...
ERROR:  Error installing mysql:
    ERROR: Failed to build gem native extension.

/opt/local/bin/ruby extconf.rb install mysql
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
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


Gem files will remain installed in /opt/local/lib/ruby/gems/1.8/gems/mysql-2.7 for inspection.
Results logged to /opt/local/lib/ruby/gems/1.8/gems/mysql-2.7/gem_make.out

Has anybody gotten mysql to work with rails on snow leopard yet? If so, what is your setup and better yet, what can I do to reproduce it?

18

18 Answers

107
votes

I just went through the same pain... here's what worked for me:

  1. Download / install the 64-bit MySQL 5.1.37 from mysql.com
  2. run the following commands:

    sudo gem update --system

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

I also uninstalled all mysql gems that were floating around from my 10.5 days, that may do it if the above doesn't work for you :)

10
votes

Rebuilding mysql as 64bit or installing the 64bit version is important, but you also need to make sure you build the native parts of the mysql gem as 64bit as well (this doesn't apply if you are on one of the original Intel Core Duo macs).

Here's the magic command:

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

You should set the ARCHFLAGS as shown above whenever you do a gem install with native components on Snow Leopard.

7
votes

If you're using bundler, you can use "bundle config" to set the proper build arguments for mysql like so:

bundle config build.mysql --with-mysql-config=/usr/local/mysql/bin/mysql_config
6
votes

I fought with this for a long time and finally got it working on Snow Leopard. I ended up installing Ruby, RubyGems, and MySQL from source (see the Hivelogic tutorial for installing Ruby and RubyGems. The MySQL tutorial is linked at the bottom). I finally got the gem to install, but I was still getting

dyld: lazy symbol binding failed: Symbol not found: _mysql_init
  Referenced from: /Library/Ruby/Site/1.8/universal-darwin10.0/mysql.bundle
  Expected in: flat namespace

dyld: Symbol not found: _mysql_init
  Referenced from: /Library/Ruby/Site/1.8/universal-darwin10.0/mysql.bundle
  Expected in: flat namespace

Trace/BPT trap

I finally deleted the mysql.bundle (I have no idea what this is for) and it all worked.

sudo rm -f /Library/Ruby/Site/1.8/universal-darwin10.0/mysql.bundle

Hope that helps someone.

4
votes

I have seen this problem many times. almost everytime I build mysql on a machine. I think, you have to pass your mysqlconfig as part of the gem install command.

sudo gem install mysql -- --with-mysql-config=/your/mysql/config

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=/usr/bin/ruby
--with-mysql-config

Remember that you need mysql dev files to be able to build this gem.

2
votes

I was having problems with getting my configuration to work after installing snow leopard. I found MAMP at http://www.mamp.info and it bundles Apache, PHP and MySQL. you install it like an application and it just works. Maybe worth giving it a try, and it is free.

mesh

1
votes

I thought I would answer my own question here. It seems as if the problem isnt in mysql, but in the mysql ruby bindings. I figured this out because when I hooked up Querius (my mysql gui), it was able to connect.

Here is how to build from source fix the bindings:

cd /tmp
wget http://www.tmtm.org/en/ruby/mysql/ruby-mysql-0.2.6.tar.gz
tar xvfz ruby-mysql-0.2.6.tar.gz
cd ruby-mysql-0.2.6
./configure
make
sudo make install

There are plenty of libraries that I will have to build from source on Snow Leopard and they keep popping up. MacPorts doesnt seem to be updated enough for all the libs so I'm off to do it on my own. Next up: freetype (http://download.savannah.gnu.org/releases-noredirect/freetype/)

1
votes

I proceeded like explained in this post (http://www.schmidp.com/2009/06/14/rubyrails-and-mysql-on-snow-leopard-10a380/), and everything now works fine.

Remember to lookout for the typos in his command to install the mysql driver.

1
votes

I had this same issue and here is what worked for me.

  1. Install Snow Leopard and the 64bit MySQL DMG.

  2. Create /etc/my.cnf to point to my previous MySQL data directory (as described here) and run

    sudo mysql_upgrade.

  3. Opened IRB and reinstall all of my gems using (via blog.costan.us/2009/07/rebuild-your-ruby-gems-if-you-update-to.html).

    `gem list`.each_line {|line| system 'sudo gem install #{line.split.first}'}

  4. Uninstalled the MySQL gems I had installed.

  5. Installed MySQL gem with

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

With that, everything seems to be working so far. *knock wood*

1
votes

In case you prefer homebrew, instead of installing MySQL manually.

Un-install the existing mysql gem (if any):

 sudo gem uninstall mysql 

Locate the ‘mysql_config’ file:

 find /usr -name 'mysql_config' 

Re-install mysql gem:

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

Note: replace /usr/local/bin/ with the corresponding mysql_config path found in step 2. Also on my blog.

1
votes

Ian Selby thank you alot but I had to remove the sudo from in front of sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config

I was installing to a gemset and am using rvm which I think asks you not to use the sudo keyword. Thanks alot.

1
votes

I've solved it this way:

  1. after install dmg file of mysql  
  2. bundle config build.mysql --with-mysql-config=/usr/local/mysql/bin//mysql_config
  3. env ARCHFLAGS="-arch x86_64"
  4. bundle install
1
votes

Having had a break from Rails for several years, I just spent some time setting up my development environment. So thought I would create a tutorial on how to install the latest version of Rails 3, MySQL, and RVM on SnowLeopard, to hopefully save others some time who are in my position.

It works as of 21st August 2011, using Rails 3.0.10, MySQL 5.5.15, and RVM 1.7.2. It should work with future Rails 3.0.x, MySQL 5.5.x, and RVM 1.7.x versions.

  1. Install XCode if not already installed. I used 4.0.2. Unfortunately this has now been withdrawn by Apple, but may be available elsewhere on the Internet. Version 3.2.x is free to download from Apple, but I did not test it with this tutorial, it probably works too.

  2. Install rvm:

    user$ bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
    
    user$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bash_profile
    
    user$ source .bash_profile
    
  3. Install a version of Ruby (e.g. 1.9.2) in rvm, basic usage instructions are here.

    user$ rvm install 1.9.2
    user$ rvm use 1.9.2
    
  4. Create a gemset for the rvm ruby installed in step 2 (x is the version number).

    user$ rvm gemset create rails30x
    user$ rvm 1.9.2@rails30x
    
  5. Install Rails 3.0.x (x is the version number).

    user$  gem install rails -v 3.0.x.
    
  6. Download and install the MySQL 5.5.x package, the startup item, and the preferences pane. All 3 of these are included in the DMG install file. Also install MySQL WorkBench.

    Find /usr/local/mysql-version-name/support-files (inserting the correct MySQL directory name) and edit mysql.server (with root privileges). Around line 46 locate the lines that read:

    basedir=
    datadir=
    

    and change them to read (inserting the correct MySQL directory name):

    basedir=/usr/local/mysql-version-name
    datadir=/usr/local/mysql-version-name/data
    

    Save the file. MySQL can now be started from the preferences pane.

    To enable rails 3 to use mysql, install the mysql2 gem (see next step). First, you’ll need to set the DYLD_LIBRARY_PATH to include the MySQL library directory. To do this, edit your ~/.bash_profile in your home folder and include the following (inserting the correct MySQL directory name):

    export DYLD_LIBRARY_PATH=/usr/local/mysql-version-name/lib:$DYLD_LIBRARY_PATH
    

    Without the inclusion of the directory in the DYLD_LIBRARY_PATH, a message about not finding the library would appear and the server would abort.

  7. Install the mysql2 gem. (as of May 2011, version 0.2.7 is the most recent working version with Rails 3.0.x).

    user$ gem install mysql2 -v 0.2.7 
    
  8. Create a rails app, and it's corresponding database (appname_development).

    user$ rails new appname
    
  9. Add this to the app's Gemfile: gem 'mysql2', "0.2.7"

  10. Add this to the app's database.yml:

    development: 
      adapter: mysql2
      encoding: utf8
      reconnect: false
      database: appname_development
      pool: 5
      username: username
      password: pwd
      host: localhost
    
  11. start app and check "About your application's environment". Everything should be working correctly.

1
votes

The instructions for setting ARCHFLAGS and passing --mysql-config=... didn't seem to be quite enough to solve this for me on Snow Leopard. In addition to doing that, I added:

DYLD_LIBRARY_PATH="/usr/local/mysql/lib:$DYLD_LIBRARY_PATH"

to my bash profile, and this solved it for me.

0
votes

I tried the archflags trick many times with slight variations but it never worked for me.

What finally did work was switching back to the version of ruby and gem installed with snow leopard.

I had built and installed my own version which has worked for me in every respect except this one. Since everything else seems to work fine, I can't help guessing the mysql plugin has some funky assumptions in it. Anyway, all I did was switch /usr/bin to be first in my path again. I had installed ruby in /usr/local/ruby-1.8.7 and /usr/local/ruby-1.9.1 to be able to easily switch. Thought I'd mention it since the archflags solution seems to work for many people, but not quite all.

0
votes

I renamed the mysql_config program from $MYSQL_HOME/bin to something else so that the configuration script from the gem installer is unable to find it. Even though I was using the libs option, the gem installer did always use the compile settings from my mysql installation, which is fat binary. But the default ruby installation is only x86_64 and therefore the compile of the gem fails. After renaming the mysql_config program the following command worked just fine and installed the gem:

sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-dir=/usr/local/mysql
0
votes

If you are installing the mysql gem with RVM you need to use the following code to install it

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

This method worked for me in Leopard Server, none of the above worked

0
votes

this article solved the issue for me :)

http://techliberty.blogspot.com/2009/12/dealing-with-rake-aborted-uninitialized.html

  • Prior to Leopard

    sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql

    • Leopard on a PPC machine:

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

    • Leopard on an Intel machine:

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

    • Snow Leopard (only on Intel):

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