【AWS】通过Web服务器连接RDS MySQL(第三部分)

【返回顶部】
第一部分:创建RDS之前的准备工作
第二部分:创建RDS MySQL
第三部分:从网络服务器连接到RDS MySQL

第三部分:连接到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。
image.png
### 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)
image.png

通过这样,从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相关信息请点击此处(推荐)】

【MySQL】什么是数据库?解释结构、机制和与表的区别
【初学者】在Linux(CentOS)上安装和启动MySQL!
【即使是初学者也很简单】使用MySQL命令!用SQL操作数据库和表!
MySQL 8.0 参考手册
UNIX/Linux 房间中如何使用mysql命令
常用的MySQL命令和语法集
在MySQL命令行中取消输入。

MySQL是一种关系数据库管理系统(RDMS),而不是数据库本身。 RDMS负责管理数据库。 数据库中包含表,而表中存储数据。
广告
将在 10 秒后关闭
bannerAds