【TOPへ戻る】
 Part1:RDS作成前の事前準備
 Part2:RDS MySQLの作成
 Part3:WebサーバーからRDS MySQLに接続
Part3:WebサーバーからRDS MySQLに接続
ではいよいよ作成したWebサーバーからRDS MySQLに接続していきます。
まず最初にWebサーバーにMySQLクライアントをインストールします。
【 MySQLクライアントとは ?】
MySQLサーバ対するクライアントのことをいう。MySQLサーバにクライアントとして接続し、データベース上のデータにアクセスしてデータを抽出したり、データベースを構築したりすることができる。
### パッケージをインストールするため[root]ユーザーにスイッチ
[ec2-user: /]$ sudo su -
Last login: Fri Jan 20 02:13:42 UTC 2023 on pts/0
### パッケージのアップデート
[root:/]# yum update
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
No packages marked for update
### 今回は、このパッケージをインストール
[root:/]# yum info mariadb
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Installed Packages
Name        : mariadb
Arch        : x86_64
Epoch       : 1
Version     : 5.5.68
Release     : 1.amzn2
Size        : 49 M
Repo        : installed
From repo   : amzn2-core
Summary     : A community developed branch of MySQL
URL         : http://mariadb.org
License     : GPLv2 with exceptions and LGPLv2 and BSD
Description : MariaDB is a community developed branch of MySQL.
            : MariaDB is a multi-user, multi-threaded SQL database server.
            : It is a client/server implementation consisting of a server daemon (mysqld)
            : and many different client programs and libraries. The base package
            : contains the standard MariaDB/MySQL client programs and generic MySQL files.
### MySQLクライアントをインストール
[root:/]# yum -y install mysql
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Resolving Dependencies
--> Running transaction check
---> Package mariadb.x86_64 1:5.5.68-1.amzn2 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
===========================================================================================================================
 Package                   Arch                     Version                             Repository                    Size
===========================================================================================================================
Installing:
 mariadb                   x86_64                   1:5.5.68-1.amzn2                    amzn2-core                   8.8 M
Transaction Summary
===========================================================================================================================
Install  1 Package
Total download size: 8.8 M
Installed size: 49 M
Downloading packages:
mariadb-5.5.68-1.amzn2.x86_64.rpm                                                                   | 8.8 MB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:mariadb-5.5.68-1.amzn2.x86_64                                                                         1/1
  Verifying  : 1:mariadb-5.5.68-1.amzn2.x86_64                                                                         1/1
Installed:
  mariadb.x86_64 1:5.5.68-1.amzn2
Complete!
これでMySQLクライアントのインストールは、完了です。
続いて作成したRDS MySQL(サーバー)に接続していきます。
RDSコンソールから、作成したRDSのエンドポイントをコピーします。
【AWSのエンドポイントとは?】
AWSのサービスに接続するために必要なURLを指します。

取得したエンドポイントを使い、WebサーバーからRDS MySQL(サーバー)に接続します。
### RDS MySQL Server に接続
### オプション[ -u = ユーザーID / -p = パスワード / -h 接続するホスト(エンドポイント) ]
[root:/]# mysql -u admin -p -h mysql-db.chwgidx2g0zh.ap-northeast-1.rds.amazonaws.com
Enter password:
### ログインが成功した場合
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 424
Server version: 8.0.31 Source distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
### ログインが失敗した場合
ERROR 1045 (28000): Access denied for user 'admin'@'10.0.1.10' (using password: YES)
【ログインがうまくいかない場合】
- 作成したRDSのステータスは、利用可能状態ですか?
- セキュリティグループやVPCは、正しく設定されていますか?
 - セキュリティグループ:Webサーバーからのアクセスを有効にしているか確認
 - VPC:Webサーバーと同じVPCにいるか確認
- RDSを作成する際に指定したユーザーIDでログインしていますか?
 - 設定画面からユーザーIDを確認しましょう。
- 入力したパスワードは、正しいですか?
 - 忘れてしまった場合、パスワードを変更することができます。【リンク】
上記で解決しなかった場合は、下記リンクを参照してください。
Amazon RDS DB インスタンスに接続する際の問題を解決するにはどうすればよいですか?
これでWebサーバーからRDS MySQL(サーバー)への接続は完了です。
ちょっと勉強がてらに、軽く遊んで終わります。
### データベース一覧表示
### RDS作成時に指定したデータベース[test_table]があることが確認できる。
MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test_table         |
+--------------------+
5 rows in set (0.03 sec)
### データベース[test_table]へ変更
MySQL [(none)]> use test_table;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
### テーブル一覧表示
MySQL [test_table]> show tables;
+----------------------+
| Tables_in_test_table |
+----------------------+
| m_users              |
+----------------------+
1 row in set (0.00 sec)
### テーブル[m_users]定義の確認
MySQL [test_table]> desc m_users;
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int          | NO   | PRI | NULL    | auto_increment |
| user_name    | varchar(100) | NO   |     | NULL    |                |
| mail_address | varchar(200) | NO   |     | NULL    |                |
| password     | varchar(100) | NO   |     | NULL    |                |
| created      | datetime     | YES  |     | NULL    |                |
| modified     | datetime     | YES  |     | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+
6 rows in set (0.01 sec)
### テーブル[m_users]の中を表示(検索)
MySQL [test_table]> SELECT * FROM m_users;
+----+-----------+------------------+----------+---------------------+---------------------+
| id | user_name | mail_address     | password | created             | modified            |
+----+-----------+------------------+----------+---------------------+---------------------+
|  1 | Qii Taro  | qiitaro@hoge.com | 123123   | 2023-01-19 13:12:25 | 2023-01-19 13:12:25 |
+----+-----------+------------------+----------+---------------------+---------------------+
1 row in set (0.01 sec)
### テーブル[m_users]にデータを挿入
MySQL [test_table]> INSERT INTO m_users (user_name, mail_address, password) VALUES ('Takeshi', 'takenoko@hoge.com', 123456);
Query OK, 1 row affected (0.02 sec)
### テーブル[m_users]の中を表示(検索)
MySQL [test_table]> SELECT * FROM m_users;
+----+-----------+-------------------+----------+---------------------+---------------------+
| id | user_name | mail_address      | password | created             | modified            |
+----+-----------+-------------------+----------+---------------------+---------------------+
|  1 | Qii Taro  | qiitaro@hoge.com  | 123123   | 2023-01-19 13:12:25 | 2023-01-19 13:12:25 |
|  2 | Takeshi   | takenoko@hoge.com | 123456   | NULL                | NULL                |
+----+-----------+-------------------+----------+---------------------+---------------------+
2 rows in set (0.01 sec)
### テーブル[m_users]のカラム(行)[id]の[2]番を削除
MySQL [test_table]> DELETE FROM m_users WHERE id = 2;
Query OK, 1 row affected (0.00 sec)
### テーブル[m_users]の中を表示(検索)
MySQL [test_table]> SELECT * FROM m_users;
+----+-----------+------------------+----------+---------------------+---------------------+
| id | user_name | mail_address     | password | created             | modified            |
+----+-----------+------------------+----------+---------------------+---------------------+
|  1 | Qii Taro  | qiitaro@hoge.com | 123123   | 2023-01-19 13:12:25 | 2023-01-19 13:12:25 |
+----+-----------+------------------+----------+---------------------+---------------------+
1 row in set (0.00 sec)
MySQL [test_table]>
MySQL は、RDMSであってデータベース自体ではない。
RDMSがデータベースを管理している。
データベースの中にテーブルは存在し、その中にデータが入っている。





