0
0

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本番環境でDBを作ってみた

Posted at

はじめに

Rails6のようなフレームワークには必ずDBが必要となってきます。そのためにEC2上でもDBを作成し、保存できるようにしてきました!
備忘録として残したいと思います。

DBの用意

私は、日頃からDBは「MySQL」を使用しています。
また今回のデプロイでは「Amazon Linux2」を使用しているので「MariaDB」を使用します。

ちなみに、MariaDBは、MySQLから派生したDBらしく基本的に同様のものと考えていいらしですよ!

ターミナルにて
[ec2-user@ip-ご自身のIP ~]$ sudo yum -y install mysql56-server mysql56-devel mysql56 mariadb-server mysql-devel

起動できたか確認します。

[ec2-user@ip-ご自身のIP ~]$ sudo systemctl start 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 #runningとなっていればおkです。
  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)

ちなみに、「systemctl」コマンドは、Amazon LinuxやCentOSに含まれているもので、インストールしたソフトウェアの起動を一括して行えるツールです!

DBのrootパスワードの設定

MariaDBにはデフォルトで「root」というユーザーでアクセスできますが、パスワードが設定されていません。
そこで、パスワードを設定する必要があります。

[ec2-user@ip-ご自身のIP ~]$ sudo /usr/bin/mysql_secure_installation
この先、
①yes
②New passwordでパスワード入力
③Re-enter new passwordで再度同じパスワードを入力

DBへ接続します。

[ec2-user@ip-ご自身のIP ~]$ mysql -u root -p
↓が出れば成功です。
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)]> 

最後に

ローカル環境でもDBは必要だったように、別の場所へデプロイしたら、都度DBは必要ということを理解できました。
AWSやherokuを触るまでは、ローカルでの環境がそのまま反映されて、何もしなくていい物だと思っていました。
こうやって手を動かしてやっていくうちに、理解が深まっていくことは嬉しいですね!

ここまで読んでいただきありがとうございました!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?