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

Jeff Lange
Jeff Lange
8,788 Points

How do I install the mysql gem, and how do I set it as the default database for Rails?

I've looked all over the interwebs for this, and can't find an answer that works.

First things first: I'm using a Windows 7 64-bit machine

In the console, when I try to:

gem install mysql

It gives me:

Temporarily enhancing PATH to include DevKit...
Building native extensions. This could take a while...

ERROR: Error installing mysql:
              ERROR: failed to build gem native extension
C:/Ruby200-x64/bin/ruby.exe extconf.rb

checking for main() in -llibmysql... yes

checking for mysql_ssl_set()... yes

checking for rb_str_set_len()... yes

checking for rb_thread_start_timer()... no

checking for mysql.h... no

checking for mysql/mysql.h... 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 configuation options. 

Then it gives me a bunch of configuration options (I don't know what they mean).

So...

  1. How I can get the mysql gem to install?

  2. How can I set mysql (instead of sqlite3) as the default for Rails?

Thank you for anyone who knows how to do this!

3 Answers

It looks like you don't have mysql installed on your system or at least when building the native extension it can't find mysql. That may be all you need to fix it. Just in case here's the general outline of what you'd need to get up and running with MySQL instead of SQLite in rails:

  1. Install MySQL. You've got a couple of options. MySQL offers an installer, you can use Homebrew, or your preferred method.
  2. Add the mysql2 gem to your Gemfile (currently at version 0.3.16 but check rubygems):

    gem 'mysql2', '~> 0.3.16'
    
  3. Run bundle install from the command line.

  4. Setup your database credentials in config/database.yml:

    development:
        adapter: mysql2
        encoding: utf8
        database: myapp_development
        pool: 5
        username: someuser
        password: somepassword
    
  5. Run rake db:create db:migrate --trace from the command line to make sure it works.

EDIT: Sorry, re-read your post and realized you were on Windows so removed the part about Homebrew.

Jeff Lange
Jeff Lange
8,788 Points

I definitely have mysql installed, as I spent the last few days going through Treehouse badges that involved learning how to install and use it.

Step 2 is where I'm having issues. When I try to install the mysql2 gem I get an error ("extconf.rb failed").

I've wasted all day on this, so for now I think I'm just going to give up and maybe try again later. It's pretty irritating after just completing a whole Treehouse section (as part of the Ruby on Rails track) on how to use MySQL...and then the stuff we build with Rails doesn't even use MySQL! What was the point of learning how to use it then??

Thanks again, guys

One of the beauties of Ruby on Rails (and other full-stack frameworks) is that it's somewhat agnostic to which database you are running. MySQL is certainly one of the more popular options but for development SQLite will likely be just fine unless you're writing custom queries.

Back to your issues with the mysql2 gem it looks like it can't find development headers:

checking for mysql.h... no

How did you install MySQL on your machine?

Jeff Lange
Jeff Lange
8,788 Points

I downloaded the community 5.6 version for windows and followed through the wizard. I had some errors getting it to download a couple of (superfluous) modules, so I uninstalled everything and reinstalled it clean with only the modules I needed, namely, Connector J, Connector .NET, Documentation, Notifier, Connector C++, Connector ODBC, and Workbench (6.2).

For some reason, I do have two directories of MySQL (one in Program Files, and one in Program Files x86), and I'm not sure why. I'm also unsure if the computer only uses one or both of those, because the two directories don't share duplicate folders. One of them has the MySQL Server, Workbench, C++ and ODBC, and the other has Notifier, Documentation, J, and .NET.

Maybe it would simplify everything if I was running linux? I would just hate to have to start over and reinstall everything right now.

Looks like it can be a bit more tricky than just a gem install to get the mysql2 gem running on windows but you shouldn't have to switch OSes just to get it running. Check out the instructions on the mysql2 github page here: https://github.com/brianmario/mysql2#windows

Looks like you'll need an archive with the development headers and then pass some parameters when installing the gem.

gem install mysql2 -- --with-mysql-dir=c:\path\to\your\mysql\install

While you'll have to install this gem manually bundler should recognize that you have it installed and carry on with the rest of your gemset.

Jeff Lange
Jeff Lange
8,788 Points

THANK YOU!!! It's working now!!! So much hassle but I'm sure I'll keep it as valuable lessons. Thanks again!

I have one additional question: where is the database information being stored? MySQL seems to automatically know it exists (when I open up MySQL and run a local instance it automatically appears in the list of databases). However, I'd like to know where the actual database file is.

When I look in project/db I find:

  • schema.rb
  • seeds. rb
  • /migrate which contains 20141105155758_create_posts.rb

None of which are MySQL database files (.sql). Any ideas?

  • db/schema.rb will be the current snapshot of the structure of your database. This is similar to the SQL file you'd use to create the structure of a database if you weren't using rails but does not contain any data, only table structure. You won't really need to touch this file as it's generated each time you run rake db:migrate.
  • db/seeds.rb is a script that is run with rake db:seed that will pre-populate the database with any required information. Say you're creating a blogging engine and want posts to be added to specific categories - you might want to pre-populate the categories using this script. Normal ActiveRecord calls would go in here, e.g. Category.create(name: "Development")
  • db/migrate/ is where rails stores migrations. These are how you should be managing the structure of your database within a rails app. You can create a new migration with rails generate migration <migration-name> from the command line

As far as where the data is stored i can't answer as to where it is on a Windows machine but on a Mac it's stored in /usr/local/var/mysql (at least with how i have MySQL installed). Though it won't be much good as it's all stored in a binary format that mysql can read.

If you're just looking to browse the contents and structure of the database to follow along as you're learning rails you can always pop into the command line tool that comes with MySQL (some setup may be required to add this to your path) or download a GUI app. MySQL has a free one called MySQL Workbench.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points
  1. You're using Windows, so this will be the first hurdle in Rails development. I would also move to Linux or Mac if you want to do Rails development seriously (Virtual Machine).

  2. You're using 64-bit version of Ruby which - for as long as I can remember - has not been working too well. I would uninstall this version and install the 32-bit one.

  3. I think the proper gem is gem install mysql2

  4. When generating a new app, you can theoretically indicate what database is to be used. The command looks like this: rails new projectname -d mysql but I never tried that, so I can't say what to expect and whether you need some special configuration for that command to work.

  5. To tell Rails to use mysql2, you have to remove sqlite3 from gemfile and add the mysql2 gem in its place. You also have to configure the database.yml file correctly, including the name of the local user and his password. Example:

development:
  adapter: mysql2
  database: db_name_dev
  username: koploper
  password:
  host: localhost
  socket: /tmp/mysql.sock

I can't really tell you more, because I always use Postgres (and only in Production; in dev and tests I stick with sqlite3, it's easier), so the database is configured for me automatically when deployed on Heroku.

Hey,

as Geoff Parsons said, that is totally correct! I have not done ruby for quite a while, sorry I could not be of more help.