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?

Amazon Linux 2はデフォルトでMariaDBが使えるのか?疑問を解消するために技術検証してみた

Last updated at Posted at 2025-01-04

はじめに

今回は、Amazon Linux 2 を新規構築した際に、MariaDB がデフォルトで導入されているかどうかについて技術検証を行います。

なお、Amazon Linux 2 のサポートは2026年6月30日に終了予定です。そのため、今後は Amazon Linux 2023 を積極的に活用することが推奨されます。

ただし、現時点でも Amazon Linux 2 を利用する場面は少なくないため、今回の検証結果を自分の備忘録として記録し、共有することにしました。

書こうと思ったきっかけ

最近、Amazon Linux 2 を使用している方から、「デフォルトで MariaDB がインストールされている」という声を耳にしました。

一方で、私の認識では、MariaDB の本体であるサーバー(mariadb-server)はインストールされておらず、そのままでは利用できないと考えていました。

この認識の違いがどちらに起因するのかを明らかにするため、技術検証を実施することにしました。

なお、この記事は2025年1月4日時点の情報を基に作成しています。将来的な変更に備えて、最新の情報を適宜確認することをお勧めします。

結論:MariaDB ライブラリ(mariadb-libs)はインストールされている

検証の結果、MariaDB サーバー(mariadb-server)自体はインストールされておらず、そのため MariaDB サービス(mariadb.service)を起動することができない状態でした。

一方で、mariadb-libs(MariaDB のクライアントライブラリ)はデフォルトでインストールされていることが確認されました。

mariadb-libs がインストールされる理由

mariadb-libs が初期状態でシステムに存在している背景には、以下の2つの理由が考えられます。

1. 依存関係によるインストール

他のパッケージ(例: MySQL コマンドラインクライアントや PHP/MySQL 連携モジュール)をインストールした際に、mariadb-libs依存関係として自動的にインストール される場合があります。

2. デフォルトパッケージとしてのインストール

Amazon Linux 2 などの一部のディストリビューションでは、MariaDB 関連の基本ライブラリが システムの一部としてデフォルトでインストール される設定になっています。

これらの理由により、mariadb-libs は Amazon Linux 2 のような環境では初期状態でインストールされていることが確認されています(2025年1月4日時点)。

MariaDBサーバーとライブラリの違い

パッケージ 役割
mariadb-server MariaDBデータベースサーバー本体。データベースを提供し、SQLクエリを処理する。
mariadb-libs クライアントライブラリ。アプリケーションが MariaDB サーバーに接続するために使用される。

Amazon Linux 2を構築して検証

今回の検証は、以下のTerraformコードを使用して、CloudShell上でシンプルな構成のEC2インスタンスを1台構築する方法を採用しました。

main.tf
provider "aws" {
  region = "ap-northeast-1" # Tokyo region
}

# VPC作成
resource "aws_vpc" "main_vpc" {
  cidr_block           = "10.0.0.0/16"
  enable_dns_support   = true
  enable_dns_hostnames = true

  tags = {
    Name = "terraform-main-vpc"
  }
}

# サブネット作成 (パブリックサブネット)
resource "aws_subnet" "public_subnet" {
  vpc_id                  = aws_vpc.main_vpc.id
  cidr_block              = "10.0.1.0/24"
  map_public_ip_on_launch = true

  tags = {
    Name = "terraform-public-subnet"
  }
}

# インターネットゲートウェイ作成
resource "aws_internet_gateway" "main_igw" {
  vpc_id = aws_vpc.main_vpc.id

  tags = {
    Name = "terraform-main-igw"
  }
}

# ルートテーブル作成
resource "aws_route_table" "public_route_table" {
  vpc_id = aws_vpc.main_vpc.id

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.main_igw.id
  }

  tags = {
    Name = "terraform-public-route-table"
  }
}

# サブネットとルートテーブルの関連付け
resource "aws_route_table_association" "public_association" {
  subnet_id      = aws_subnet.public_subnet.id
  route_table_id = aws_route_table.public_route_table.id
}

# セキュリティグループ作成
resource "aws_security_group" "public_sg" {
  vpc_id = aws_vpc.main_vpc.id

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"] # HTTPアクセスを許可
  }

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"] # SSHアクセスを許可
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"] # すべてのアウトバウンドを許可
  }

  tags = {
    Name = "terraform-public-sg"
  }
}

# EC2インスタンス作成
resource "aws_instance" "apache_ec2" {
  ami                         = "ami-0b6fe957a0eb4c1b9" # Amazon Linux 2 AMI (Tokyo region)
  instance_type               = "t2.micro"
  subnet_id                   = aws_subnet.public_subnet.id
  security_groups             = [aws_security_group.public_sg.id] # 修正: IDを参照
  associate_public_ip_address = true # パブリックIPv4アドレスを有効化

  user_data = <<-EOF
    #!/bin/bash
    yum update -y
    yum install -y httpd
    systemctl start httpd
    systemctl enable httpd
    echo "<html><body><h1>Terraformを使い、検証環境を最低限の構成でデプロイしています</h1></body></html>" > /var/www/html/index.html
  EOF

  depends_on = [aws_security_group.public_sg] # 修正: セキュリティグループに依存

  tags = {
    Name = "terraform-apache-ec2"
  }
}

内容については、過去の記事でも詳しくご紹介していますので、興味のある方はぜひ確認してみてください。

実際にコマンドで確認

ここでは、パブリックサブネットに配置され、グローバルIPアドレスを持つEC2インスタンスに対して、AWSのEC2 Instance Connectサービスを利用してSSH接続を行います。

Screenshot 2025-01-04 at 9.11.42.png

EC2 Instance Connectの使い方についても、過去の記事で解説していますので、参考にしてみてください。

MariaDBの状態確認

MariaDBがインストールされている場合、以下のコマンドでサービスの状態を確認できます。

sudo systemctl status mariadb

しかし、実際にコマンドを実行した結果は以下の通りです。

Unit mariadb.service could not be found.

この結果から、MariaDBサービス(mariadb.service)が存在しないため、MariaDBサーバーが導入されていない可能性が高いと考えました。

Screenshot 2025-01-04 at 9.12.18.png

インストール状況の確認

現在インストールされている MariaDB 関連のパッケージを確認するためのコマンドを実行してみました。

sudo yum list installed | grep mariadb

結果を見ると、mariadb-libs.x86_64がインストールされているものの、MariaDBサーバー本体は存在しないことがわかります。

Screenshot 2025-01-04 at 9.13.34.png

この結果から、mariadb-libs(MariaDBのクライアントライブラリ)のみがインストールされており、MariaDBサーバー本体はデフォルトで含まれていないことが確認できました。

MariaDBを試しにインストールしてみた

MariaDBを利用するには、MariaDBサーバーをインストールする必要があります。以下の手順でインストールと設定を行います。

インストール手順

MariaDBサーバーをインストールします。

sudo yum install mariadb-server

サーバーをインストール後、以下のコマンドでサービスを起動し、自動起動を有効化します。

sudo systemctl start mariadb
sudo systemctl enable mariadb

サービスの状態を確認して、正常に動作していることを確認します。

sudo systemctl status mariadb

以下のように、サービスがアクティブであることを確認しました。

Screenshot 2025-01-04 at 9.15.09.png

MariaDBに接続する

インストールが完了したら、MariaDBクライアント(mysqlコマンド)を使用して接続します。

mysql -u root -p

パスワードが設定されていない場合、-pオプションを省略して接続することも可能です。

以下は接続時の画面例です。

[ec2-user@ip-10-0-1-109 ~]$ mysql -u root
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-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)]> 

初期設定(任意)

MariaDBの初期設定を行う場合は、以下のコマンドを使用して設定を進めてください。

sudo mysql_secure_installation

これにより、rootパスワードの設定や不要なデフォルト設定の削除が可能です。

まとめ

今回の技術検証では、自分の気になっていた点について仮説を立て、それを実際に検証してみました。結果的に仮説が正しいと確認できたことは、大きな収穫でした。

一方で、デフォルト状態では MariaDB のライブラリ (mariadb-libs) が導入されているため、MySQL など他のデータベースを利用したい場合は、不要なライブラリを削除してから導入することをおすすめします。

今回の検証内容は簡単なものですが、この情報が誰かの技術的な参考や助けになれば幸いです!

参考記事

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?