Ruby on rails Mysql on ubuntu - mysql

I am trying to develop ruby on rails aplication using mysql database in Ubuntu environment.
I have installed rails and mysql succesfully. But when I try to generate model, it reports the message
rails g model person
invoke active_record
/home/meuser/.rvm/gems/ruby-1.9.2-p320#global/gems/bundler-1.1.4/lib/bundler/rubygems_integration.rb:147:in `block in replace_gem': Please install the mysql adapter: `gem install activerecord-mysql-adapter` (mysql is not part of the bundle. Add it to Gemfile.) (LoadError)
Then I tried to install activerecord-mysql-adapter:
meuser#ubuntu:~/myproject$ gem install activerecord-mysql-adapter
ERROR: Could not find a valid gem 'activerecord-mysql-adapter' (>= 0) in any repository
ERROR: Possible alternatives: activerecord-jdbcmysql-adapter, activerecord-jdbcmssql-adapter, activerecord-fb-adapter, activerecord-odbc-adapter, activerecord-jdbc-adapter
meuser#ubuntu:~/myproject$
Initiated by the answers below, I also tried this:
meuser#ubuntu:~/myproject$ gem "mysql2", "< 0.3"
ERROR: While executing gem ... (RuntimeError)
Unknown command mysql2,
meuser#ubuntu:~/myproject$
And this:
meuser#ubuntu:~/myproject$ gem 'mysql2'
ERROR: While executing gem ... (RuntimeError)
Unknown command mysql2
meuser#ubuntu:~/myproject$
And this:
meuser#ubuntu:~/myproject$ gem install "mysql2" -v=2.8.17
ERROR: Could not find a valid gem 'mysql2' (= 2.8.17) in any repository
ERROR: Possible alternatives: mysql2
meuser#ubuntu:~/myproject$
And this:
meuser#ubuntu:~/myproject$ sudo apt-get install libmysql-ruby libmysqlclient-dev
[sudo] password for meuser:
Reading package lists... Done
Building dependency tree
Reading state information... Done
libmysqlclient-dev is already the newest version.
libmysql-ruby is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
meuser#ubuntu:~/myproject$ gem "mysql2", "< 0.3"
ERROR: While executing gem ... (RuntimeError)
Unknown command mysql2,
meuser#ubuntu:~/myproject$
Here is my database.yml file
development:
adapter: mysql2
encoding: utf8
reconnect: false
pool: 5
database: myproject_db
username: root
password: somepassword
host: localhost
socket: /var/run/mysqld/mysqld.sock
Does anyone knows solution for this issue?
Thanks.

The adapter gem is called 'mysql2', and you need add it to your gemfile:
gem 'mysql2'
Then run the bundle command and edit your database.yml file to point to the right database:
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: yourapp_development
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock

try to install mysql2 gem in version < 0.3 like 0.2.8 it is common issue.
in Gemfile type
gem "mysql2", "< 0.3"

Related

RoR 4 ERROR on 'rake db:create' or 'rake about'

I have RoR 4.0 and ruby-1.9.3-p484 installed. gem install bundler and bundle install run without any errors. Then I need to create a db using rake db:create and I'm getting following error (I get the same error on rake about as well):
rake aborted! Could not load
'active_record/connection_adapters/mysql2_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.
From gem list:
activerecord-mysql2-adapter (0.0.3)
mysql2 (0.3.14)
rake (10.1.1, 0.9.2.2)
config/database.yml
adapter: mysql2
encoding: utf8
host: localhost
database: my_database
pool: 20
username: root
password:
socket: /tmp/mysql.sock
MySQL is running
Please let me know if I should share some more informaion. Thank you!!!
Thank you everybody for your suggestions! I finally found the solution.
Apparently, mysql2 gem did not work well with mysql-5.5.12. It was also installed from source files on my mac. I deleted mysql from my machine and installed it using Brew following steps here. Everything seems fine now.

Getting an error when i try to use mysql in rails

i am trying to use mysql as a database for my rails application.
here is what my database.yml file looks like
development:
adapter: mysql2
encoding: utf8
database: blog_development
pool: 5
username: root
password:
socket: /tmp/mysql.sock
and this is the error i am getting when i try the rake db:create command
hilarl#isa009:~/workspace/blog$ rake db:create rake aborted! Please
install the mysql2 adapter: gem install activerecord-mysql2-adapter
(mysql2 is not part of the bundle. Add it to Gemfile.)
Tasks: TOP => db:create (See full trace by running task with --trace)
and when i try gem install activerecord-mysql2-adapter i get 'ERROR: Could not find a valid gem 'activerecord-mysql2-adaptor' (>= 0) in any repository'
i am using ubuntu with ruby 1.9.3p194 and rails 3.2.3
what the contents of your Gemfile ?
you should add :
gem 'mysql2'
in your gem file, after that from your directory apps in terminal you should execute bundle install

Rails 3.2.3 wants sqlite3 gem on mysql production env

I have a Rails 3.2.3 application which gets deployed with Capistrano. Until now, this workflow worked perfectly for several months now. But since the last deployment of the latest changes, the rake db:migrate action hangs because it can't find the SQLite3 gem and adapter.
executing "cd /home/*/*/*/releases/20120425232058 && bundle exec rake RAILS_ENV=production db:migrate"
executing command
rake aborted!
Please install the sqlite3 adapter: `gem install activerecord-sqlite3-adapter` (sqlite3 is not part of the bundle. Add it to Gemfile.)
the thing is, the application is running on MySQL. I have in my Gemfile:
source 'https://rubygems.org'
gem 'rails', '3.2.3'
gem 'jquery-rails'
gem 'omniauth-openid'
gem 'will_paginate'
gem 'pusher'
gem 'carrierwave'
gem 'capistrano'
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
group :development, :production do
gem 'mysql2'
end
group :production do
gem 'unicorn'
end
and the database.yml file:
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: *****_development
pool: 5
username: *****
password: *****
host: localhost
production:
adapter: mysql2
encoding: utf8
reconnect: false
database: *****_production
pool: 5
username: *****
password: *****
host: localhost
the application runs without problems in production mode on my local machine.
why is Rails trying to install SQLite ?
I beleive you have sqlite specified in your config/database.yml
My bad guys. Something went wrong in the Capistrano recipe. Thanks though for your answers !
Can you post the list of changes in the new deployment? Perhaps it's an issue with one of the gems that also use ActiveRecord?

Rails mysql2 error: "rake aborted! Please install the mysql2 adapter..."

I am running this on a Windows 7 (64 bit) machine. I installed RoR using the one-click installer. I've updated my database.yml file to use mysql2:
development:
adapter: mysql2
encoding: utf8
database: blog_development
pool: 5
username: root
password:
socket: /tmp/mysql.sock
test:
adapter: mysql2
encoding: utf8
database: blog_test
pool: 5
username: root
password:
socket: /tmp/mysql.sock
production:
adapter: mysql2
encoding: utf8
database: blog_production
pool: 5
username: root
password:
socket: /tmp/mysql.sock
I added this line to my Gemfile (per the tutorial video):
gem 'mysql2', :group => :production
Then:
gem install mysql2
which succeeds. Then:
bundle install
Which also succeeds, but mysql2 is not listed.
Then:
rake db:create
which gives this error:
"rake aborted!
Please install the mysql2 adapter: gem install activerecord-mysql2-adapter (my
sql2 is not part of the bundle. Add it to Gemfile.)
Tasks: TOP => db:create
(See full trace by running task with --trace)"
bundle show mysql2
Gives this error: "Could not find gem 'mysql2' in the current bundle."
What am I missing to get mysql2 going?
Run bundle install before rake db:create (after the gem install mysql2)
Go to your app
Open Gemfile
Add this line
gem 'mysql2'
Similar issue was resolved for me after
creation of libmysql.lib file per https://github.com/brianmario/mysql2/issues/486 and using it to install/compile native gems (lib resided in directory used for "--with-mysql-lib="$mysql top_path/lib"" gem installation)
putting libmysql.dll to ruby_top bin folder
installing both mysql and mysql2 gems (was getting exactly the same error with just mysql2 gem installed, though database.yml had "adapter = mysql2" everywhere).
After all that mysql2 appeared in list of gems after command "bundle install", I was able to run "rake db:create" successfully, started redmine x64 windows on x64 ruby 2.0 with x64 mysql on webrick, going on with configuring on some production server.
===============
An update
I have to precise that besides installing both mysql and mysql2 I created file Gemfile.local in redmine application top directory which probably made the trick with bundler.
So I would recommend to replace the last step with:
installing mysql2 gem
creating the file Gemfile.local in application top directory where
you list local gems to be included to your bundle.
I saw another answer that recommends to add mysql2 gem to Gemfile, but for me mysql2 was already included in Gemfile but has appeared in bundler output after adding to Gemfile.local only.
I'm leaving both solutions if I'm wrong and the trick was done by mysql gem installed together with mysql2, unfortunatelly I cannot remove/reinstall all from scratch now to test that for sure, I will update when I'm able to do so, hope all this will save some time to someone.
My Gemfile.local file contents is:
---8<---
gem "mysql2", "~> 0.3.11"
gem "eventmachine"
gem "thin"
---8<---

How do I change my database from SQLite to MYSQL in Rails

I know that you have to change the database.yml but I don't know what to change it to and how to download MYSQL and all of that jazz.
Gemfile:
gem 'mysql2'
config/database.yml
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: db_name_here
pool: 5
username: root
password:
host: localhost
Command line:
bundle install
rake db:create
rake db:migrate
Of course MySQL needs to be installed.
If you're creating a new project:
rails new app_name_here -d mysql
I ran into the same problem when trying to use the mysql2 gem with Rails 3.0.9.
When I ran rake db:create after installing the mysql2 gem, it gave me these warnings:
WARNING: This version of mysql2 (0.3.6) doesn't ship with the ActiveRecord adapter bundled anymore as it's now part of Rails 3.1
WARNING: Please use the 0.2.x releases if you plan on using it in Rails <= 3.0.x
To specify that you only want to use the 0.2.x versions of mysql2, edit your Gemfile so that
gem 'mysql2'
becomes
gem 'mysql2', '~> 0.2.1'
As of Rails 6 a command has been added to do this automatically.
$ rails db:system:change --to=mysql
conflict config/database.yml
Overwrite /home/jim/Rails projects/myapp/config/database.yml? (enter "h" for help) [Ynaqdhm] y
force config/database.yml
gsub Gemfile
gsub Gemfile
Ref.: https://www.bigbinary.com/blog/rails-6-has-added-a-way-to-change-the-database-of-the-app