Rails 3.1 rc4 problem with mysql2 - mysql

I just installed passenger (apache2) on my ubuntu server to deploy my rails 3.1 rc4 rails app into the wild, and wanted to change from sqlite to mysql.
I installed the necessary ubuntu packages
I updated my Gemfile with the recommended:
gem 'mysql2'
And updated my database.yml file
production:
adapter: mysql2
database: database
username: deploy
password: (***blocked for security reasons***)
socket: /var/run/mysqld/mysqld.sock
pool: 5
timeout: 5000
When I try to create the production database with rake:
rake db:migrate RAILS_ENV=”production” --trace
It gives me a bunch of crazy errors
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
rake aborted!
”production” database is not configured
/usr/local/rvm/gems/ruby-1.9.2-p180/gems/activerecord- 3.1.0.rc4/lib/active_record/connection_adapters/abstract/connection_specification.rb:62:in `establish_connection'
/usr/local/rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/connection_specification.rb:55:in `establish_connection'
/usr/local/rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.1.0.rc4/lib/active_record/railtie.rb:69:in `block (2 levels) in <class:Railtie>'
/usr/local/rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.1.0.rc4/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
/usr/local/rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.1.0.rc4/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
/usr/local/rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.1.0.rc4/lib/active_support/lazy_load_hooks.rb:26:in `on_load'
(And so on)
I have tried install the regular mysql gem, which also didn't work. I have also tried <0.3 of mysql2, but that seemed to lead to an error being thrown complaining about using versions 0.2 with rails 3.1 rc4
I am really stumped :/

have you tried without the quotes around "production" ?
RAILS_ENV=production rake db:migrate

Related

Rails 4 mysql2 error when deploying to production

I've got an Ubuntu virtual server (Digital Ocean Droplet) set up with Capistrano 3. I've successfully gotten all of the directories set up and the project directory has the current folder in it, containing config/database.yml file. I can't get it deployed to where it will run. It gets stuck on the rake db:migrate phase. rake assets:precompile successfully works, so I think it must be something with my database.yml file. The "production" block looks like this:
config/database.yml (production part)
production:
adapter: mysql2
encoding: utf8
database: capstone_production
username: <%= ENV['DATABASE_USERNAME'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
host: localhost
port: /var/run/mysqld/mysqld.sock
I have checked that the file is valid YAML with YAMLLint utility. Also, the environment variables echo properly with (root) credentials that I am able to use for mysql login. When I run cap production deploy:check, everything is good. However, running cap production deploy does everything okay until it gets to the rake db:migrate Capistrano process. Then it aborts and throws up this:
The deploy has failed with an error: #<SSHKit::Runner::ExecuteError: Exception while executing on host 107.170.105.223: Exception while executing on host 107.170.105.223: rake exit status: 1
rake stdout: rake aborted!
Mysql2::Error: Access denied for user 'root'#'localhost' (using password: NO)
/home/deploy/apps/capstone_project/shared/bundle/ruby/2.1.0/gems/mysql2-0.3.20/lib/mysql2/client.rb:70:in `connect'
/home/deploy/apps/capstone_project/shared/bundle/ruby/2.1.0/gems/mysql2-0.3.20/lib/mysql2/client.rb:70:in `initialize'
/home/deploy/apps/capstone_project/shared/bundle/ruby/2.1.0/bundler/gems/rails-331210df7c8a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb:18:in `new'
/home/deploy/apps/capstone_project/shared/bundle/ruby/2.1.0/bundler/gems/rails-331210df7c8a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb:18:in `mysql2_connection'
/home/deploy/apps/capstone_project/shared/bundle/ruby/2.1.0/bundler/gems/rails-331210df7c8a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:435:in `new_connection'
/home/deploy/apps/capstone_project/shared/bundle/ruby/2.1.0/bundler/gems/rails-331210df7c8a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:445:in `checkout_new_connection'
/home/deploy/apps/capstone_project/shared/bundle/ruby/2.1.0/bundler/gems/rails-331210df7c8a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:416:in `acquire_connection'
/home/deploy/apps/capstone_project/shared/bundle/ruby/2.1.0/bundler/gems/rails-331210df7c8a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:351:in `block in checkout'
/home/deploy/apps/capstone_project/shared/bundle/ruby/2.1.0/bundler/gems/rails-331210df7c8a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:350:in `checkout'
/home/deploy/apps/capstone_project/shared/bundle/ruby/2.1.0/bundler/gems/rails-331210df7c8a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block in connection'
/home/deploy/apps/capstone_project/shared/bundle/ruby/2.1.0/bundler/gems/rails-331210df7c8a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in `connection'
/home/deploy/apps/capstone_project/shared/bundle/ruby/2.1.0/bundler/gems/rails-331210df7c8a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:541:in `retrieve_connection'
/home/deploy/apps/capstone_project/shared/bundle/ruby/2.1.0/bundler/gems/rails-331210df7c8a/activerecord/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
/home/deploy/apps/capstone_project/shared/bundle/ruby/2.1.0/bundler/gems/rails-331210df7c8a/activerecord/lib/active_record/connection_handling.rb:87:in `connection'
/home/deploy/apps/capstone_project/shared/bundle/ruby/2.1.0/bundler/gems/rails-331210df7c8a/activerecord/lib/active_record/migration.rb:916:in `initialize'
/home/deploy/apps/capstone_project/shared/bundle/ruby/2.1.0/bundler/gems/rails-331210df7c8a/activerecord/lib/active_record/migration.rb:814:in `new'
/home/deploy/apps/capstone_project/shared/bundle/ruby/2.1.0/bundler/gems/rails-331210df7c8a/activerecord/lib/active_record/migration.rb:814:in `up'
/home/deploy/apps/capstone_project/shared/bundle/ruby/2.1.0/bundler/gems/rails-331210df7c8a/activerecord/lib/active_record/migration.rb:792:in `migrate'
/home/deploy/apps/capstone_project/shared/bundle/ruby/2.1.0/bundler/gems/rails-331210df7c8a/activerecord/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
rake stderr: Nothing written
I've tried many combinations of YAML syntax and restarted mysql server, puma server, and NGINX server over and over again. This is crazy, please help.
UPDATE
The problem seems to stem from this:
Mysql2::Error: Access denied for user 'root'#'localhost' (using password: NO)
From what I've read in MySQL docs, this error is thrown when you try to login without password. So I've got all the right credentials - I can even hard code the actual username and password into the YAML file and same error. For some reason when Capistrano executes the following command (via cap production deploy or cap production deploy:migrate), it's not including the password from database.yml:
INFO [85893e95] Running ~/.rvm/bin/rvm default do bundle exec rake db:migrate on 107.170.105.223
DEBUG [85893e95] Command: cd /home/deploy/apps/capstone_project/releases/20160205235633 && ( export RAILS_ENV="production" ; ~/.rvm/bin/rvm default do bundle exec rake db:migrate )
How to ensure that Capistrano executes the migration just like I would locally in development?
Thank you

Heroku installing Amazon RDS : mysql or mysql2?

I'm installed the Amazon RDS Add-on in Heroku, and created OK an instance in Amazon AWS. But I can't run
heroku run rake db:migrate
It's ending with
(...)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
rake aborted!
undefined method `accept' for nil:NilClass
/app/vendor/bundle/ruby/1.9.1/gems/activerecord- 3.2.11/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `to_sql'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/database_statements.rb:38:in `select_values'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:587:in `get_all_versions'
When I created my instance I followed the instructions from Heroku :
$ heroku addons:add amazon_rds url=mysql2://user:pass#rdshostname.amazonaws.com/databasename
And since then, I'm fighting with Mysql2. First I noticed in Heroku that it was pointing to
mysql://user:pass#mydatabase.us-east-1.rds.amazonaws.com/MyFirstDb
Is it normal that there is no 2 like mysql2://user ? I added it. I changed my Gemfile with
gem 'mysql2', '< 0.3.7'
and my database.yml with
production:
adapter: mysql2
database: db/production.mysql2
before it was set as sqlite3.
I'm newbie, this is my first app that I want to deploy to Amazon AWS so maybe I miss something obvious ! Any idea ?
I got it ! I had forgotten to
gem install activerecord-mysql2-adapter
The error message in Heroku says to run gem install activerecord-mysql-adapter (without the 2), but with the 2, it works perfectly now !
I hope this post will help others. So, to sum up, here is what you need to do :
1- Set the "2" in the add-on link in Heroku
mysql2://user:pass#mydatabase.us-east-1.rds.amazonaws.com/MyFirstDb
2- Add a line to your Gemfile :
gem 'mysql2', '~> 0.3.11'
3- Change your database.yml
production:
adapter: mysql2
database: db/production.mysql2
4- Run
gem install activerecord-mysql2
heroku run rake db:migrate
And enjoy ! :-)

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.

Can't open rails console: production database not configured, establish_connection raises ActiveRecord::AdapterNotSpecified

My app works pretty much perfectly in production – the website part of it, at least. The problem only pops up when I SSH into my VPS and do "rails c RAILS_ENV=production". The console works fine in development mode.
I've had this problem before (or at least one that looks like it) and fixed it by adding "reconnect: true" to database.yml – but it didn't fix it this time around. Here's the start of the error output:
/home/tour/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/connection_adapters/abstract/connection_specification.rb:62:in `establish_connection': RAILS_ENV=production database is not configured (ActiveRecord::AdapterNotSpecified)
from /home/tour/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/connection_adapters/abstract/connection_specification.rb:55:in `establish_connection'
from /home/tour/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/railtie.rb:59:in `block (2 levels) in <class:Railtie>'
from /home/tour/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
from /home/tour/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
from /home/tour/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/lazy_load_hooks.rb:26:in `on_load'
from /home/tour/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/railtie.rb:57:in `block in <class:Railtie>'
from /home/tour/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.0/lib/rails/initializable.rb:25:in `instance_exec'
from /home/tour/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.0/lib/rails/initializable.rb:25:in `run'
from /home/tour/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.0/lib/rails/initializable.rb:50:in `block in run_initializers'
And for my production database settings:
production:
adapter: mysql
encoding: utf8
database: tour_production
pool: 5
username: [user]
password: [password]
socket: /var/run/mysqld/mysqld.sock
reconnect: true
I can't get my mind around why it works through passenger, but no luck with the console. Anyway, I'm using Rails 3, Ruby 1.9.2, Passenger and Nginx.
I've been doing some little stuff through Runner, which is annoying. I'd like to get this fixed. So thanks a bunch for any help.
When you are using the console you specify the environment as the first argument rather than as an environment variable. So:
ruby script/console production
will load the production environment.
Your error message is because it is looking for the environment RAILS_ENV=production rather than just production.
The latest Rails 3 way of doing this is simply:
bundle exec rails console -e production
#Shadwell is correct, however if you're after a consistent syntax you can set RAILS_ENV at the beginning like this:
RAILS_ENV=production rails console
I solved with run:
RAILS_ENV=production bundle exec rails c
more detail on link
or
stackoverflow