Using MySQL with Rails (Building Blog in 15 Min.) - mysql

I am running through the Rails blog in 15 minutes tutorial and want to use MySQL instead of Sqlite. I have MySQL installed. And I created the app with the -d mysql switch.
Here is my error message when I attempt to scaffold:
"Incorrect MySQL client library version! This gem was compiled for 5.5.27 but the client library is 5.6.23. (RuntimeError)"
Based on this I believe I have the incorrect gemfile. I cannot seem to find the one that I need to use however. I used the command (gem install mysql2). Surely this is simple, no?

Yes #Daniel Clukier is correct it seems that your Mysql client version doesn't match with your mysql client gem version. Try this command
bundle update

Try adding gem 'sqlite3' to your Gemfile and run bundle in your terminal and see if that fixes it.
Edit:
Try what Daniel said, gem install mysql2 and then bundle and see if that fixes it. If not, try following this RichOnRails link here. It walks through making an app using the MySQL2 library.

Related

Heroku error: Please install the postgresql adapter but I'm using mysql, so why am I seeing this error?

Please install the postgresql adapter: gem install activerecord-postgresql-adapter (pg is not part of the bundle. Add it to Gemfile.)
So I followed the instructions here: How to handle Ruby on Rails error: "Please install the postgresql adapter: `gem install activerecord-postgresql-adapter'"
First of all, that doesn't fix my issue (causes other errors that I'd need to debug), but regardless, why should I have to install the PG interface for ruby exactly? I'm on mysql!
Edit: I've been told Heroku doesn't support MySQL. Was this a recent change? I was using mysql just about 3 months back for my Heroku site without any issues at all.
I don't want to presume anything but if you were able to work with heroku before, you probably had something like this in your Gemfile:
group :production do
gem 'pg'
....
end
group :development do
gem 'rspec-rails'
gem 'sqlite3'
....
end
This would ensure that rails is using mysql (or sqlite3 if you copy the example verbatim) in the development environment and postgres ('pg') on the production environment hosted by heroku. Give this a shot and see if it solves your problem. I hope it does.

Problem with Rails 3 and MySQL related to JSON dependency in ActiveSupport

I built my Rails 3 app using sqlite and now I'm trying to switch over to MySQL.
I created a new MySQL db, changed database.yml accordingly, and I added an older version of the mysql2 gem to my gemfile (gem 'mysql2', '< 0.3') which is supposed to play nicer with Rails 3.
I can start the dev server fine. When I visit a page, I get a Mysql2::Error (Table 'twitter_quiz_development.users' doesn't exist). Which is fine--I still need to add the schema to the new db. This is where I run into trouble:
rake db:schema:load returns this nasty error.
The solution found here did not work for me. gem pristine --all return this.
I'm running Ubuntu 11 if that helps.
It appears that the error has to do with JSON but I have no idea how to solve this. Thanks.
The issue(s) you are experiencing could be due to 3 things:
The version of Ruby you are using
The use of the SystemTimer gem with Ruby 1.9
The JSON gem
I've elaborated below...
The first thing I would suggest is to upgrade your version of Ruby 1.9. The latest stable is:
ruby 1.9.2p180 (2011-02-18 revision 30909)
I would highly recommend using Ruby Version Manager (RVM) to install and manage Rubies.
In terms of using the JSON gem, have a look at:
http://ruby.about.com/od/tasks/a/The-Json-Gem.htm
To quote:
On Ruby 1.8.7, you'll need to install a gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution. So, if you're using 1.9.2, you're probably all set. If you're on 1.8.7, you'll need to install a gem.
Before you install the JSON gem, first realize that this gem is distrubuted in two variants. Simply installing this gem with gem install json will install the C extension variant. This requires a C compiler to install, and may not be available or appropriate on all systems. Though if you can install this version, you should.
If you can't install the C extension version, you should gem install json_pure instead. This is the same gem implemented in pure Ruby. It should run everywhere that Ruby code runs, on all platforms and on a variety of interpreters. However, it's considerably slower than the C extension version.
Edit:
Looks like you should avoid using the SystemTimer gem with Ruby 1.9. See this for more info: http://isitruby19.com/systemtimer
To quote a comment made on that page by Phillipe:
I am the (co)-author of this gem. Short story : Using this gem in Ruby 1.9 is useless and does not make any sense!
System Timer is trying to work around some limitation of the "green thread" model used in Ruby 1.8 (MRI). See http://ph7spot.com/musings/system-timer for more details.
It is then irrelevant in a Ruby 1.9 which abandoned the green thread model and embraced native threads (kind of since there is still a global interpreter lock).
Cheers,
- Philippe
Your gem pristine --all looks like it errored out with the SystemTimer gem before it got to the json gem. Is that gem used in the Rails app in question?
Are you using RVM? If so, I'd manually remove the SystemTimer and json gems and their binaries (if any) from ~/.rvm/gems/ruby-whatever-version/ (in the gem and bin subdirectories).
Try to update to the latest version of the JSON gem (1.5.1) by adding this to your Gemfile:
gem "json", "1.5.1"
And then run bundle install to install SystemTimer and the later version of the json gem.

Installing Rails, MySQL, etc. everything goes wrong

I've been struggling with this for a few hours. Everything just stopped working and I can't get it to work anymore. I'm a noob at Ruby, Ruby on Rails and the Terminal in general. This is really frustrating me so I just try to describe my problem as detailed as possible hoping someone can give me a solution.
I'm on Mac OS X Snow Leopard. I couldn't get Rails working at all just now: Could not find gem 'rails' headaches
But after some tries of reinstalling it, it suddenly worked again. But now I just can't get MySQL to work, and it sometimes even breaks the Rails installation again.
This is what I do:
sudo gem uninstall rails
sudo gem uninstall mysql
sudo gem uninstall mysql2
After these commands, I check the installed gems with gem list. No MySQL gem is listed anymore, but I can still see rails (2.3.5, 2.2.2, 1.2.6)
. Is this normal? Does this mean I have 3 Rails installations? It doesn't make sense to me. Anyway, then I do this:
sudo gem clean
Which fails completely. I get a bunch of errors like this:
Attempting to uninstall fcgi-0.8.7
Unable to uninstall fcgi-0.8.7:
Gem::InstallError: cannot uninstall, check gem list -d fcgi
It doesn't uninstall anything. At this point, I try to install everything again. I start with:
sudo gem install rails
Which succeeds (I think):
Successfully installed rails-3.0.3
Successfully installed builder-2.1.2
2 gems installed
Installing ri documentation for rails-3.0.3...
File not found: lib
Then, I update RubyGems:
sudo gem update --system
sudo gem install rubygems-update
sudo update_rubygems
Then it says I have 1.3.7 installed, so it succeeded, I think. So now I proceed with installing MySQL. I already got MySQL 5.5.8 installed on my machine. I did some research about installing MySQL on Snow Leopard, and it seems I have to use this command:
sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
I get a bunch of errors like this:
No definition for time_set_neg
No definition for time_set_second_part
No definition for time_equal
No definition for error_errno
At this point, I assume I got both Rails and the MySQL gem installed, so I try to start a new project.
rails new user_group -d mysql
It works! Rails is installed correctly. Now, I try generating a model.
cd user_group
rails generate model User
It fails with this error:
Could not find gem 'mysql2 (>= 0, runtime)' in any of the gem sources listed in your Gemfile.
Try running bundle install.
So I try running bundle install. It installs a lot of gems. Then I try to generate my model again. I get this error:
Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle: dlopen(/Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.16.dylib (LoadError)
Referenced from: /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
Reason: image not found - /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
This is as far as I can get. What should I do? And why should this be so hard...
Snow Leopard supplies Ruby 1.8.7. Ruby 1.8.7 doesn't automatically add the require 'rubygems' command to a Ruby script, like 1.9.2 will, so in general you need to add that to your script. That fixes the problems with Ruby not finding the gems called by your code in a lot of cases. Rails is a different animal, but the version with 1.8.7 is well documented so you should have found a fix pretty quickly.
The Ruby supplied by Snow Leopard is really for its own use. Users can piggyback on that, but it's installed by Apple because they have applications that use it. Use locate podcast | grep rb$ to see. Similarly, Perl and Python in Snow Leopard are used to support code Apple has installed so messing with them is not a good idea. Changing the built-in Ruby, Perl or Python usually won't cause the system to explode in flames, but it can cause irritating and unexplained problems farther down the road when system maintenance routines stop working.
By using sudo to remove gems installed by Apple and add new ones, you've left your system Ruby in an unexpected state as far as Apple's apps are concerned. Rather than mess with Apple's Ruby, you should install a separate Ruby for your own use using Fink or MacPorts, or from source code, or by using RVM.
Personally, I use RVM because it creates a ~/.rvm sandbox in your home directory, and makes it easy to manage multiple versions of Ruby and gems. RVM requires you to install the XCode development library, which you can download from http://developer.apple.com/technologies/ after a free registration. XCode is also on your system DVD, but that version is reported to be buggy, so use the downloaded version.
Make sure to add the MySql gem to that projects gemfile with the following code:
gem 'mysql', '2.8.1'
Run a bundle install/update and try it. You told it to use MySQL and the gem might even be installed, but you aren't loading it.
Also, you're failing at a couple of points: notice the
File not found: lib
Then again, I could be completely wrong about this - it's my attempt at providing a clue for your troubleshooting.

Rails 3 Mysql Problems

Trying to start a new Rails 3 beta 4 app with mysql.... Running OS X Snow Leopard. WIth previous versions of Rails I have no problem with MySQL. But now when I start the Rails 3 app I get the following error when I click "About Your Application Environment" on the Rails index.html startup screen:
undefined method `init' for Mysql:Class
Change your Gemfile to use 'mysql2', it's a more modern driver and has nicer features as other people have mentioned.
New Rails applications use the mysql2 gem by default.
I ran into the same issue (RoR 3, OSX 10.6, mysql 2.8.1 gem).
You can use irb to rule out RoR:
irb
require 'rubygems'
require 'mysql'
db = Mysql.connect('hostname', 'username', 'password', 'database')
If the above doesn't work, you may want to try removing the mysql gem and reinstalling it. I came across a post saying bundle install might mess up the install without displaying errors.
sudo gem uninstall mysql
sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
Verify things work via irb, then start up rails again.
I ended up switching from the mysql gem to the ruby-mysql gem, worked.
For simple usage, which is typical (connecting, querying, iterating over results), I found mysql2 gem which is much faster than mysql or ruby-mysql gems and auto-casts values to proper types. And it installes perfectly on Snow Leopard while I couldn't get mysql gem to work.
More info at http://github.com/brianmario/mysql2
I think I have found the solution for the problem. In my case the problem was that the mysql gem hasn't been properly installed using the bundler . when I did this:
bundle install mysql (noobish mistake)
all gems went to mysql directory, but later on I have checked the docs of the bundler gem
and did this:
bundle install bundler_files ( to know where the gems are in the future)
everything looked almost ok except that when mysql gem was installing i got some errors.
I noticed that it was because of my folder path "/home/pawel/Aptana Studio Workspace/myrails_app"
If you have spaces in your folder path this gem wont install properly and later on when you modify the path to one without spaces and try to install the mysql gem IT WONT DISPLAY ANY ERRORS, but the installation will be corrupted, because you will have some extra folders there with some files etc. so
DELETE THE GEM FOLDER CREATED BY BUNDLER AND REINSTALL GEMS WITH THIS COMMAND:
bundle install
That solved the problem.
you can try switching to the mysql2 gem which should resolve all that issues for you. see: https://github.com/brianmario/mysql2/
I think what happens is that the mysql gem isn't able to load the mysql dynamic library (supposed to be supplied by the native MySQL installation). To test whether this is happening, do this
$ irb
1.9.2p320 :001 > require 'mysql_api'
=> true
1.9.2p320 :002 >
If it isn't able to load this low level mysql_api, (which actually supplies the functionality to the mysql gem), it will give you some potentially useful errors. Usually it is not able to find the dynamic library. To remedy this, I found a couple of solutions:
From http://wonko.com/post/how-to-install-the-mysqlruby-gem-on-mac-os-x-leopard, do this
For system-wide install
sudo env ARCHFLAGS="-arch i386" gem install mysql -- \
--with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib \
--with-mysql-include=/usr/local/mysql/include
or local install
env ARCHFLAGS="-arch i386" gem install mysql -- \
--with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib \
--with-mysql-include=/usr/local/mysql/include
and then from http://alexbraunstein.com/2011/08/12/library-loaded-libmysqlclient-18-dylib/ put in .bash_profile:
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH
I has the same issue after upgrading to Snow Leopard. On installing the MySQL gem, I got a bunch of errors about the documentation, then, on running the server:
undefined method `init' for Mysql:Class
I was also having some similar, but unrelated issues with other gems, particularly those that had C components that needed compiling such as RedCloth and hpricot:
Unable to compile native extensions
These errors were to do with native extensions in base 64 architecture. The solution was threefold:
I reinstalled XCode 4. The upgrade to Snow Leopard had broken my C compiler, so some gems were failing to compile. This took me a step closer, but didn't fix the issue.
I blew away and reinstalled RVM. It appeared to be installing gems in one directory, and finding them in another. This fixed every native architecture base64 error, but the MySQL gem was still failing.
I removed and downgraded MySQL to version 5.1. This fixed the MySQL gem issue.
All is now well again.

problems with mysql for ruby on rails

ah...it is not easy to get started ruby on rails to me..
I am trying to use mysql for ruby on rails development.
When I execute "gem install mysql", I get following error.
error: while generating documentation for mysql-2.8.3-x86-mswin32...message: unhandled special: special: type=17...bla bla...
how do I install mysql for ruby on rails!??!
If you are on windows and want to avoid mysql issues, just use v 2.1.2, just before they removed the mysql connector and put it into the mysql gem. It's better like that, but I ran into MANY issues installing this on the Windows XP machine at my work.
Make sure that your version of gems is the latest.
You should be able to update it using:
gem update --system
Going by your error it failed to generate the documentation for the Gem, though it could have installed it as it was. Firstly check to see if it did install, use:
gem list --local
And check to see if the MySQL driver is in the list already.
If it isn't the make sure you're installing the correct version of the driver, either mswin32, or ruby should do.
That's an error concerning rdoc generation. If you execute the following command you'll get no error:
gem install --no-rdoc mysql
I believe there are some problems generating rdoc for the Japanese localized version of the docs. Anyway, the mysql gem gets installed.
You can easily install the MySQL gem
either install the MySQL in ruby file where all gems are install or you can follow the path:
C:\Ruby192\lib\ruby\gems\1.9.1\gems
execute the below command:
gem install MySQL
hope this helps you!