EC2インスタンスにMariaDBをインストールする

MariaDBとはオープンソースでOracleのMySQLと高い互換性を持っているリレーショナルデータベース(RDMB)です。当記事では、AWSのEC2インスタンスにMariaDBをインストール手順を学習します。EC2インスタンスにSSH接続ができていて、基本的なLinuxコマンドが使えることが前提となります。基礎学習がまだの方は「EC2インスタンスの作成手順」と「Linuxの基礎コマンド」の記事を学習してから本記事を読み進めてください。

目次

MariaDBのインストール手順

以下のコマンドでMariaDBをインストールします。

インストール

【MariaDBのバージョン確認】
sudo dnf list | grep mariadb

【MariaDBの詳細確認】
sudo dnf info mariadb105-server

【MariaDBのパッケージをインストール】
sudo dnf install mariadb105-server -y

MariaDBの起動と初期設定

インストールが終わったらMariaDBを起動して初期設定を行います。

【MariaDBを起動】
sudo systemctl start mariadb

【初期設定の実行】
sudo mysql_secure_installation

以下のプロンプトが表示されるので、順次答えていきます。

Switch to unix_socket authentication [Y/n] 
->初回実行時なのでルートユーザのパスワード作成をします。 Y と入力してください。。

Change the root password? [Y/n]
->ルートパスワードを変更するので Y と入力して2回パスワードを入力します。

Remove anonymous users? [Y/n] 
->デフォルトのアノニマスユーザを削除するので、Y と入力。

Disallow root login remotely? [Y/n] 
->ルートユーザの外部サーバからのアクセスは許可しないので、Y と入力。

Remove test database and access to it? [Y/n] Y
->デフォルトのテストデータベースは不要なので、Y と入力。

Reload privilege tables now? [Y/n] Y
->変更を適用するので、Y と入力。

設定したパスワードは忘れないように大切に保管してください。

以下はコマンド実行後の例です。

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] Y
Enabled successfully!
Reloading privilege tables..
 ... Success!


You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] Y
New password: 
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

参考記事:https://docs.aws.amazon.com/linux/al2023/ug/ec2-lamp-amazon-linux-2023.html

MySQL コマンドライン クライアントのインストール

MariaDBのインストールが完了したので、ターミナル上でMySQLのクエリが実行できるように、MySQL小マウンドラインツールをインストールします。

【Amazon Linux 2023の例】

【コマンドラインツールのインストール】
sudo dnf install mariadb105

【バージョンの確認】
mysql --version

参考記事:https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/mysql-install-cli.html

コマンドラインツールでクエリを実行する

ここまでで、MariaDBサーバとコマンドラインクライアントのインストールが完了しました。同じEC2インスタンス上からMariaDBにアクセスしてみましょう。

【MySQLコマンドラインツールにスイッチ】
sudo mysql
->ターミナルのプロンプトが変わったのが確認できます。

【データベースの表示をしてみましょう。】
MariaDB [(none)]> SHOW DATABASES;
->以下の様な出力になればアクセス成功です。
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.002 sec)

MySQLコマンドラインツールを終了する場合はquitコマンドを使用します。これで元のEC2インスタンスのターミナルの戻ります。

MariaDB [(none)]> quit

パワーユーザを作成してみよう

前述の通り、ルートユーザは外部サーバからのアクセスをできないように設定しました。そこで、外部サーバからアクセスできる一般ユーザを作成しましょう。再度、MySQLのコマンドラインツールを使って作成してみてください。

ここでは権限の強いパワーユーザ(ユーザ名=poweruser)を作成してみます。

【パワーユーザの作成】
MariaDB [(none)]> CREATE USER 'poweruser'@'%' IDENTIFIED BY 'パスワード';

【全権限の付与】
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'poweruser'@'%' WITH GRANT OPTION;

【設定の反映】
FLUSH PRIVILEGES;

ここでのパワーユーザは練習用のユーザです。本番用のユーザに与える権限は必要に応じてせいげんするようにしましょう。

セキュリティーグループの作成

外部からMariaDBをインストールしているEC2インスタンスにアクセスするためには、EC2インスタンスにセキュリティーグループの設定が必要です。「EC2セキュリティーグループの作成」の記事を参考にMariaDB用のセキュリティーグループを作成しておきましょう。

まとめ

今回はMariaDBをEC2インスタンスにインストール留守手順を学習しました。MariaDBのインストール後の設定時にルートユーザのパスワードの設定をしましたね。DBのデータは大切な資産です。パスワードが流出しないようにパスワードは注意を払って管理する必要があることを覚えておきましょう。コマンドラインツールは、ルートユーザでしか行えない管理などもできるので併せてインストールしておきましょう。

次回は「phpMyAdminのインストールと設定、そしてテーマを変えてみよう」を学習していきます。

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

コメント

コメントする

目次