39

I updated all the packages I installed with Homebrew. MySQL got upgraded to 5.6.12 (from 5.5.27 or so):

$ mysql --version
mysql  Ver 14.14 Distrib 5.6.12, for osx10.8 (x86_64) using  EditLine wrapper

and now the mysql2 gem doesn't compile anymore:

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

        /Users/pupeno/.rvm/rubies/ruby-1.9.3-p429-perf/bin/ruby extconf.rb
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... yes
checking for mysql.h... no
checking for mysql/mysql.h... no
-----
mysql.h is missing.  please check your installation of mysql and try again.
-----
*** 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
    --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=/Users/pupeno/.rvm/rubies/ruby-1.9.3-p429-perf/bin/ruby
    --with-mysql-config
    --without-mysql-config

The file mysql.h is present in /usr/local/Cellar/mysql/5.6.12/include/mysql.h. Any ideas what's going on?

The file mysql.h doesn't appear in /usr/local because the symlink seems to go deeper than it should:

$ ls -la /usr/local/include/mysql
lrwxr-xr-x  1 pupeno  admin  36 21 Jun 15:18 /usr/local/include/mysql@ -> ../Cellar/mysql/5.6.12/include/mysql

instead of

/usr/local/Cellar/mysql/5.6.12/include

Having said that, I manually fixed the symlink and the compilation still failed. So I'm stuck.

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
  • 1
    Do you have the mysql developer library installed ? – Hunter McMillen Jun 21 '13 at 14:54
  • @HunterMcMillen I thought all development files were installed with thy mysql brew package. What am I missing? – Pablo Fernandez Jun 21 '13 at 15:15
  • Homebrew + rbenv has always worked for me. Could this be an RVM issue? Also, is `mysql.h` actually present where you think it is? – tadman Jun 21 '13 at 15:26
  • @tadman it worked until the upgrade and mysql.h is present where I described in the question. I'm not sure how rvm could be messing this up, but I wouldn't discard the option. – Pablo Fernandez Jun 21 '13 at 15:38
  • Same issue here found a resolution already? – Rubytastic Jun 22 '13 at 14:05
  • Same issue, tried switching back to older versions of Ruby, but that didn't help, the only other thing I can think of is the upgrade of MySQL that messed things up. – iain Jun 22 '13 at 15:27

7 Answers7

33

I had the same problem, I managed to fix it. I did a lot of things, and I don't know for sure what it was, but going to MySQL 5.6.10 seems to have worked.

Uninstall MySQL 5.6.12:

brew unlink mysql
brew uninstall mysql

Go to the homebrew directory:

cd /usr/local

Go to version 5.6.10 (you can find a list of versions by running brew versions mysql:

git checkout 48f7e86 Library/Formula/mysql.rb

Then install mysql again:

brew install mysql

And now gem install mysql2 works for me.

I've also installed mysql from source (brew install mysql --build-from-source), but that didn't solve it, but might be part of the solution.

iain
  • 16,204
  • 4
  • 37
  • 41
  • 7
    iain's answer solved it for me. In homebrew 0.9.4 (not sure about other versions) you can use `brew pin mysql` after downgrading to 5.6.10, and that will prevent it from getting upgraded if you use `brew upgrade` regularly. – B. Ruschill Jun 25 '13 at 14:12
  • If you do a brew uninstall does that dump your datadir? I wasn't sure, so in my case I used `brew switch` to change back to my old mysql version I already had installed, as the upgrade to the latest version wasn't critical for me. – stockli Sep 14 '13 at 05:26
23

just ;)

gem install mysql2 -- --with-mysql-config=/usr/local/Cellar/mysql/5.6.10/bin/mysql_config
Nedudi
  • 5,639
  • 2
  • 42
  • 37
14

This also seemed to work for me. It forces the gem to compile against 5.6.10.

bundle config build.mysql2 --with-mysql-config=/usr/local/Cellar/mysql/5.6.10/bin/mysql_config

Related answer: https://stackoverflow.com/a/9360181

Community
  • 1
  • 1
croemmich
  • 159
  • 2
11

The top answer here is out-of-date... the brewers have fixed mysql:

brew update
brew upgrade mysql
gem install mysql2

This will update mysql to the latest version, which seems to work fine with the mysql2 gem on OSX.

webaholik
  • 1,619
  • 1
  • 19
  • 30
jkrall
  • 536
  • 3
  • 10
11

I had to specify the ldflags and cppflags for it to compile correctly, like this:

gem install mysql2 -v '0.5.2' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
  • 2
    this was the only thing that worked for me in Jan 2020; note that the flags he indicates are listed when you run "brew info mysql@5.6" – subelsky Jan 14 '20 at 20:19
  • 1
    This was the only thing that worked for me too, possibly since upgrading to openssl 1.1 `gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include` – spume Feb 20 '20 at 15:30
  • 1
    this solved my `ld: library not found for -lssl clang: error: linker command failed with exit code 1 (use -v to see invocation)` mysql2 error – jmoon90 Jun 22 '20 at 21:23
6

Much newer versions of MySQL are available, but for an older project install on Mac OS X Mojave, I still needed to run MySQL 5.6 and link against it in a Rails 5 app.

I configured bundler, then installed the bundle and was good to go.

$> brew install mysql@5.6
$> bundle config build.mysql2 --with-mysql-config=/usr/local/Cellar/mysql\@5.6/5.6.42/bin/mysql_config
$> bundle install

Note that you'll need to update the MySQL release version in the path.

1

I would recommend this answer instead: mysql2 gem fails to compile with MySQL 5.6.12 on OS X with Homebrew

The problem is with the compiler settings in Homebrew's version of MySql. Original answer found here: http://www.randomactsofsentience.com/2013/05/gem-install-mysql2-missing-mysqlh-on-os.html

Community
  • 1
  • 1
jaysqrd
  • 375
  • 4
  • 13