LoginSignup
0
1

More than 3 years have passed since last update.

【RDS/Rails】Railsサーバ(AWS Cloud9で構築)とRDS(MySQL)間の接続を確立する

Posted at

目標

Railsサーバ(AWS Cloud9によって構築)とRDS(MySQL)間の接続を確立する

はじめに

ほぼメモです。
Railsはデフォルトでsqlliteをデータベースとして利用する設定になっているので、
それをRDSで構築したMySQLを利用するよう設定変更しました。

前提

・RailsサーバがCloud9によって構築済みであること(※)。

※以下記事の「Cloud9にチャレンジ!」までを実施する
[初心者向け]Cloud9で五分でできる、"Yay! You’re on Rails!"(Cloud9にチャレンジ!)

作業の流れ

項番 タイトル
1 RDSを構築する
2 Railsサーバから接続する

手順

1.RDSを構築する

以下記事の「1.RDSを作成する」を実施します。

【RDS】EC2とRDS(MySQL)間の接続を確立する(1.RDSを作成する)

2.Railsサーバから接続する

①Railsの新規アプリケーション作成


$ rails new test_app

作成後、アプリケーション配下へ移動します。


$ cd test_app

②mysql-develをyumインストール


$ sudo yum install mysql-devel

③Gemfile編集

デフォルトでsqlliteのgemをインストールする仕様となっているので、
mysqlをインストールするよう以下のように編集します。

gem 'sqlite3'⇒コメントアウト
gem 'mysql2'⇒追加


ファイル名: Gemfile

#gem 'sqlite3'
gem 'mysql2'

④MySQLのgemインストール(bundle install)


$ bundle install

⑤database.yml編集
development環境にmysqlのデータベースを利用するようdatabase.ymlを編集(デフォルトのsqlliteへの接続定義は上書き削除)(※)

※以下に関しては値を置換する必要があります。
<db_username><db_password><rds_endpoint>
⇒前手順「1.RDSを構築する」で構築したRDSの内容に従って書き換え

<db_name>
⇒任意の名前でOK。


ファイル名: database.yml

default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: <db_username>
  password: <db_password>
  host: <rds_endpoint>
  database: <db_name>
  socket: /var/lib/mysql/mysql.sock

development:
  <<: *default

⑥データベース作成


$ rails db:create

⑦アプリケーション起動


rails s

⑧検証テーブル作成


$ rails generate model user name:string

$ rails db:migrate

⑨Mysql接続
mysql -h <rds_endpoint> -u <db_username> -p <db_name>


$ mysql -h database-test.cgfjapta11py.ap-northeast-1.rds.amazonaws.com -u admin -p practice
Enter password: 
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 24
Server version: 5.7.22-log Source distribution

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

接続後、一応データベース及びテーブルが正常に作成されているか確認
show databases;
show tables;


mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| innodb             |
| mysql              |
| performance_schema |
| practice           |
| sys                |
| testdb             |
+--------------------+
7 rows in set (0.00 sec)

mysql> show tables;
+----------------------+
| Tables_in_practice   |
+----------------------+
| ar_internal_metadata |
| schema_migrations    |
| users                |
+----------------------+
3 rows in set (0.00 sec)
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1