RoR database Error - mysql

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

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 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.

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.

Need to switch from SQLite3 to MySQL, and want to deploy with MySQL

We are currently using SQLite3 as our database and want to switch it to MySQL before we send our Rails app live via Passenger and Nginx to our Linode Ubuntu 10.04 Lucid box.
we set up the msql server on our linode ubuntu 10.04 box
and set a password for the root user. how do we configure database.yml ? what are the steps to set up this MySQL database? We have the mysql2 gem installed and in our Gemfile.
What does database.yml need to look like in order to deploy with MySQL ?
Does the database need any specific name as in database name?
Do we go about running rake db:create on our machine or run it on our Ubuntu box?
current database.yml
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 2000
# 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:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 2000
production:
adapter: mysql2
encoding: utf8
database: production
pool: 5
username: root
password: "mysql password"
Did you try a simple connection test? If not, here is a script to test the connection to your db.
#! /usr/bin/ruby
# getting required gems
require 'rubygems'
require 'mysql'
## ----- test the connection -----
# http://rubydoc.info/gems/mysql/2.8.1/frames
# real_connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, sock=nil, flag=nil)
mysql = Mysql.connect('localhost', 'root', 'password', 'db_name', 3006, '', '')
## ----- if connected, will list databases -----
puts mysql.list_dbs().to_s
In case the 'mysql' did not install properly, you will have to custom configure the install properties. On my Mac (different that you linux machine), I referenced this blog post to get something like this,
sudo env ARCHFLAGS="-arch x86_64" gem install --no-rdoc --no-ri mysql -- --with mysqlconfig=/usr/local/mysql/bin/mysql_config
UPDATE - Had to reinstall mysql gem for a different ruby version, and removing the architecture notation allowed me to install the gem.
sudo gem install --no-rdoc --no-ri mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
On your production server, go to your app's root directory and type nano config/database.yml and paste this code in. Change the names.
production:
adapter: mysql
database: something_production
username: somethingotherthanroot
password: passwordnostring
host: 127.0.0.1
Now, either create the database on your own, on the server, or you can run rake RAILS_ENV=production db:create:allhell i've never tried but i'm sure you can do rake db:create:production too.
once done, just do rake RAILS_ENV=production db:migrate
Looks like your database.yml is fine, except for the fact that you should have your production name database named like this: projectname_production
Answering your question of how to create the database once on the server, ssh into your application server and do a rake db:create RAILS_ENV=production
This will create the production database on the server. Same thing with your migration. It should be rake db:migrate RAILS_ENV=production. This will start the migration for your production database.

How do I configure PostgreSQL or MySQL to set up a database with Sphinx?

I really need help installing Sphinx and getting it going.
I'm on rails 3 and I'm following these set of directions: http://freelancing-god.github.com/ts/en/quickstart.html to install Sphinx.
Thinking Sphinx was easy to install since it's a gem. However, the guide says I need to have both Sphinx and Thinking Sphinx installed to get started (Is this true?). I checked Ryan Bates' railscast for Thinking Sphinx and he only installed the plugin and got started immediately.
Anyways, I installed Sphinx via MacPorts and here's proof:
...
---> Configuring mysql5
---> Building mysql5
---> Staging mysql5 into destroot
---> Installing mysql5 #5.1.57_0
The MySQL client has been installed.
...
---> Installing sphinx #0.9.9_0+mysql5
---> Activating sphinx #0.9.9_0+mysql5
---> Cleaning sphinx
It automatically configured Sphinx with mysql5. I have both mySQL and POSTgreSQL.
Now the next step was to rake thinking_sphinx:index after setting up an index in my user model which looks like this:
define_index do
indexes year
indexes major
indexes books_buy
indexes books_sell
indexes facebook
indexes restaurants
indexes interests
end
However, rake thinking_sphinx:index gave me this:
rake aborted!
Don't know how to build task 'thinking_sphnix:index'
and then
rake aborted!
no such file to load -- sqlite3
So I figured my database was configured on sqlite3 still. So I switched the gem to mySQL and edited my database.yml file to look like this:
# Switched over to mysql
# 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.
development:
adapter: mysql5
encoding: utf8
database: sphinx_development
pool: 5
username: root
password:
socket: /tmp/mysql.sock
test:
adapter: mysql5
encoding: utf8
database: sphinx_test
pool: 5
username: root
password:
socket: /tmp/mysql.sock
production:
adapter: mysql5
encoding: utf8
database: sphinx_production
pool: 5
username: root
password:
socket: /tmp/mysql.sock
and I got these errors:
rake aborted!
Please install the mysql5 adapter: `gem install activerecord-mysql5-adapter` (no such file to load -- active_record/connection_adapters/mysql5_adapter)
and
ERROR: Could not find a valid gem 'activerecord-mysql5-adapter' (>= 0) in any repository
ERROR: Possible alternatives: activerecord-jdbcmysql-adapter, activerecord-nulldb-adapter, activerecord-odbc-adapter, activerecord-jdbc-adapter, activerecord-postgis-adapter
Tony-Ngs-MacBook-Air:sample_app TonyNg$ gem install activerecord-nulldb-adapter
Any tips would be generous and helpful. Would also be willing to switch to PostgreSQL if help lead provided. Thanks!
You need to put either mysql or mysql2 as your adapter - and include either gem (same names as the adapters) in your Gemfile. I recommend mysql2, as it's actively maintained - but keep in mind if you're using Rails 3.0.x, then you must use a 0.2.x release of mysql2. If you're on Rails 3.1, then use mysql2 0.3.x.