Missing the mysql2 gem. Add it to your Gemfile: gem 'mysql2' - ruby

Versions
ruby 1.9.2-p290
rails 3.0.9
mysql2-0.2.13 under rvm
ubuntu 11.04
gem installed with "gem install mysql2 -v '< 0.3' ". All dependencies were met.
rails & 'rails c' are working fine.
I need to run a script outside of rails environment using activerecord 3.0.9. When I run the script this is the output:
======================
/home/ross/work/x/library/models.rb:27:in `eval': !!! Missing the mysql2 gem. Add it to your Gemfile: gem 'mysql2' (RuntimeError)
from /home/ross/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.9/lib/active_record/connection_adapters/mysql_adapter.rb:19:in `mysql_connection'
from /home/ross/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract/connection_pool.rb:229:in `new_connection' ....
=======================
Clearly the gem is there, because rails works.
Don't know why the gem isn't found when running a script?
Can anyone pls shed some light on the issue?
Regards
Ross

In case it helps anyone, in my case I had forgotten to change the adapter in database.yml to mysql2 for the production database, the Gemfile was OK (upgrading from rails 2.3.4 to 3.0.9).

Check your database.yml first
You have something like this:
development:
adapter: mysql
encoding: utf8
...
To solve your problem you need replace mysql to mysql2.
Correct version of database.yml
development:
adapter: mysql2
encoding: utf8
...

Related

Could not load 'active_record/connection_adapters/postgresql_adapter'

I have a Sinatra app using Postgres via Activerecord. I'm using RVM on a Mac and a number of gems including:
activerecord
activesupport
When I run the server (thin), I get an error:
Could not load 'active_record/connection_adapters/postgresql_adapter'.
Make sure that the adapter in config/database.yml is valid. If you use
an adapter other than 'mysql', 'mysql2', 'postgresql' or 'sqlite3' add
the necessary adapter gem to the Gemfile.
The adapter is set to postgresql in the config, and I've checked my RVM and paths: My Gem install path is set properly, and I can see the appropriate gems installed properly, and the specific adapter is there too, so really not sure what could be happening or how to further troubleshoot this?
Using Ruby 2.0.0-p598, activerecord and activesupport 4.1.4
--
Further debugging attempts:
ruby -v
ruby 2.0.0p598 (2014-11-13 revision 48408) [x86_64-darwin14.0.0]
which ruby
/Users/user/.rvm/rubies/ruby-2.0.0-p598/bin/ruby
gem env (relevant parts):
EXECUTABLE DIRECTORY: /Users/user/.rvm/gems/ruby-2.0.0-p598/bin
GEM PATHS:
/Users/user/.rvm/gems/ruby-2.0.0-p598
/Users/user/.rvm/gems/ruby-2.0.0-p598#global
SHELL PATH:
/Users/user/.rvm/gems/ruby-2.0.0-p598/bin
/Users/user/.rvm/gems/ruby-2.0.0-p598#global/bin
/Users/user/.rvm/rubies/ruby-2.0.0-p598/bin
ls $GEM_HOME/gems
activerecord-4.1.4
activesupport-4.1.4
Really at a loss for what else I can look into
database.yml
development:
adapter: postgresql
database: example_db
username: example_user
password: example_password
host: localhost
A next debugging step I took was to try using rvm to install a whole new version of ruby. This worked, so I used rvm to completely remove my old version of ruby with rvm remove 2.0.0-p598 and reinstall it again rvm install 2.0.0-p598.
I still have no idea what may have happened, but perhaps somehow something in the gem caches/builds were corrupted, so if anyone runs into a weird error like this where everything is in place but still doesn't work, try clearing everything with rvm and reinstalling.

heroku run rake db:create fails with Could not find gem 'pg..., but the database is mysql2 (not postgresql)

I receive the following error when I run heroku run rake db:create:
Running `rake db:create` attached to terminal... up, run.6027
Could not find gem 'pg (>= 0.11.0) ruby' in the gems available on this machine.
Run `bundle install` to install missing gems.
In the config/database.yml I'm using the default mysql2 database:
production:
adapter: mysql2
database: redmine
host: localhost
...
development:
adapter: mysql2
...
test:
adapter: mysql2
...
So I guess 'pg (>= 0.11.0) ruby' does not need to be installed. Am I wrong?
I'm using Windows XP. It is the first time I use Ruby.
UPDATE 1
I forgot to say bundle install finished correctly without errors.
On the other hand, Gemfile has the following code:
adapters.each do |adapter|
case adapter
when 'mysql2'
gem "mysql2", "~> 0.3.11", :platforms => [:mri, :mingw]
gem "activerecord-jdbcmysql-adapter", :platforms => :jruby
...
when /postgresql/
gem "pg", ">= 0.11.0", :platforms => [:mri, :mingw]
gem "activerecord-jdbcpostgresql-adapter", :platforms => :jruby
...
So again, I guess it should never enter in when /postgresql/ when the command heroku run rake db:create is executed. Am I right?
See the discussion above... This is a Heroku application and it needs a database configured or it is going to try and force Postgres as a dependency to help you out.
You need to do bundle install before running the rake task for db creation and fulfill all gem dependencies. This is required even though you are using mysql and not postgres because the 'pg gem' is specified in the Gemfile and you have to make sure that all gem dependencies in your Gemfile are made available to your application by running the bundle install commmand.
As in your case you are using mysql, you do not require the 'pg gem' and can remove or comment it in the Gemfile.

mysql2 adapter issue: libruby.so.1.8: cannot open shared object file: No such file or directory

I note that there have been many other similar questions, but my context seems to be different.
Error
The following is the error I am getting, trying to run/restart a Rails Application (Redmine). Redmine was running fine, but there have been some server updates by the shared host, thus seeing these problems.
mysql2 is listed in the gem/bundle list. mysql2.so exists in the location it is looking for, and has the required (executable) permissions. Thank you any suggestions in addressing this issue.
Error message:
Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` (libruby.so.1.8: cannot open shared object file: No such file or directory - /home/test/gems/gems/mysql2-0.3.11/lib/mysql2/mysql2.so)
Exception class:
LoadError
Enviornment
# ruby -v
ruby 1.9.3p429 (2013-05-15 revision 40747) [x86_64-linux]
# rails -v
Rails 3.2.6
# gem -v
1.8.23
# mysql --version
mysql Ver 14.14 Distrib 5.5.34, for Linux (x86_64) using readline 5.1
#gem list
...
mysql2 (0.3.11)
...
#bundle show
...
mysql2 (0.3.11)
...
#vim database.yml
production:
adapter: mysql2
database: database_name
host: localhost
username: dbuser
password: dbpwd
encoding: utf8
Answer to this question solved my issue.
gem uninstall mysql2
bundle install

"Please install the mysql2 adapter"

I am attempting to follow this Redmine setup tutorial. When I get to the point of starting the server, I type sudo rails server and I get the following error:
Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` (mysql2 is not part of the bundle. Add it to Gemfile.) (LoadError)
The relevent section of my Gemfile reads:
....
if File.exist?(database_file)
database_config = YAML::load(ERB.new(IO.read(database_file)).result)
adapters = database_config.values.map {|c| c['adapter']}.compact.uniq
if adapters.any?
adapters.each do |adapter|
case adapter
when /mysql/
gem "mysql", "~> 2.8.1", :platforms => [:mri_18, :mingw_18]
gem "mysql2", "~> 0.3.11", :platforms => [:mri_19, :mingw_19]
gem "activerecord-jdbcmysql-adapter", :platforms => :jruby
...
and Gemfile.lock contains mysql2:
...
multi_json (1.5.0)
mysql (2.8.1)
mysql2 (0.3.11)
net-ldap (0.3.1)
...
and my database.yml file includes the following:
...
production:
adapter: mysql2
database: redmine
host: localhost
username: ****
password: ****
development:
adapter: mysql2
database: redmine_development
host: localhost
username: ****
password: ****
encoding: utf8
...
Running bundle install seems to succeed, but mysql2 is not listed in the output, which mysql2 returns nothing, and bundle show mysql2 returns Could not find gem 'mysql2' in the current bundle.
I have uninstalled and reinstalled the mysql2 gem, with apparent success each time.
I have read through descriptions of similar-sounding problems on other stackoverflow questions, but none of their solutions turned out to solve my problem.
You will get this error if mysql2 is not included in the Gemfile.lock file that's generated by bundle. While the Gemfile is used to manage dependencies, the lock file is what is actually loaded by the rails app.
That's a more verbose Gemfile than I have seen before (I'm not generally familiar with Redmine)- where is this from? It doesn't appear to be from the most recent stable source. If you know you'll be using mysql2, I don't think there's any reason you need your Gemfile to parse your database config. For whatever reason, it's not properly reading the database.yml file.
Try adding gem 'mysql2' outside any blocks or loops and run bundle again.

Arel Deprecation Warning when running rake db:create

I'm trying to create an app in Rails 3.1 with mysql2 v 0.2.6. When running rake db:create, I get the following error:
DEPRECATION WARNING: Arel::Visitors::VISITORS is deprecated and will be removed.
Database adatpers should define a visitor_for method which returns the appropriate
visitor for the database. For example, MysqlAdapter.visitor_for(pool) returns
Arel::Visitors::MySQL.new(pool). (called from mon_synchronize at
c:/Ruby192/lib/ruby/1.9.1/monitor.rb:201)
I can access the MySQL monitor, so the gem appears to be installed correctly. What else could be going on here?
Thanks!
Try updating your msql gem.
In ./Gemfile
gem require 'mysql2', '~> 0.3.6'
bundle update mysql2
use the below specified version in gemfile.
gem 'mysql2', '~>0.3.10'
then run
bundle install

Resources