0
4

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 3 years have passed since last update.

AWS Cloud9でMariaDBを使用する方法

Last updated at Posted at 2021-08-20
1 / 19

#概要
AWS Cloud9の環境にてMariaDBの初期設定、データベースの作成、テーブルの作成、カラムの作成という一連の作業ができることの確認をします。
今回使用するMariaDBは、AWS Cloud9に最初からインストールされているものを使用います。


#yum アップデート

$ sudo yum -y update

#MySQLのバージョンを確認

$ mysql --version
mysql  Ver 15.1 Distrib 10.2.38-MariaDB, for Linux (x86_64) using  EditLine wrapper

#MariaDB-Server をインストールする
sudo systemctl start mariadb とかを使えるようにするためです

$ sudo yum install mariadb-server

#/etc/my.cnfの編集
文字コードにUTF-8を利用するための設定を行なってください。

$ sudo vim /etc/my.cnf
[mysqld]
...
character-set-server=utf8 #mysqldセクションに追記

[client]
default-character-set=utf8 #新規にclientセクションと設定を追記

#MySQLのステータスを確認(停止状態)

$ sudo systemctl status mariadb
● mariadb.service - MariaDB 10.2 database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/mariadb.service.d
           └─tokudb.conf
   Active: inactive (dead)

#MySQLの起動

sudo systemctl start mariadb

#MySQLのステータスを確認(起動状態)

$ sudo systemctl status mariadb
● mariadb.service - MariaDB 10.2 database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/mariadb.service.d
           └─tokudb.conf
   Active: active (running) since Fri 2021-08-20 02:45:35 UTC; 45s ago
  Process: 9423 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, status=0/SUCCESS)
  Process: 9220 ExecStartPre=/usr/libexec/mysql-prepare-db-dir %n (code=exited, status=0/SUCCESS)
  Process: 9165 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)
 Main PID: 9350 (mysqld)
   Status: "Taking your SQL requests now..."
    Tasks: 38
   Memory: 78.3M
   CGroup: /system.slice/mariadb.service
           └─9350 /usr/libexec/mysqld --basedir=/usr
・
・

#MySQLへの接続
MySQLへは下記のコマンドでログインすることができます。
※初期パスワードは何も入力しないでEnterを押してください。

$ mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.2.38-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

#MySQLで使用されている文字コードの確認
utf8が利用されていることを確認してください。

MariaDB [(none)]> SHOW VARIABLES LIKE "chara%";
+--------------------------+------------------------------+
| Variable_name            | Value                        |
+--------------------------+------------------------------+
| character_set_client     | utf8                         |
| character_set_connection | utf8                         |
| character_set_database   | utf8                         |
| character_set_filesystem | binary                       |
| character_set_results    | utf8                         |
| character_set_server     | utf8                         |
| character_set_system     | utf8                         |
| character_sets_dir       | /usr/share/mariadb/charsets/ |
+--------------------------+------------------------------+
8 rows in set (0.00 sec)

#データベース作成
今回は「sample」という名前でデータベースを作成しています。

MariaDB [(none)]> CREATE DATABASE sample;
Query OK, 1 row affected (0.01 sec)

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sample             |
| test               |
+--------------------+
5 rows in set (0.00 sec)

#テーブルの作成
今回は「todos」という名前のテーブルを作成しています。

MariaDB [(none)]> USE sample;
Database changed

MariaDB [sample]> CREATE TABLE sample.todos (
    -> id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
    -> done BOOLEAN,
    -> task TEXT
    -> );
Query OK, 0 rows affected (0.01 sec)

MariaDB [sample]> SHOW TABLES;
+------------------+
| Tables_in_sample |
+------------------+
| todos            |
+------------------+
1 row in set (0.00 sec)

MariaDB [sample]> DESCRIBE todos;
+-------+------------+------+-----+---------+----------------+
| Field | Type       | Null | Key | Default | Extra          |
+-------+------------+------+-----+---------+----------------+
| id    | int(11)    | NO   | PRI | NULL    | auto_increment |
| done  | tinyint(1) | YES  |     | NULL    |                |
| task  | text       | YES  |     | NULL    |                |
+-------+------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

#カラムの挿入

MariaDB [sample]> INSERT INTO todos(done, task) VALUES(false, '1');
Query OK, 1 row affected (0.00 sec)

MariaDB [sample]> INSERT INTO todos(done, task) VALUES(false, '2');
Query OK, 1 row affected (0.00 sec)

MariaDB [sample]> INSERT INTO todos(done, task) VALUES(false, '3');
Query OK, 1 row affected (0.00 sec)

MariaDB [sample]> SELECT * FROM sample.todos;
+----+------+------+
| id | done | task |
+----+------+------+
|  1 |    0 | 1    |
|  2 |    0 | 2    |
|  3 |    0 | 3    |
+----+------+------+
3 rows in set (0.00 sec)

#カラムの更新

MariaDB [sample]> UPDATE todos SET done=true, task='11' WHERE id=1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [sample]> SELECT * FROM sample.todos;
+----+------+------+
| id | done | task |
+----+------+------+
|  1 |    1 | 11   |
|  2 |    0 | 2    |
|  3 |    0 | 3    |
+----+------+------+
3 rows in set (0.00 sec)

#カラムの削除

MariaDB [sample]> DELETE FROM sample.todos WHERE id=1;
Query OK, 1 row affected (0.01 sec)

MariaDB [sample]> SELECT * FROM sample.todos;
+----+------+------+
| id | done | task |
+----+------+------+
|  2 |    0 | 2    |
|  3 |    0 | 3    |
+----+------+------+
2 rows in set (0.00 sec)

#カラムの削除(全件)

MariaDB [sample]> DROP TABLE todos;
Query OK, 0 rows affected (0.01 sec)

MariaDB [sample]> SHOW TABLES;
Empty set (0.00 sec)

#データベースの削除

MariaDB [sample]> DROP DATABASE sample;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

#まとめ
Cloud9で開発する場合に、便利かもしれません。
開発環境を自分で構築するのは少し手間がかかってしまうので、これは楽にデータベースを使えるかもしれませんね。

0
4
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
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?