Error when running rake db:migrate - mysql

I'm following the railscast where Ryan sets up a VPS. But I need to install mysql instead of postgresql.
when I run the cap deploy:cold command, it all seems to run fine, until it runs the bundle, which fails when trying to install the mysql2 gem.
so i logged into the server and installed it manually (the database had already been setup).
but when i run the rake db:migrate command I keep getting this error:
rake aborted!
database configuration does not specify adapter
Tasks: TOP => db:migrate => db:load_config
in the database.yml file i have the following
production:
adapter: mysql2
encoding: utf8
reconnect: false
database: blog_production
pool: 5
username: blog
password: *****
host: localhost
socket: /var/run/mysqld/mysqld.sock
I've edited the socket from default, added and removed the host line, and installed the gem manually, but nothing seems to make it work!

This usually happens when your deployment scripts or the currently logged in user on the VPS have not set the RAILS_ENV environment variable. Then it defaults to 'development' and your database.yml would have no configuration for that environment. So try
RAILS_ENV=production bundle exec rake db:migrate --trace
instead.

Corrupt yml file solved it for me.
Renamed and recreated the database.yml file and it worked.

Related

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

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.

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.

Rails : rake db:create mysql error

I am trying to setup a mysql database, but I am getting this error message:
rake db:create
(in /Users/yookd/Desktop/rails/blog)
WARNING: Global access to Rake DSL methods is deprecated. Please Include
... Rake::DSL into classes and modules which use the Rake DSL methods.
WARNING: DSL method Blog::Application#task called at /Library/Ruby/Gems/1.8/gems/railties-3.0.7/lib/rails/application.rb:215:in `initialize_tasks'
db/test.sqlite3 already exists
rake aborted!
uninitialized constant Mysql2
Tasks: TOP => db:create
(See full trace by running task with --trace)
Any assistance with this?
(following directions on http://guides.rubyonrails.org/getting_started.html)
EDIT:::
How can I use mysql as my database as opposed to sqlite? In the gemfile, it explicitly states: gem 'sqlite'... do I need to replace that line with something like gem mysql?
This is a known issue. See: Rails - rake db:create error
BUT note that Rake 0.9.1 has been released which supposedly fixes things. So first try upgrading rake with:
gem update rake
And update your bundle:
bundle update rake
For your edit, yes, to use mysql you have to first have mysql installed and configured on your system. Then in your gemfile, remove 'gem sqlite' and replace it with:
gem 'mysql'
Then, in your config/database.yml, set it up something similar to:
development:
adapter: mysql
database: name_of_database
pool: 5
timeout: 5000
host: localhost
password: mysql_password
Do the same for test and production.

rake aborted! uninitialized constant Mysql2

Goksel-Eryigits-Mac-mini:blog geryit$ rake db:migrate --trace
(in /Users/geryit/Sites/blog)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
rake aborted!
Client does not support authentication protocol requested by server; consider upgrading MySQL client
/Users/geryit/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/mysql.rb:453:in `read'
/Users/geryit/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/mysql.rb:130:in `real_connect'
/Users/geryit/.rvm/gems/ruby-1.8.7-p330/gems/activerecord-3.0.3/lib/active_record/connection_adapters/mysql_adapter.rb:600:in `connect'
/Users/geryit/.rvm/gems/ruby-1.8.7-p330/gems/activerecord-3.0.3/lib/active_record/connection_adapters/mysql_adapter.rb:164:in `initialize
.
.
.
database.yml
development:
adapter: mysql
encoding: utf8
database: test
pool: 5
username: root
password:
socket: /tmp/mysql.sock
Have you defined
gem 'mysql2'
in Gemfile? For Rails 3 and Bundler, it's not enough to install the gem, but to include it in Gemfile.
They work together now without any problems. The problem was Mysql version and mysql gem flags. I removed MYSQL 5.5 and installed Mysql 5.1 back. Everything is fixed. Thanks.
I wrote a blog post about installing, it can be helpful : https://geryit.com/blog/installing-mysql-with-rails-on-mac-os-x-snow-leopard/
Try uninstalling the mysql gem. It might be conflicting with the mysql2 gem you're using in your database.yml.
I was getting 'uninitialized constant Mysql' on Snow Leopard and Rails 3. Adding 'mysql' gem to Gemfile fixed it for me.