#author("2025-03-15T21:37:25+09:00;2025-03-15T19:52:00+09:00","default:tanopro","tanopro")
#author("2025-03-15T21:41:52+09:00;2025-03-15T19:52:00+09:00","default:tanopro","tanopro")
#html{{
<h1 style="color: red; background-color: #FFFFCC; padding: 20px;">
MariaDB
</h1>

<div class="alert alert-warning" role="alert">
MySQL派生のデータベース
</div>
}}

//----------------------------------------
#contents
//----------------------------------------

//----------------------------------------
// 用語の定義
* MariaDBとは? [#definition]

- MariaDB - Google 検索
https://www.google.com/search?q=MariaDB

- MariaDB - Wikipedia
https://ja.wikipedia.org/wiki/MariaDB
>MariaDBは、MySQL派生として開発されている、オープンソースの関係データベース管理システム (RDBMS) である。

** 公式サイト [#a06ada10]

- MariaDB Enterprise Open Source Database | MariaDB
https://mariadb.com/

- MariaDB · GitHub
https://github.com/mariadb-corporation

** 最新バージョンの確認 [#x395de3c]
ダウンロードページを見ると、現時点で提供されている最新バージョンを確認することができます。

- Download MariaDB Products & Tools | MariaDB
https://mariadb.com/downloads/


//----------------------------------------
* インストール [#dc35e8d7]
Ubuntu 24.04 LTSにMariaDBをインストールするメモ。

** aptコマンドでインストールできるバージョンの確認方法 [#za11e8cc]

Ubuntuの公式リポジトリーからaptコマンドでインストールできるMariaDBのバージョンを調べるには、以下の手順を実行してください。

*** 方法 1: apt-cache policy を使用する [#qbc090a5]
 apt-cache policy mariadb-server
このコマンドを実行すると、インストール可能なMariaDBのバージョンが表示されます。

*** 方法 2: apt list を使用する [#k5b95f21]
 apt list -a mariadb-server
これにより、リポジトリーに登録されているMariaDBのバージョン一覧が表示されます。

*** 方法 3: apt show を使用する [#u75b33ed]
 apt show mariadb-server | grep Version
この方法では、利用可能なバージョンを簡潔に表示できます。

*** 方法 4: apt-cache madison を使用する [#y95b89f8]
 apt-cache madison mariadb-server
この方法では、リポジトリーにあるバージョンとそれが提供されているソースを確認できます。

*** 注意点 [#x85f9624]
- MariaDBのバージョンは、Ubuntuのバージョンによって異なります。最新のLTSリリースでは比較的新しいバージョンが提供されることが多いです。
- より新しいバージョンを利用したい場合は、MariaDB公式のリポジトリーを追加してインストールすることも可能です。


//----------------------------------------
* Ubuntu 24.04 に MariaDB をインストールする方法 [#g01712b0]

Ubuntu の公式リポジトリーからインストールする方法と、MariaDB の公式リポジトリーから最新バージョンをインストールする方法の 2 通りを紹介します。

----

** ''方法 1: Ubuntu の公式リポジトリーからインストール'' [#v362197c]

*** ''1. パッケージリストを更新'' [#ga783045]
まずは、システムのパッケージリストを最新の状態に更新します。
 sudo apt update && sudo apt upgrade -y

*** ''2. MariaDB をインストール'' [#hc07036c]
以下のコマンドで MariaDB をインストールします。
 sudo apt install -y mariadb-server mariadb-client

インストールされたMariaDBのバージョンを確認します。
 mysql --version
↓
 mysql  Ver 15.1 Distrib 10.11.8-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper

- MariaDB は、MySQL との後方互換性を維持するため、MySQL と同じコマンドを使用できます。
つまり、MySQLと同じように、MySQLのコマンドで操作できます。

*** ''3. MariaDB の起動と自動起動設定'' [#g31f01eb]
MariaDB が正常にインストールされたか確認し、サービスを起動します。
 sudo systemctl start mariadb
 sudo systemctl enable mariadb

*** ''4. セキュリティ設定の実施'' [#cdb114f3]
MariaDB を安全に使用するために、初期設定を行います。
 sudo mysql_secure_installation

この設定では、以下のようなオプションを設定できます。
- ''root パスワードの設定''(Ubuntu の公式リポジトリー版では root は `unix_socket` 認証がデフォルト)
- ''匿名ユーザーの削除''
- ''リモート root ログインの無効化''
- ''テストデータベースの削除''
- ''権限の再読み込み''

すべての質問に「Y」を選択すると、安全な設定になります。

*** ''5. MariaDB の動作確認'' [#lc2e5b1e]
MariaDB が正常に動作しているか確認します。
 sudo systemctl status mariadb

また、MariaDB に接続できるか確認します。
 sudo mysql -u root -p

パスワードを入力後、MariaDB のコンソールが開けば成功です。

----

** ''方法 2: MariaDB の公式リポジトリーから最新バージョンをインストール'' [#hb8c2edf]
Ubuntu 公式リポジトリーでは MariaDB のバージョンがやや古い場合があるため、最新のバージョンを使いたい場合は、MariaDB の公式リポジトリーを追加してインストールするのがおすすめです。

*** ''1. 公式リポジトリーを追加'' [#v5fcf5f9]
まず、MariaDB の公式リポジトリーを追加するために、MariaDB の公式サイトからリポジトリー設定スクリプトを取得します。
 sudo apt install -y curl software-properties-common
 curl -LsS https://r.mariadb.com/downloads/mariadb_repo_setup | sudo bash
このコマンドを実行すると、MariaDB の最新リポジトリーが自動で追加されます。

*** ''2. 最新の MariaDB をインストール'' [#p8ad27b5]
 sudo apt update
 sudo apt install -y mariadb-server mariadb-client

*** ''3. インストール後のセットアップ'' [#q2095920]
あとは、上記の「方法 1」の手順 ''3'' 以降(起動、セキュリティ設定、動作確認)を実行してください。

----

** ''どちらの方法を選ぶべきか?'' [#w902e62b]
- ''安定したバージョンで問題ない場合'' → Ubuntu の公式リポジトリー(方法 1)
- ''最新の機能やバージョンを使いたい場合'' → MariaDB 公式リポジトリー(方法 2)

どちらの方法でもインストール後の操作はほぼ同じなので、用途に応じて選択してください。


//----------------------------------------
* セキュリティー設定ツール「mysql_secure_installation」の使い方 [#i0ae71a5]

- mysql_secure_installation - Google 検索
https://www.google.com/search?q=mysql_secure_installation

- mysql_secure_installation - MariaDB Knowledge Base
https://mariadb.com/kb/ja/mysql_secure_installation/

- MySQL :: MySQL 8.0 リファレンスマニュアル :: 4.4.2 mysql_secure_installation — MySQL インストールのセキュリティー改善
https://dev.mysql.com/doc/refman/8.0/ja/mysql-secure-installation.html

- Ubuntu 24.04 : MariaDB 10.11 インストールと設定 : Server World
https://www.server-world.info/query?os=Ubuntu_24.04&p=mariadb


** mysql_secure_installation とは? [#e3bf7300]
MariaDB の初期セキュリティ設定を行うためのスクリプトで、以下の設定を順に行います。​

+ 現在の root パスワードの入力: 新規インストール直後でパスワードが未設定の場合は、そのまま Enter キーを押します。​
+ unix_socket 認証への切り替え: unix_socket 認証を使用すると、システムのユーザー認証と連携し、追加のパスワード入力なしで MariaDB にアクセスできます。必要に応じて「Y」または「n」を選択してください。​
+ root パスワードの設定: unix_socket 認証を使用しない場合、root ユーザーにパスワードを設定することが推奨されます。​
+ 匿名ユーザーの削除: セキュリティ上、匿名ユーザーは削除することが推奨されます。​
+ リモートからの root ログインの無効化: root ユーザーのリモートログインを無効にすることで、セキュリティを強化できます。​
+ テストデータベースの削除: 不要なテストデータベースを削除することで、セキュリティリスクを減少させます。​
+ 特権テーブルの再読み込み: 上記の変更を即時反映させるために、特権テーブルを再読み込みします。​

これらの手順を順に実行することで、MariaDB の基本的なセキュリティ設定が完了します。
各項目の意味を理解して、自分の利用状況に応じて設定します。


** mysql_secure_installation の使用例 [#t54d6ce8]
 sudo mysql_secure_installation

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): &color(red){←Enterキーを押す(インストール直後でパスワードが未設定の場合は、そのまま Enter キーを押す)};
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] n &color(red){←nを入力(unix_socket認証はLinuxのユーザー認証を使う方式。nでもLinuxのrootはMariaDBにログインできる。)};
... skipping.

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

Change the root password? [Y/n] n &color(red){←nを入力(nにするとLinuxのrootだけがMariaDBにログインできる。後で作業用のMariaDBユーザーを追加すればOK)};
... skipping.

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 &color(red){←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 &color(red){←Yを入力(root のリモートログインは無効とする)};
... 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 &color(red){←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 &color(red){←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!


//----------------------------------------
* 作業用の MariaDB ユーザーを追加する方法 [#oe13e596]
MariaDB に Linuxのrootユーザーでログインし、作業用のユーザーを作成できます。


** MariaDB にログイン [#p3dda699]
 sudo mysql
↓
 [sudo] password for linuxuser: ←今ログインしているLinuxユーザーのパスワードを入力(仮にlinuxuserというユーザーならlinuxuserのパスワードを入力する) 
 Welcome to the MariaDB monitor.  Commands end with ; or \g.
 Your MariaDB connection id is 37
 Server version: 10.11.8-MariaDB-0ubuntu0.24.04.1 Ubuntu 24.04
 
 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を操作するプロンプトに変わった。


** MariaDB からログアウト [#x51ea829]
MariaDBの操作から抜け出すときは「exit」というコマンドを入力すればOK。

 MariaDB [(none)]> exit
 Bye


** 作業用の MariaDB ユーザーを作成 [#h060ae0b]
MariaDBにログインして、以下のコマンドを入力する。

 CREATE USER 'youruser'@'localhost' IDENTIFIED BY 'yourpassword';
 GRANT ALL PRIVILEGES ON *.* TO 'youruser'@'localhost' WITH GRANT OPTION;
 FLUSH PRIVILEGES;

- youruser → 作業用ユーザー名(自由に変更可)
- yourpassword → 任意のパスワード


** ユーザー情報一覧表示 [#e43ac90a]
 select user,host,password from mysql.user;
↓
 +-------------+-----------+-------------------------------------------+
 | User        | Host      | Password                                  |
 +-------------+-----------+-------------------------------------------+
 | mariadb.sys | localhost |                                           |
 | root        | localhost | invalid                                   |
 | mysql       | localhost | invalid                                   |
 | youruser    | localhost | *1234567890ABCDEF1234567890ABCDEF12345678 |
 +-------------+-----------+-------------------------------------------+
 4 rows in set (0.001 sec)

先ほど追加したMariaDBの作業用ユーザー「youruser」があればOKです。


** 作業用ユーザーでログインできるか確認 [#ycff6640]
いったんMariaDBからログアウトして、MariaDBの作業用ユーザーでログインし直してみます。
 mysql -u youruser -p
パスワードを入力すると、作業用ユーザーで MariaDB にログインできます。

もしうまくいかなければ、作業用ユーザーを作り直すか、ChatGPTにエラーメッセージを入力して解決方法を質問してみましょう。

----

以上で、MariaDBが使えるようになりました。


//----------------------------------------
// *Amazon [#amazon]
#html{{

}}

//----------------------------------------
* 参考 [#reference]
//----------------------------------------

- MariaDB インストール Ubuntu 24.04 - Google 検索
https://www.google.com/search?q=MariaDB+%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB+Ubuntu+24.04

- Ubuntu 24.04 : MariaDB 10.11 インストールと設定 : Server World
https://www.server-world.info/query?os=Ubuntu_24.04&p=mariadb
→「mysql_secure_installation」の項目を参照。


//----------------------------------------
* 関連 [#lb8d3c15]
//----------------------------------------

// -[[]]

トップ   編集 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS