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

EC2にRailsアプリをデプロイ③ ~本番環境にデータベースを用意する~

Posted at

はじめに

これは前回の続きです。
データベースを本番環境に用意してあげてデータを保存できるようにします。

私はローカル環境でMySQLを利用してアプリケーションの作成をしました。

MariaDBをインストール

MariaDBとはMySQLの派生として開発されているオープンソースソフトウェアです。今回使用しているAmazon Linux 2ではこのMariaDBを使用することとなっており、MySQLと互換性があります。

下記のコマンドでインストールします。
Amazon Linux 2を利用している場合yumコマンドでインストールできます。

ターミナル
[ec2-user@ip-172-31-25-189 ~]$ sudo yum -y install  mariadb-server mysql-devel

systemctlコマンドというコマンドを使ってインストールしたソフトウェアを起動します。

ターミナル
[ec2-user@ip-172-31-25-189 ~]$ sudo systemctl start mariadb

起動しているかの確認を下記のコマンドで行います。

ターミナル
[ec2-user@ip-172-31-25-189 ~]$ sudo systemctl status mariadb

● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
   Active: active (running) since 土 2020-02-29 07:00:11 UTC; 7s ago
  Process: 5993 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
  Process: 5957 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)

緑色で「acrtive(running)」と表示されていれば起動できています。

データベースのrootパスワードの設定をします。
yumでインストールしたMariaDBはデフォルトでrootというユーザーでアクセスできるようになっています。しかしパスワードが設定されていないので自分で設定する必要があります。

このパスワードは後ほど使うのでメモに残しておいてください。
私はメモを残していなくて大変苦労しました!!

下記のコマンドを実行しパスワードを設定します。

ターミナル
[ec2-user@ip-172-31-25-189 ~]$ sudo /usr/bin/mysql_secure_installation

実行したあと
①Enter current password for root (enter for none): 」と表示されたらEnterキーを押します。

②「Set root password? [Y/n]」と表示されたら「Y」を入力してEnterキーを押す

③「New password:」と表示されたら自身で決めたパスワードを入力
※入力しても特に変化ありませんが文字はちゃんと入力されています

④「Re-enter new password:」と表示されたら、同じパスワードを入力
※入力しても特に変化ありませんが文字はちゃんと入力されています

⑤ ①〜④が完了したあとは細かい質問を4つほどされるので全て「Y」を入力してエンターしてください。

最後にちゃんとパスワードが使えるか確認をしてみます。下記コマンドを入力

ターミナル
[ec2-user@ip-172-31-25-189 ~]$ mysql -u root -p

「Enter password:」と表示されたら先ほどのパスワードを入力してエンターを押します。

下記ように表示されればしっかり設定できています。

ターミナル
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 142
Server version: 5.5.64-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)]> 

「exit」を入力したら閉じられます。
これでデータベースを用意できました。
次回はいよいよデプロイしていきます。

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?