Connect MySQL RDS to a Rails application
suggest changeCreate the MySQL database
- Sign in to AWS and open the RDS service.
- Choose
Launch DB Instance. - MySQL Community Edition is selected by default. Click
Select. - Choose the use case, for example
production, and clickNext. - Set the MySQL version, storage size, DB instance identifier, master username, and password, then click
Next. - Enter the database name and click
Launch DB Instance. - Wait until the instance is available. Copy the endpoint. That value is the hostname.
Install the adapter
Add the MySQL adapter to the Gemfile:
gem 'mysql2'
Then run bundle install.
Other adapters:
gem 'pg'for PostgreSQLgem 'activerecord-oracle_enhanced-adapter'for Oraclegem 'activerecord-sqlserver-adapter'for SQL Server
Configure config/database.yml
<%= RDS_DB_NAME %> is not an environment variable. Read the values from ENV:
production:
adapter: mysql2
encoding: utf8
database: <%= ENV["RDS_DB_NAME"] %> # name set when creating the database
username: <%= ENV["RDS_USERNAME"] %> # master username
password: <%= ENV["RDS_PASSWORD"] %> # master password
host: <%= ENV["RDS_HOSTNAME"] %> # instance endpoint
port: <%= ENV["RDS_PORT"] %> # MySQL default is 3306
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents