yellowreign
yellowreign

Reputation: 3638

Setting Up Rails on EC2 and RDS

I have an EC2 instance with my Rails code, and I'm trying to set it up to talk to Amazon RDS, but I'm getting an error when I try to start the rails server that I don't know how to correct:

/opt/bitnami/ruby/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11/lib/mysql2/client.rb:44:in `connect': Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) (Mysql2::Error)

In my database.yml file, I have this:

development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: development
  pool: 5
  username: root
  password: secret
  host: localhost

production:
  adapter: mysql2
  encoding: utf8
  database: mydbname
  username: myusername
  password: mypassword
  host: mypublicdnsformydb
  port: 3306

I assigned the RDS instance to the same security group as my EC2 which has:

22 (SSH)    0.0.0.0/0
80 (HTTP)   0.0.0.0/0   
443 (HTTPS) 0.0.0.0/0

When I add my PC's CIDR, I can connect to the instance perfectly fine, so I'm pretty sure that my username, password and host are correct.

Upvotes: 0

Views: 2195

Answers (3)

Isaac Queiroz
Isaac Queiroz

Reputation: 3

This worked just fine for me, add in the first line of your Gemfile

if RUBY_VERSION =~ /1.9/
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
end

Source: Alan Ho Comment: http://www.a.rnaud.net/2011/10/how-to-fix-invalid-byte-sequence-in-us-ascii-in-bundler-installation/

Upvotes: 0

yellowreign
yellowreign

Reputation: 3638

To fix this, I added this line in my database.yml for production

socket: /opt/bitnami/mysql/tmp/mysql.sock

and I ran my rails server specifying production as the environment:

rails s -e production

Upvotes: 1

nnahum
nnahum

Reputation: 364

I think the problem is that your development environment points to a mysql local, and not RDS. By default Rails runs the development environment not the production, so I would try adding the same config of the production in the dev:

 development:
  adapter: mysql2
  encoding: utf8
  database: mydbname
  username: myusername
  password: mypassword
  host: mypublicdnsformydb
  port: 3306

Upvotes: 0

Related Questions