27
17

More than 3 years have passed since last update.

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

Last updated at Posted at 2020-06-24

目標

・EC2サーバとRDS(MySQL)間の接続を確立する。

前提

・構築済みのEC2が存在すること。

利用環境

EC2
OS(AMI) : Amazon Linux 2 AMI (HVM), SSD Volume Type
ソフトウェア: mysql Ver 14.14 Distrib 5.5.62, for Linux (x86_64) using readline 5.1

RDS
データベースエンジン: MySQL(5.7.22)

参考AWSドキュメント

MySQL DB インスタンスを作成して MySQL DB インスタンス上のデータベースに接続する
https://docs.aws.amazon.com/ja_jp/AmazonRDS/latest/UserGuide/CHAP_GettingStarted.CreatingConnecting.MySQL.html

作業の流れ

項番 タイトル
1 RDSを作成する
2 MySQLクライアント接続

手順

1.RDSを作成する

①AWS マネジメントコンソールにサインインし、Amazon RDS コンソール (https://console.aws.amazon.com/rds/) を開く
データベースの作成をクリック
③データベースの作成方法を選択
以下のように、各値を投入してください。

簡単作成(データベースの詳細設定の多くをデフォルトで設定してくれます)
データベースエンジンはMySQL
tempsnip.png

任意のDBインスタンス識別子マスターユーザ名(MySQL接続時の認証等で利用されます)を入力
tempsnip.png

マスターパスワードを入力(パスワード自動生成することも可能、MySQL接続時の認証で利用されます)
tempsnip.png

④作成したRDSが利用可能になるまで待機
tempsnip.png

⑤作成したRDSの3306番ポートを開く
作成したRDSの詳細画面からVPCセキュリティグループをクリック(利用ポートが3306番であることも確認)
tempsnip.png

3306番ポートを許可
tempsnip.png

2.MySQLクライアント接続

①EC2にOSログイン
②MySQLクライアントインストール
sudo yum install mysql実行


# Complete!と出力されること
[ec2-user@ip-172-31-32-13 ~]$ sudo yum install mysql
(中略)
Installed:
  mysql.noarch 0:5.5-1.6.amzn1

Dependency Installed:
  mysql-config.x86_64 0:5.5.62-1.23.amzn1 mysql55.x86_64 0:5.5.62-1.23.amzn1
  mysql55-libs.x86_64 0:5.5.62-1.23.amzn1

Complete!

③MySQLログイン
mysql -h <RDSエンドポイント名> -P 3306 -u <マスターユーザ名> -p実行


# マスターユーザのパスワードを入力し、正常にログイン出来ればOK
[ec2-user@ip-172-31-32-13 ~]$ mysql -h database-test.cgfjapta11py.ap-northeast-1.rds.amazonaws.com -P 3306 -u admin -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
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>
27
17
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
27
17