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

William Lynch
William Lynch
5,636 Points

I need help to use MySQL with Ruby

Hello, thanks upfront for any help you can provide.

I'm trying to use MySQL with RoR. Roughly put, I want to use the current versions of the software, but I still get the "Error: Failure to build native extension". What I need is a step by step guide to get this to work. I have researched this issue for several days and I am no closer than when I started. I have tried all the individual suggestions that I found, but no luck. I have no data in the database and could start all over if it would fix the problem.

Here's my software. The different versions work differently. I don't mind changing the versions of ruby, rails, MySQL, etc. I can't change windows version though.

Windows 8.1 Ruby 2.2.2 Rails 4.2.1 MySQL 5.1 DevKit MySQL connector Norton (latest version from 5/5/2015)

I have tried all variations of gem install mysql2 that I found. The end goal is to install the MySQL2 gem and then to run "rails generate scaffold monkeys".

I'm embarrassed by how much time I've spent on this. I desperately need help.

Thanks, Bill Lynch

Here's my database.yml file:

MySQL. Versions 5.0+ are recommended.

Install the MYSQL driver

gem install mysql2

Ensure the MySQL gem is defined in your Gemfile

gem 'mysql2'

And be sure to use new-style password hashing:

http://dev.mysql.com/doc/refman/5.0/en/old-client.html

default: &default adapter: mysql2 encoding: utf8 pool: 5 username: root password: host: localhost

development: <<: *default database: monkeyingaround_development

Warning: The database defined as "test" will be erased and

re-generated from your development database when you run "rake".

Do not set this db to the same as development or production.

test: <<: *default database: monkeyingaround_test

As with config/secrets.yml, you never want to store sensitive information,

like your database password, in your source code. If your source code is

ever seen by anyone, they now have access to your database.

Instead, provide the password as a unix environment variable when you boot

the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database

for a full rundown on how to provide these environment variables in a

production deployment.

On Heroku and other platform providers, you may have a full connection URL

available as an environment variable. For example:

DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase"

You can use this database configuration with:

production:

url: <%= ENV['DATABASE_URL'] %>

production: <<: *default database: monkeyingaround_production username: monkeyingaround password: <%= ENV['MONKEYINGAROUND_DATABASE_PASSWORD'] %>

1 Answer

First install mysql gem, run gem install mysql2 Second you have to add mysql2 gem to your gem file. gem 'mysql2' run bundle install Third you have to define mysql database parameters in your config/database.yaml file. See example bellow

development: adapter: mysql2 encoding: utf8 database: my_database_name username: my_database_user password: my_database_user_password host: 127.0.0.1 port: 3306 secure_auth: false

You have to create the database before run any rails models. I hope this will help you.

William Lynch
William Lynch
5,636 Points

Thank you for the quick reply. That did not work either, but I think we may be closer. I get a different error message when running "rails generate scaffold monkeys". I did add the database, but maybe I did that wrong. I signed on to MySQL and from the > prompt,

mysql> create database monkeyingaround_development -> ; Query OK, 1 row affected (0.14 sec)

Thanks, Bill

This shows the error message: Microsoft Windows [Version 6.3.9600] (c) 2013 Microsoft Corporation. All rights reserved.

C:\Users\William>cd\rails

C:\rails>cd monkeyingaround

C:\rails\monkeyingaround>cd monkeyingaround

C:\rails\monkeyingaround\monkeyingaround>rails generate scaffold monkeys C:/Ruby22/lib/ruby/gems/2.2.0/gems/mysql2-0.3.18-x86-mingw32/lib/mysql2/mysql2.r b:2:in require': cannot load such file -- mysql2/2.2/mysql2 (LoadError) from C:/Ruby22/lib/ruby/gems/2.2.0/gems/mysql2-0.3.18-x86-mingw32/lib/my sql2/mysql2.rb:2:in<top (required)>' from C:/Ruby22/lib/ruby/gems/2.2.0/gems/mysql2-0.3.18-x86-mingw32/lib/my sql2.rb:31:in require' from C:/Ruby22/lib/ruby/gems/2.2.0/gems/mysql2-0.3.18-x86-mingw32/lib/my sql2.rb:31:in<top (required)>' from C:/Ruby22/lib/ruby/gems/2.2.0/gems/bundler-1.9.9/lib/bundler/runtim e.rb:76:in require' from C:/Ruby22/lib/ruby/gems/2.2.0/gems/bundler-1.9.9/lib/bundler/runtim e.rb:76:inblock (2 levels) in require' from C:/Ruby22/lib/ruby/gems/2.2.0/gems/bundler-1.9.9/lib/bundler/runtim e.rb:72:in each' from C:/Ruby22/lib/ruby/gems/2.2.0/gems/bundler-1.9.9/lib/bundler/runtim e.rb:72:inblock in require' from C:/Ruby22/lib/ruby/gems/2.2.0/gems/bundler-1.9.9/lib/bundler/runtim e.rb:61:in each' from C:/Ruby22/lib/ruby/gems/2.2.0/gems/bundler-1.9.9/lib/bundler/runtim e.rb:61:inrequire' from C:/Ruby22/lib/ruby/gems/2.2.0/gems/bundler-1.9.9/lib/bundler.rb:134 :in require' from C:/rails/monkeyingaround/monkeyingaround/config/application.rb:7:in <top (required)>' from C:/Ruby22/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/command s/commands_tasks.rb:141:in require' from C:/Ruby22/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/command s/commands_tasks.rb:141:inrequire_application_and_environment!' from C:/Ruby22/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/command s/commands_tasks.rb:128:in generate_or_destroy' from C:/Ruby22/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/command s/commands_tasks.rb:50:ingenerate' from C:/Ruby22/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/command s/commands_tasks.rb:39:in run_command!' from C:/Ruby22/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/command s.rb:17:in<top (required)>' from bin/rails:4:in require' from bin/rails:4:in<main>'

C:\rails\monkeyingaround\monkeyingaround>

Here's the new database.yml file:

MySQL. Versions 5.0+ are recommended.

Install the MYSQL driver

gem install mysql2

Ensure the MySQL gem is defined in your Gemfile

gem 'mysql2'

And be sure to use new-style password hashing:

http://dev.mysql.com/doc/refman/5.0/en/old-client.html

default: &default adapter: mysql2 encoding: utf8 pool: 5 username: root password: dukeduke8 host: 127.0.0.1 port: 3306 secure_auth: false

development: <<: *default database: monkeyingaround_development

Warning: The database defined as "test" will be erased and

re-generated from your development database when you run "rake".

Do not set this db to the same as development or production.

test: <<: *default database: monkeyingaround_test

As with config/secrets.yml, you never want to store sensitive information,

like your database password, in your source code. If your source code is

ever seen by anyone, they now have access to your database.

Instead, provide the password as a unix environment variable when you boot

the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database

for a full rundown on how to provide these environment variables in a

production deployment.

On Heroku and other platform providers, you may have a full connection URL

available as an environment variable. For example:

DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase"

You can use this database configuration with:

production:

url: <%= ENV['DATABASE_URL'] %>

production: <<: *default database: monkeyingaround_production username: monkeyingaround password: <%= ENV['MONKEYINGAROUND_DATABASE_PASSWORD'] %>