0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Amazon Lightsail】LAMP環境構築チュートリアル②(DB編)

Last updated at Posted at 2022-09-11

構築作業

前回の①では、Webサーバの構築まで完了しました。ここからはDBを構築をしていきたいと思います。

1. MySQL接続

  • Amazon Lightsailのホーム画面にアクセスし、「LAMP_TEST」インスタンスにSSH接続します。
    スクリーンショット 2022-09-11 20.05.42.png

  • SSHクライアントのWindow上で下記コマンドを実行します。

$ cat $HOME/bitnami_application_password
$ (パスワードが表示されます)
$ mysql -u ユーザー -p'パスワード'

2. DB・テーブル作成

  • SSHクライアントのWindow上で下記のSQLを実行します。
dump_lamp_test.sql
BEGIN;

DROP DATABASE IF EXISTS `lamp_test`;

CREATE DATABASE lamp_test;

USE lamp_test;

DROP TABLE IF EXISTS `continents`;

CREATE TABLE `continents` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

INSERT INTO `continents` VALUES (1,'Asia'),(2,'Africa'),(3,'North America'),(4,'Europe'),(5,'South America'),(6,'Oceania'),(7,'Antarctic');

DROP TABLE IF EXISTS `countries`;

CREATE TABLE `countries` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `parent_id` int(11) NOT NULL,
  `name` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;

INSERT INTO `countries` VALUES (1,1,'Japan'),(2,1,'China'),(3,1,'Vietnam'),(4,1,'Korea'),(5,2,'Kenya'),(6,2,'Tanzania'),(7,3,'America'),(8,3,'Canada'),(9,4,'Germany'),(10,4,'England'),(11,5,'Brazil'),(12,5,'Argentina'),(13,6,'Australia');

COMMIT;

SELECT countries.*, continents.name FROM countries INNER JOIN continents ON countries.parent_id = continents.id;
  • 下記の通りテーブルの中身が表示されればOKです。
MariaDB [lamp_test]> SELECT countries.*, continents.name FROM countries INNER JOIN continents ON countries.parent_id = continents.id;
+----+-----------+-----------+---------------+
| id | parent_id | name      | name          |
+----+-----------+-----------+---------------+
|  1 |         1 | Japan     | Asia          |
|  2 |         1 | China     | Asia          |
|  3 |         1 | Vietnam   | Asia          |
|  4 |         1 | Korea     | Asia          |
|  5 |         2 | Kenya     | Africa        |
|  6 |         2 | Tanzania  | Africa        |
|  7 |         3 | America   | North America |
|  8 |         3 | Canada    | North America |
|  9 |         4 | Germany   | Europe        |
| 10 |         4 | England   | Europe        |
| 11 |         5 | Brazil    | South America |
| 12 |         5 | Argentina | South America |
| 13 |         6 | Australia | Oceania       |
+----+-----------+-----------+---------------+
13 rows in set (0.000 sec)
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?