Rails 4 / Unicorn app can't connect to MySQL when running as non-root user

353 views Asked by At

I have a VPS hosted on digital ocean and am running a simple Rails app with an nginx + unicorn setup.

I originally cloned the project to the root home directory and ran everything as root for a quick and dirty setup.

Recently I created a more dedicated user and used capistrano to deploy the app to /var/www/my_app/current.

I'm starting the app up with the command

SECRET_KEY_BASE="6969..." RAILS_ENV="production" bundle exec unicorn -c config/unicorn.rb -D

I've re-pointed the nginx file to read from the new unicorn socket location (under /var/www/current/tmp/sockets/unicorn.sock) and am using the same above exact command to start up unicorn. However, when running as any non-root user it can't seem to connect to mysql

log/unicorn.log

I, [2015-06-10T23:43:39.531175 #18610]  INFO -- : Refreshing Gem list
I, [2015-06-10T23:43:41.765092 #18610]  INFO -- : unlinking existing socket=/var/www/my_app/releases/20150610234033/tmp/sockets/unicorn.sock
I, [2015-06-10T23:43:41.765431 #18610]  INFO -- : listening on addr=/var/www/my_app/releases/20150610234033/tmp/sockets/unicorn.sock fd=12
E, [2015-06-10T23:43:41.767981 #18610] ERROR -- : Access denied for user 'my_app'@'localhost' (using password: YES) (Mysql2::Error)
/var/www/my_app/shared/bundle/ruby/2.0.0/gems/mysql2-0.3.18/lib/mysql2/client.rb:70:in `connect'
/var/www/my_app/shared/bundle/ruby/2.0.0/gems/mysql2-0.3.18/lib/mysql2/client.rb:70:in `initialize'
/var/www/my_app/shared/bundle/ruby/2.0.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/mysql2_adapter.rb:18:in `new'
/var/www/my_app/shared/bundle/ruby/2.0.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/mysql2_adapter.rb:18:in `mysql2_connection'

Another post suggested that it might have something to do with specifying the mysql socket for connection, so I added a line to my database.yml that tells it where to find the mysql socket

config/database.yml

...
username: 'my_app'
password: 'some-password'
socket: '/var/run/mysqld/mysqld.sock'

No luck with that either...

I'm positive the user is set up the connect to the DB because I can start the app up using rails server, so it's clearly a unicorn related thing.

Any thoughts? Thanks!

UPDATE

I've pinpointed the issue further. I use shell/environment variables in my database.yml to read the username / password for mysql. When I replace them with hard-coded strings, it works fine. It appears the issue is with unicorn reading the shell variables and/or processing the file through erb

0

There are 0 answers