15

I tried several ideas ... none of them worked ... I'm just trying to install mysql2 as a gem. My mysql is working, but every time my system says, that mysql.h is missing ... Has someone an idea? It's very frustrating now ...

I'm using osx 10.8.3, ruby 1.9.3, rails 3.2.13 and home-brew.

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

        /Users/gadreel/.rvm/rubies/ruby-1.9.3-p429/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/gadreel/.rvm/rubies/ruby-1.9.3-p429/bin/ruby
    --with-mysql-config
    --without-mysql-config

    file `which mysql`
    /usr/local/mysql/bin/mysql: Mach-O 64-bit executable x86_64
gadreel
  • 207
  • 1
  • 2
  • 13

5 Answers5

46

What worked for me in Mountain Lion Rails install(using Homebrew and RVM) was editing /usr/local/Cellar/mysql/5.XX.XX/bin/mysql_config and removing -Wno-null-conversion -Wno-unused-private-field from cxflags and cxflags options as follows:

Before:

cflags="-I$pkgincludedir  -Wall -Wno-null-conversion -Wno-unused-private-field -Os -g -fno-strict-aliasing -DDBUG_OFF " #note: end space!
cxxflags="-I$pkgincludedir  -Wall -Wno-null-conversion -Wno-unused-private-field -Os -g -fno-strict-aliasing -DDBUG_OFF " #note: end space!

After:

cflags="-I$pkgincludedir  -Wall -Os -g -fno-strict-aliasing -DDBUG_OFF " #note: end space!
cxxflags="-I$pkgincludedir  -Wall -Os -g -fno-strict-aliasing -DDBUG_OFF " #note: end space!

After that gem install mysql2 proceeded without hickups

Note: this is probably due to a change introduced to mysql_config after 5.6.10: http://bugs.mysql.com/bug.php?id=69645

Vlad Gurovich
  • 8,463
  • 2
  • 27
  • 28
4

Using a package manager like Homebrew or MacPorts makes it fairly straight-forward to fix this. The binary distribution of MySQL direct from Oracle and the one bundled with OS X itself does not have the development headers, of which mysql.h is one of them.

Homebrew would fix it like this:

brew install mysql

MacPorts is very similar:

sudo port install mysql

Both of these install libraries, a command-line client and the associated development headers for the libraries. Enabling the server is optional.

As an alternative, you can get the source direct from Apple and install it whatever way you see fit.

Generally Homebrew is the best way to go.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • I did this, but it didn't work. I reinstalled port and then mysql5. Now I have this error: Users/gadreel/.rvm/gems/ruby-1.9.3-p429/gems/mysql2-0.3.11/lib/mysql2/client.rb:44:in `connect': Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) (Mysql2::Error) – gadreel May 20 '13 at 21:28
  • `sudo port install mysql` does not work. Closest command is, to install the latest version of `mysql` is: `sudo port install mysql55`; however, this does not create `mysql.h`. The port `mysql55-server` also does not include it. Which `port` would? – Alex Aug 29 '17 at 14:44
  • @Alex MacPorts has fallen out of favour with the community so I'm not sure what condition it's in. The vast majority of devs are using Homebrew. Switching over can be a bit of a hassle, but this might be less hassle than getting MacPorts working. – tadman Aug 29 '17 at 15:57
  • The problem is, I typically tend to run as root, and then Homebrew tends to fail, saying I have to run it as a regular user. So I have to log out, log back in, etc.-- so I strongly prefer to use (any) tool that can run as root. – Alex Aug 29 '17 at 16:09
  • Running as root is a bad habit and one that you should *immediately* curtail. There's a reason HomeBrew is badgering you about that. – tadman Aug 29 '17 at 16:10
  • My view is, it shouldn't be the responsibility of Homebrew, with their clinking mugs, to tell me I shouldn't be running as root. My company tells me what to do, and what not to do, and I'm sure they know better. But anyways, if `macports` no longer works for this, then maybe you could at least remove it from your answer. – Alex Aug 29 '17 at 16:18
  • If your company is telling you to run as root I have *serious* questions about your company. It's the computer equivalent of running with scissors and I'd challenge you to find *any* security expert that thinks it's a good idea. This answer is from 2013, you can't expect all advice from that era to be up to date. – tadman Aug 29 '17 at 16:20
0

Installing a gem sometimes involves compiling source. In this case the MySQL gem needs to create the ruby interface by compiling the C code -- you most likely need to install the MySQL development files.

(missed the OSX reference earlier)

Xaq
  • 558
  • 1
  • 4
  • 13
0

You need to install the development headers for mysql, using brew you might need to install a lower version than 8 since some header files are removed from this distribution. For instance the my_global.h is removed, so if you code uses this header you will be forced to install a previous version then 8.

Header File Dependencies

We have started cleaning up header files dependencies, i.e. work on “include what you use” and on reorganizing header files to remove build dependencies. We have fixed ambiguous include paths; almost all should now be from the root. Incrementality has increased a lot after e.g. my_global.h went away, and sql_class.h was also reduced a fair bit in weight. Shipped client headers are self-contained and much more sane. For example, client headers are now platform independent (no difference between 32- and 64-bit Linux).

brew install mysql@5.7 mysql-client@5.7
Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
0

If you're using the standard XAMPP this could be the problem, you could probably need to install xampp-devel which is the development package for xampp, this distribution contains mysql headers *.h files as well as other related sources, you can find older versions in here.

you can than copy the include directory in your /Applications/XAMPP/xamppfiles then install gem using this command:

sudo gem install mysql2 -v 0.3.21 -- --with-mysql-config=/Applications/XAMPP/xamppfiles/bin/mysql_config --with-mysql-include=/Applications/XAMPP/xamppfiles/lib/include/ --no-ri --no-rdoc

this version worked for me, and don't need to include --with-mysql-dir if you specify mysql_config path, and then you should get a similar output:

This could take a while...
Successfully installed mysql2-0.3.21
Parsing documentation for mysql2-0.3.21
Installing ri documentation for mysql2-0.3.21
Done installing documentation for mysql2 after 0 seconds
1 gem installed

you may also need to provide rails with the correct version of libmysqlclient, here is an example using Symlink:

sudo ln -s /Applications/XAMPP/xamppfiles/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib

Don't forget in update bundle packages.

Ibrahim.H
  • 1,062
  • 1
  • 13
  • 22