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)  

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

    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.
    socket      = /var/run/mysqld/mysqld.sock
    nice        = 0

    # * 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


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


    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
  • host: <censored endpoint> ... contains the hostname of your RDS instance, as seen in the AWS console? – Michael - sqlbot Aug 5 '14 at 10:44
  • Yes, It's a long string like this database-name.jldfs892je9ijsf.us-west-2.rds.amazonaws.com – ninja08 Aug 5 '14 at 16:32
  • Actually when I try to type 'mysql' or using the mysql command it gives me errors. For the former it gives me "The program 'mysql' can be found in the following packages:" and lists out some mysql packages. I've also found that my database isn't running production! That might be the reason it's not connecting. – ninja08 Aug 6 '14 at 4:25

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

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.