Ubuntu – thesql.sock error when deploying with AWS Ubuntu and RDS

amazon-rdsamazon-web-servicesMySQLruby-on-railsUbuntu

 Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
 Rails.root: /opt/deployment.myproject/releases/20140805050704

I've spent the better part of 3 days trying to find an answer to this question. Also trying to understand what's happening behind the scenes, but now I think it's a good time to just ask the world for help!

I'm deploying a Ruby on Rails application with AWS on an Ubuntu AMI, EC2, and RDS. I decided to go with a MSQL database because that's what I've built my app with, which has me using RDS.

Since I'm using RDS, and I have to provide an endpoint, username, and password, I'm thinking that maybe the hint "local" in the error tells me that it thinks the database is local. I believe it's not since it's on a different instance from my EC2 and being linked to. (Amirite?)

I've tried to search for a mysql.sock file with many different types of 'find' and 'grep' commands. The ones that actually do get a search return a huge list of directories displaying something like "no permission".

I've also searched for it at the path the error displays but there is no mysql directory in the run directory.

A few questions that I think might help answer this issue..

When exactly is this file created? When the database is created? Shouldn't this file be on the RDS instance then? Or is it created when I run either 'cap deploy:setup' or 'cap deploy'?

Since it says "Rails.root" I'm thinking that it's looking for the file starting from the root of the app, specifically. So something like app/var/run…etc instead of the actual root.

Oh, and by the way. When I FTP into the EC2 instance I get this message as well.

Could not change directory to “var/run/mysqld/mysqld.sock' (2)  
Rails.root:/opt/deployment.myproject/releases/20140803230821”.

Also: I was able to connect to RDS via Sequel Pro and my migrations ran perfectly via Capistrano because all my tables are there!

I think I've said enough. Here are some files that might be relevant:

my.cnf shortened for brevity

    [client]
    port        = 3306
    socket      = /var/run/mysqld/mysqld.sock

    # Here is entries for some specific programs
    # The following values assume you have at least 32M ram

    # This was formally known as [safe_mysqld]. Both versions are currently parsed.
    [mysqld_safe]
    socket      = /var/run/mysqld/mysqld.sock
    nice        = 0

    [mysqld]
    #
    # * Basic Settings
    #
    user        = mysql
    pid-file    = /var/run/mysqld/mysqld.pid
    socket      = /var/run/mysqld/mysqld.sock
    port        = 3306
    basedir     = /usr
    datadir     = /var/lib/mysql
    tmpdir      = /tmp
    lc-messages-dir = /usr/share/mysql

database.yml

    production:
      adapter: mysql2
      database: <censored db name> 
      host: <censored endpoint>
      username: <censored username>
      password: <censored password>
      port: 3306
      pool: 5
      timeout: 5000

deploy.rb

    role :web, '<censored db name>'
    role :app, '<censored db name>'
    role :db,  '<censored db name>', primary: true

    set :rails_env,    'production'
    set :webroot,      '/opt/myproject'        # specify directory symlink
    set :deploy_to,    '/opt/deployment.myproject'
    set :user,         'ubuntu'
    set :use_sudo,     false
    set :bundle_flags, '--deployment --quiet' # some bundler options

    after 'deploy:update_code', 'deploy:symlink_shared'
    after 'deploy:update_code', 'deploy:migrate'

    load 'deploy/assets' # results in 'rake assets:precompile' during deploy

Best Answer

Capistrano is setup to use the production environment, but whatever command/script/etc you are using to actually start the Rails app on the box is likely in development mode.

export RAILS_ENV=production
bin/rails s