Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

How do you connect MySQL to Ruby on Rails?

I followed this post, http://blog.teamtreehouse.com/installing-ruby-rails-and-mysql-on-os-x-lion.

I have been learning Ruby and Rails for a month, so I am quite familiar with Ruby. I also know some parts of Rails due to the understandings acquired by following several tutorials.

However, I continuously fail to 'gem install mysql' or 'gem install mysql2' prior to 'rails new photos -d mysql'.

I already successfully finished 'brew install mysql' and checked 'mysql -u root'.

seodongju@Seoui-MacBook-Pro ~/Desktop/Projects$ sudo gem install mysql2 Password: Building native extensions. This could take a while... ERROR: Error installing mysql2: ERROR: Failed to build gem native extension.

/usr/local/rvm/rubies/ruby-2.0.0-p247/bin/ruby extconf.rb

checking for rb_thread_blocking_region()... yes checking for rb_wait_for_single_fd()... yes checking for rb_hash_dup()... yes checking for rb_intern3()... 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=/usr/local/rvm/rubies/ruby-2.0.0-p247/bin/ruby --with-mysql-config --without-mysql-config

Gem files will remain installed in /usr/local/rvm/gems/ruby-2.0.0-p247/gems/mysql2-0.3.13 for inspection. Results logged to /usr/local/rvm/gems/ruby-2.0.0-p247/gems/mysql2-0.3.13/ext/mysql2/gem_make.out

2 Answers

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hey Dongju Seo try following the instructions in this StackOverflow post, that may be what you're running in to.

Thank you, Jason. I already successfully installed mysql and mysql2 gems! I don't know what to use between them though..

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

I would use the mysql2 gem -- it's more modern and has some better capabilities with newer apps.

Yep, I heard mysql2 gem is 2x faster than mysql! Should I also write 'mysql2' at the database type on the configuration file?

Is mysql2 also usable with MySQL commands?

Jason Seifer
Jason Seifer
Treehouse Guest Teacher

It is a little faster and also more compatible.