概要
RDSを構築してローカルマシンからアクセスするまでの処理について記載する。
準備
・AWSアカウント
手順
1.セキュリティグループの設定
RDSのアクセスを許可するために、インバウンドルールのプロトコルとポート範囲を指定する。
- タイプ:MYSQL/Aurora
- プロトコル:TCP
- ポート範囲:3306
- ソース:任意※
※IPアドレスによるアクセス制限をかける場合は、こちらにIPをアドレスを設定(xxx.xxx.xxx.xxx/xx)
2.RDSの作成とMySQLの設定
以下の記事を参考にRDSの設定を行い、MySQLのDBを作成する。
https://aws.amazon.com/jp/getting-started/hands-on/deploy-wordpress-with-amazon-rds/2/
上記の流れで行いつつ、
・パブリック・アクセス可能を「あり」にチェック
・セキュリティグループに手順1で登録したセキュリティグループを指定
を行う。
3.MySQLクライアント設定
ローカル環境にMySQLクライアントをインストールし、MySQLコマンドが実行できるようにしておく。
$ sudo yum install -y mysql
4. DB接続
手順2で設定した、データベース名、マスターのユーザー名とパスワードを準備しておく。
コマンドで、mysql -u {マスターユーザー名} -p -h {RDSエンドポイント}
を入力してEnterを押す。
パスワード入力が求められるので、用意したパスワードを入力してEnterを押す。
$ mysql -u admin -p -h xxxxxxx.ap-northeast-1.rds.amazonaws.com
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 213
Server version: 5.7.17 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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>
DB確認
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| innodb |
| xxxxxxxxxxxxx | <-作成したDBが表示されるはず
| mysql |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.02 sec)
mysql>
5. SQLデータの登録(インポート)
mysql -u {マスターユーザー名} -p -h {RDSエンドポイント} DB名 < xxxx.sql
$ mysql -u admin -p -h xxxxxxx.ap-northeast-1.rds.amazonaws.com db_name < xxxx.sql
パスワード入力が求められるので、用意したパスワードを入力してEnterを押す。
インポートが完了したらMySQLに入ってデータの確認をする。
$ mysql -u admin -p -h xxxxxxx.ap-northeast-1.rds.amazonaws.com
:
:
mysql > use {データベース名}
use {データベース名}
Database changed
MySQL [{データベース名}]> show tables;
+------------------------------------------+
| Tables_in_xxxxxxxxxxxxx |
+------------------------------------------+
| dtb_xxxxxxxxx_role |
| dtb_xxxx_info |
:
:
+------------------------------------------+
173 rows in set (0.00 sec)
PHPローカル環境からアクセスする
//接続情報設定
$dbhost = 'RDSエンドポイント';
$dbport = '3306';
$dbname = 'データベース名';
$charset = 'utf8';
$dsn = "mysql:host={$dbhost};port={$dbport};dbname={$dbname};charset={$charset}";
$username = "ユーザー名";
$password = "パスワード";
$pdo = new PDO($dsn, $username, $password)
// -------テーブル一覧を取得する場合
$sql = "SHOW TABLES FROM ". $dbname;
$table_stmt=$pdo->prepare($sql);
$table_stmt->execute();
while($table_rec = $table_stmt->fetch(PDO::FETCH_ASSOC)){
foreach($table_rec as $key => $val){
//テーブル名表示
var_dump($val);
}
}
// -------テーブル一覧を取得する場合
// -------SQL実行する場合(select)
// pdoのインスタンス生成後から記載
$q = $pdo->query("SELECT * FROM テーブル名");
foreach ($q as $row) {
// データ表示
var_dump($row);
}
// -------SQL実行する場合(select)
$pdo = null;
今回はローカルにCakePHPの開発環境があったので、そちらを使ってRDSにアクセスしてみた。
その他
コマンドでの接続確認
$ mysql -u {ユーザー名} -p -h {エンドポイント}
Enter password: {RDS設定時の}
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 213
Server version: 5.7.17 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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>