0

I am currently trying to deploy my Rails 4 app to AWS, but each time I try to view the app on AWS, I get an application error. I check the logs and see:

Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

I've been reading several things, such as: this, but I haven't had any luck trying these various solutions and I'm driving myself crazy.

The app runs fine locally.

Here are some details:

  • I have Mysql and Mysql server installed
  • The service is running (again everything works as expected locally)

Here is the [client] portion of my.cnf, which is located at /etc/mysql/

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

I see some suggestions talking about mysqld, others mysql - I don't understand the difference.

Database.yml

development:
  adapter: mysql2
  encoding: utf8
  database: wp_development
  pool: 5
  username: root
  password: **Left out
  host: localhost

I just downloaded MySql today, so I am on 5.5, the latest build. *I'm running Linux.

Can someone please point me toward a solution?

Thanks!

Community
  • 1
  • 1
  • This has been covered in other posts. http://stackoverflow.com/questions/5499035/ruby-on-rails-3-cant-connect-to-local-mysql-server-through-socket-tmp-mysql-s – Felipe Garcia Sep 15 '13 at 02:16
  • As I mentioned, I've seen a bunch of these solutions and nothing has worked. Server seems to be running. I have a socket in database.yml and changing to 127.0.0.1 just gives another error "Cannot connect to MySql on 127.0.0.1". –  Sep 15 '13 at 04:47

2 Answers2

0

If you want to connect locally, you should add socket: /var/run/mysqld/mysqld.sock inside the development config in your database.yml.

Also, make sure your RAILS_ENV is correct when running your app on AWS. If it is not set, then it should be development. But I'm not sure how you are starting your app.

masahji
  • 544
  • 2
  • 6
  • Ok, so I added the socket line to my database.yml, and AWS sets the RAILS_ENV variable to production by default, so I changed that to "development". I've made progress. I no longer get an "application failure" error page on AWS, now I get a Rails error page, but the page still complains of the "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)" error. –  Sep 15 '13 at 04:04
  • I am using Beanstalk on AWS to start the app. "eb start". Handles pretty much everything I believe. –  Sep 15 '13 at 04:05
0

The reason could be the specified socket path in your Gemfile might be wrong.

First, to find your socket file:

mysqladmin variables | grep socket

For me, this gives:

| socket  /var/run/mysqld/mysqld.sock 

Then, add this line /var/run/mysqld/mysqld.sock to your config/database.yml in the socket field. See the below examples

Example: socket: /var/run/mysqld/mysqld.sock

Development Mode

development:
  adapter: mysql2
  host: localhost
  username: root
  password: xxxx
  database: xxxx
  socket: /var/run/mysqld/mysqld.sock #  this line

Production Mode

production:
  adapter: mysql2
  host: localhost
  username: root
  password: xxxx
  database: xxxx
  socket: /var/run/mysqld/mysqld.sock  #  this line

Test Mode

test:
  adapter: mysql2
  host: localhost
  username: root
  password: xxxx
  database: xxxx
  socket: /var/run/mysqld/mysqld.sock  #  this line
Prabhakar
  • 6,458
  • 2
  • 40
  • 51