Report this

What is the reason for this report?

Rails 4 mysql2 error when deploying to production

Posted on February 5, 2016

I’ve got a 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.

Thank you



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Did you ever resolve this? I have the same issue. My database.yml has the correct credentials, but Capistrano always tries root with no password. The database.yml is set up to use a deploy user with a password.

To me it seems that Capistrano is not using the correct Rails.Env.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.