序章

MariaDB は、オープンソースのリレーショナルデータベース管理システムであり、人気のある LAMP (Linux、Apache、MySQL、PHP / Python / Perl)のデータベース部分としてMySQLの代替として一般的に使用されます。 )スタック。 これは、MySQLのドロップイン代替品となることを目的としています。

このインストールガイドの短いバージョンは、次の3つのステップで構成されています。

  • を使用してパッケージインデックスを更新します apt
  • をインストールします mariadb-server を使用したパッケージ apt. このパッケージには、MariaDBと対話するための関連ツールも含まれています
  • 含まれているを実行します mysql_secure_installation サーバーへのアクセスを制限するセキュリティスクリプト
  1. sudo apt update
  2. sudo apt install mariadb-server
  3. sudo mysql_secure_installation

このチュートリアルでは、MariaDBをUbuntu 22.04サーバーにインストールし、それが実行されており、安全な初期構成になっていることを確認する方法について説明します。

前提条件

  • このチュートリアルに従うには、Ubuntu22.04を実行しているサーバーが必要です。 このサーバーには、root以外の管理ユーザーとUFWで構成されたファイアウォールが必要です。 Ubuntu22.04の初期サーバーセットアップガイドに従ってこれをセットアップします。

ステップ1—MariaDBをインストールする

この記事の執筆時点で、Ubuntu22.04のデフォルトのAPTリポジトリにはMariaDBバージョン10.5.12が含まれています。

これをインストールするには、サーバーのパッケージインデックスを次のように更新します。 apt:

  1. sudo apt update

次に、パッケージをインストールします。

  1. sudo apt install mariadb-server

これらのコマンドはMariaDBをインストールしますが、パスワードの設定やその他の構成変更を求めるプロンプトは表示されません。 デフォルトの構成ではMariaDBのインストールが安全でないため、次のようなスクリプトを使用します。 mariadb-server パッケージは、サーバーへのアクセスを制限し、未使用のアカウントを削除するために提供されます。

ステップ2—MariaDBの構成

新しいMariaDBインストールの場合、次のステップは、含まれているセキュリティスクリプトを実行することです。 このスクリプトは、リモートrootログインやサンプルユーザーなどの安全性の低いデフォルトオプションの一部を変更します。

セキュリティスクリプトを実行します。

  1. sudo mysql_secure_installation

これにより、MariaDBインストールのセキュリティオプションにいくつかの変更を加えることができる一連のプロンプトが表示されます。 最初のプロンプトでは、現在のデータベースrootのパスワードを入力するように求められます。 まだ設定していないので、 ENTER 「なし」を示します。

Output
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, you'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none):

UNIXソケット認証に切り替えるかどうかを尋ねられます。 Since you already have a protected root account, you can skip this step. タイプ n 次にを押します ENTER.

Output
. . . 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

次のプロンプトでは、データベースrootパスワードを設定するかどうかを尋ねられます。 Ubuntuでは、MariaDBの root アカウントは自動システムメンテナンスと密接に関連しているため、そのアカウントに構成されている認証方法を変更しないでください。

そうすることで、管理者アカウントへのアクセスを削除することにより、パッケージの更新によってデータベースシステムが破壊される可能性があります。 タイプ n 次にを押します ENTER.

Output
. . . OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation. Set root password? [Y/n] n

後で、ソケット認証がユースケースに適していない場合に、パスワードアクセス用に追加の管理者アカウントを設定する方法について説明します。

そこから、を押すことができます Y その後 ENTER 後続のすべての質問のデフォルトを受け入れます。 これにより、一部の匿名ユーザーとテストデータベースが削除され、リモート root ログインが無効になり、これらの新しいルールが読み込まれるため、MariaDBは行った変更をすぐに実装します。

これで、MariaDBの初期セキュリティ構成が完了しました。 次のステップはオプションですが、MariaDBサーバーに対してパスワードで認証する場合は、これに従う必要があります。

Step 3 — (Optional) Creating an Administrative User that Employs Password Authentication

On Ubuntu systems running MariaDB 10.5, the root MariaDB user is set to authenticate using the unix_socket パスワードではなく、デフォルトでプラグイン。 This allows for some greater security and usability in many cases, but it can also complicate things when you need to allow an external program (e.g., phpMyAdmin) administrative rights.

Because the server uses the root account for tasks like log rotation and starting and stopping the server, it is best not to change the root account’s authentication details. Changing credentials in the /etc/mysql/debian.cnf configuration file may work initially, but package updates could potentially overwrite those changes. Instead of modifying the root account, the package maintainers recommend creating a separate administrative account for password-based access.

To this end, we will create a new account called admin with the same capabilities as the root account, but configured for password authentication. Open up the MariaDB prompt from your terminal:

  1. sudo mariadb

Then create a new user with root privileges and password-based access. Be sure to change the username and password to match your preferences:

  1. GRANT ALL ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;

Flush the privileges to ensure that they are saved and available in the current session:

  1. FLUSH PRIVILEGES;

Following this, exit the MariaDB shell:

  1. exit

Finally, let’s test the MariaDB installation.

Step 4 — Testing MariaDB

デフォルトのリポジトリからインストールすると、MariaDBは自動的に実行を開始します。 これをテストするには、そのステータスを確認します。

  1. sudo systemctl status mariadb

次のような出力が表示されます。

Output
● mariadb.service - MariaDB 10.5.12 database server Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2022-03-11 22:01:33 UTC; 14min ago Docs: man:mariadbd(8) https://mariadb.com/kb/en/library/systemd/ . . .

MariaDBが実行されていない場合は、コマンドで開始できます sudo systemctl start mariadb.

追加のチェックについては、を使用してデータベースに接続してみてください。 mysqladmin ツール。管理コマンドを実行できるようにするクライアントです。 たとえば、このコマンドは、Unixソケットを使用して root としてMariaDBに接続し、バージョンを返すように指示します。

  1. sudo mysqladmin version

次のような出力が表示されます。

Output
mysqladmin Ver 9.1 Distrib 10.5.12-MariaDB, for debian-linux-gnu on x86_64 Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Server version 10.5.12-MariaDB-1build1 Protocol version 10 Connection Localhost via UNIX socket UNIX socket /run/mysqld/mysqld.sock Uptime: 15 min 53 sec Threads: 1 Questions: 482 Slow queries: 0 Opens: 171 Open tables: 28 Queries per second avg: 0.505

結論

このガイドでは、MariaDBリレーショナルデータベース管理システムをインストールし、 mysql_secure_installation インストールされたスクリプト。 また、MariaDBサーバーの機能をテストする前に、パスワード認証を使用する新しい管理ユーザーを作成するオプションもありました。

実行中の安全なMariaDBサーバーができたので、サーバーを操作するために実行できる次の手順の例を次に示します。