Mysql gem and Rails3 - mysql

I am trying to install in my Gemfile the mysql gem. I type down this:
group :development, :test do
#gem 'sqlite3-ruby', :require => 'sqlite3'
#using mysql gem
gem 'mysql', '2.8.1'
end
I run bundle install and everything runs ok. Ok when I run "rake db:reset" i get this:
Please install the sqlite3 adapter: `gem install activerecord-sqlite3-adapter` (sqlite3 is not part of the bundle. Add it to Gemfile.)
Tasks: TOP => db:drop => db:load_config
(See full trace by running task with --trace)
Now why would it even refer to sqlite3 since I am using mysql?

I would advise you to use the mysql2 gem, rather than the mysql one (see: What the difference between mysql and mysql2 gem)
Also you need to change the "config/database.yml" file, probably it now looks like this:
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
Which means that Rails tries to use sqlite3 rather than mysql for your database. There's a section in the Rails guides on how to change this for MySQL: http://guides.rubyonrails.org/getting_started.html#configuring-a-database

it looks like you created a rails app without specifying that you were going to use MySql database. Just a thought...
Did you run
rails new APP_NAME -d mysql -- This will create the app configured to use MySQL
or
rails new APP_NAME -- This will create the app configured to use SQLLite3

Related

Ruby on rails "NameError: uninitialized constant Mysql2::Client::REMEMBER_OPTIONS"

I am trying to learn web development with ruby on rails. I have been following a course on Lynda.com and my steps were as follows:
I installed Ruby + DevKit 2.4.4-1(x64)
I installed rails as in the command line as follows
gem install rails --version 5.0.0
I installed MySQL version 5.7.21
I installed mysql2 gem as follows
gem install mysql2
I installed Atom text editor
I created a new project rails new kudaweb -d mysql
I created the required databases in the MySQL shell
CREATE DATABASE kudaweb_development; and CREATE DATABASE kudaweb_test;
I then created a new user and granted all privileges as follows
GRANT ALL PRIVILEGES ON kudaweb_development.* TO 'rails_user'#'localhost' IDENTIFIED BY 'mypassword'
and i did this for the test database as well
I configured the database.yml file as follows:
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: rails_user
password: mypassword
host: localhost
I ran the following code to check if I had correctly configured the database
rails db:schema:dump
and I got the following error
C:\Users\Dj K-Staxx\Desktop\RUBY\kudaweb>rails db:schema:dump
rails aborted!
NameError: uninitialized constant Mysql2::Client::REMEMBER_OPTIONS
bin/rails:4:inrequire'
bin/rails:4:in <main>'
Tasks: TOP => db:schema:dump
I have no idea how to resolve this. I have searched the internet for solutions but to no avail. I am using windows 7 64bit and for the mysql2 gem I am using version 0.5.0
Seems like there may be some config issue between your 0.5.0 gem and mysql. It may not have built correctly. see: https://github.com/brianmario/mysql2/issues/954
Option 1: try reverting to 0.4.9 or 0.4.10 gem
set your gemfile to:
# ./Gemfile
...
gem 'mysql2', '0.4.9'
...
Option 2: try to compile the gem locally with c-connector
You may need a local C-connector to properly build the gem locally. see: https://www.digitalgyan.org/how-to-install-ruby-on-rails-mysql2-gem-on-windows-10/
that may be as simple as downloading the appropriate files from MySQL: https://dev.mysql.com/downloads/connector/c/
In either scenario, you may benefit from a quick script to test out connection (to rule out Rails as the problem).
require 'mysql2'
client = Mysql2::Client.new(host: "localhost", username: "rails_user", password: "mypassword")
update mysql2 gem in the Gemfile as:
gem 'mysql2', '~> 0.5.2'

RoR database Error

Trying to run the database in RoR i have this error
Couldn't create database for {"adapter"=>"sqlite3", "pool"=>5, "timeout"=>5000, "database"=>"db/test.sqlite3"}
rake aborted!
Gem::LoadError: Specified 'mysql2' for database adapter, but the gem is not loaded. Add gem 'mysql2' to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).
but when i do gem list i get that i have mysql2 (0.4.1)
How can i fix this? and also, why does this happens?
Check /config/database.yml file
Probably your file contains something like this:
development:
adapter: sqlite3
pool: 5
timeout: 5000
database: db/development.sqlite3
Change sqlite to mysql and add login settings, also check environment (development, production or test)
you need to run bundle install as you have already added the gem in the Gemflle.Also you need to setup mysql and other libs before installing it.
You should include the mysql2 gem into your gemfile and run 'bundle install'.
Also your config/database.yml should look something like the below
development:
adapter: mysql2
encoding: utf8
database: my_db_name
username: root
password: my_password
Here the username and password will be the one you gave at the time of configuring the mysql inyour system

Converting rails project on sqlite3 on local host

I am new to rails and i am facing alot of problems while running the project on local host using mysql database .Is there any way to run a project on sqlite3 on local host and on server it can be run using mysql database.
Yes, you can. In fact, I believe this setup is pretty common.
In your Gemfile, use sqlite3 in the development group, and mysql2 in the production group. For example,
group :production do
gem 'mysql2'
end
group :development, :test do
gem 'sqlite3'
end
Then, on your development machine, use
$ gem install --without production
to avoid installing mysql. Finally, in database.yml, check that the sqlite adapter is selected for test and development, and the mysql adapter is selected for production.
development:
adapter: sqlite3
database: db/development.sqlite3
test:
adapter: sqlite3
database: db/test.sqlite3
production:
adapter: mysql2
encoding: utf8
This should be enough to get you started.

How to resolve Please install the mysql adapter: `gem install activerecord-mysql-adapter` (mysql is not part of the bundle. Add it to Gemfile.)

I am running a rails3 app on Dreamhost: not the best call but I need it operational before I can pitch migrating to Heroku.
I am using rails 3.2.1, and ruby 1.8.7 with
gem 'mysql2', '~> 0.2.7'
in my Gemfile...
I get the following error when I deploy and reload:
Please install the mysql adapter: gem install activerecord-mysql-adapter (mysql is not part of the bundle. Add it to Gemfile.)
I have tried gem 'mysql2', gem 'mysql2', '< 0.3' and get the exact same issue.
Any other ideas?
First try adding config.assets.initialize_on_precompile = false to your application.rb.
Then make sure your DATABASE_URL var starts with mysql2:// instead of mysql://
In general when rails is trying to be helpful it will try to generate the name of an adapter gem based on whatever you have in your database.yml. If that gem doesn't exist it's a good indication that something with the adapter line is wrong
for mysql2 the adapter type needs to be mysql2 as well
database.yml
adapter: mysql2
I had the same problem and after checking if you have in database.yml that you have adapter:mysql2 you should check if you have the gem of mysql loaded.
in your Gemfile you should have the line : gem "mysql2" and not other database like sqlite3 which was my case.
Hope this helps.
I get into the same situation of trying to set up mysql with Ruby on Rails for my
Windows Vista platform installed with MySQL 5.5 and Rails Installer for Ruby 1.93.
After getting the adapter and connector for mysql2, I still get this error message : "rubygems_integration.rb:143:in block in replace_gem': Please install the mysql2
adapter:gem install activerecord-mysql2-adapter` (mysql2 is not part of the b
undle. Add it to Gemfile.) (LoadError)"
I checked the my gem directory "Ruby1.9.3\lib\ruby\gems\1.9.1\gems" and see
both activerecord-mysql2-adapter-0.0.3 and mysql2-0.3.11-x86-mingw32 are there and
the database.yaml file is correctly updated.
The missing piece is here:http://www.mohanarun.com/how-to-install-mysql-adapter-ruby-gem-in-windows/
After restarting the MySQL server and update the gem file as stated in the above URL. It works.
I hope this will work for you.

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