0

I'm trying to setup two Rails applications. I want both of them to use one MySQL database. My setup is like this:

With Elastic Beanstalk I've setup two Rails applications with two diffrent enviroments. One of those applications I setup with a MySQL database. So now I have two EC2 instances - one for each rails app and one RDS MySQL instance.

I setup Security Groups in both EC2 instances to be able to connect to MYSQL via port 3306.

I've setup the DB Security Group to authorized both EC2 instances and also I setup the CIDR/IP with the Private IP of the second EC2 Instance (the one without the MySQL database set while configuring Beanstallk Application)

In both Rails applications I setup the database.yml like:

production:
  adapter: mysql2
  encoding: utf8
  database: <%= ENV['RDS_DB_NAME'] %>
  username: <%= ENV['RDS_USERNAME'] %>
  password: <%= ENV['RDS_PASSWORD'] %>
  host: <%= ENV['RDS_HOSTNAME'] %>
  port: <%= ENV['RDS_PORT'] %>

I assigned the ENV values for the production on the second instance (that one without the MySQL DB) with ENV values from the first EC2 instance.

What am I missing here?

I also added my computers IP address to the DB Securty Group and I'm able to connect to the MySQL DB with the credentials I use as ENV varaibles.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
kabebop
  • 73
  • 7

3 Answers3

2

You might be missing this:

socket: /var/lib/mysql/mysql.sock
  • Thx, but it didn't help. I added it to the database.yml in production section and it didn't work. The database is not on the local server - it's an additional instance on amazon. With this line I'm still getting an error `Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) (Mysql2::Error)` – kabebop Apr 23 '13 at 17:51
  • Try this: http://stackoverflow.com/questions/5499035/ruby-on-rails-3-cant-connect-to-local-mysql-server-through-socket-tmp-mysql-s – Nasim Khamidov Apr 23 '13 at 19:43
  • I was looking at this but it's not my case. The MySQL DB is on a remote instance, so it's not installed localy on either of the EC2 instances. I can connect to it from only one instance, and this is the problem. I think the socket error is because when rails can't find the remote DB it's trying to find it on the local machin. – kabebop Apr 24 '13 at 10:46
0

Problem solved - instead of the ENV variables I set the DB credentials in the database.yml file. The enviroment variables were overwritten by every push with git aws.push on elastic beanstalk.

kabebop
  • 73
  • 7
0

In my case, it was because I used the wrong ENV variable names. You can double check you've set everything correctly by connecting to your instance with eb ssh, and then printenv | grep RDS, which should give you something like:

RDS_HOSTNAME=yourhostname.amazonaws.com
RDS_DB_NAME=ebdb
RDS_PASSWORD=yourpassword
RDS_USERNAME=yourusername
RDS_PORT=3306

Ensure these are reflected in your config/database.yml file - either by using <%= ENV['varname'] %>, or by hardcoding them. The former option being preferable, obviously. :)

Mythical Fish
  • 293
  • 2
  • 7
  • 20