LoginSignup
0
0

More than 1 year has passed since last update.

【Rails】簡単にRDSを使いたい

Posted at

環境

ホスト:MacOS
ゲスト:CentOS6
Vagrant
MySQL

Problem

https://qiita.com/Alice_ecilA/items/54b4624f0126c7eec5e5
↑こちらの続きです。
vagrantをコピーしたので、アプリケーション側とRDS側にしていきます。

Solve

アプリケーションサーバ

config/database.yml
development:
  <<: *default
  database: my_database
  username: apps
  password: this_is_password
  host: 192.168.33.11

アプリケーション側はdatabase.ymlにRDSの接続情報を追加してあげればOK。

RDS用

mysql -u root [-p] #パスワードがある場合-p
-- アプリケーションサーバからの接続を許可する
mysql> GRANT all ON *.* TO apps@'192.168.33.22' IDENTIFIED BY 'this_is_password';
mysql> FLUSH PRIVILEGES;

-- 先程のIPが登録されていれば接続できる状態
mysql> SELECT user,host FROM mysql.user;
+------------------+---------------+
| user             | host          |
+------------------+---------------+
| apps             | 192.168.33.22 |
| apps             | localhost     |
| root             | localhost     |
+------------------+---------------+

RDS側はmysqlに入り、アプリケーションサーバのIPアドレスを登録して接続を許可すればOK。

まとめ

これでアプリケーションとRDSの通信ができるようになりました。
外部のDBを使いたい場合はこれが応用できると思います。
もしうまく行かないのであれば、RDS側のOSで外部との通信を許可しているか?(Firewall)
MySQLサーバの設定ファイル(mysqld.cnf)でIPアドレスの制限を解除しているか?確認してみましょう。

0
0
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
0