ウェブサイト検索

Ubuntu 22.04にMariaDBをインストールして使用する方法


Ubuntu 22.04にMariaDBをインストールして使用する方法

このページで

  1. 前提条件
  2. MariaDBをインストールして構成する
  3. MariaDBのバージョンを確認する
  4. 結論

MariaDBは、アプリケーションデータを保存するために使用される無料のオープンソースで強力なデータベース管理システムです。それは安定したリレーショナルデータベース管理システムであり、人気のあるMySQLデータベースシステムのフォークです。特にスケーラビリティとミッションクリティカルな展開のために設計されています。このチュートリアルを書いている時点での最新バージョンは10.6.7です。すべてのメジャーリリースは少なくとも5年間サポートされます。したがって、MariaDB 10.6.7は2026年までサポートされます。

このチュートリアルでは、Ubuntu 22.04サーバーにMariaDB 10.6をインストールする方法をお見せします。

前提条件

  • Ubuntu 22.04を実行しているサーバー。
  • サーバーにはルートパスワードが設定されています。

MariaDBをインストールして構成する

まず、次のコマンドでシステムパッケージをすべて更新します:

apt update -y

すべてのパッケージが更新されたら、以下のコマンドを使用してMariaDBサーバーとクライアントの最新バージョンをインストールします。

apt-get install mariadb-server mariadb-client -y

インストールが成功したら、MariaDB サービスを開始し、システム再起動時に自動的に起動するように設定します:

systemctl start mariadb
systemctl enable mariadb
systemctl status mariadb

次の出力が表示されるはずです:

? mariadb.service - MariaDB 10.6.7 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-06-25 14:34:47 UTC; 39s ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
    Process: 3174 ExecStartPre=/usr/bin/install -m 755 -o mysql -g root -d /var/run/mysqld (code=exited, status=0/SUCCESS)
    Process: 3175 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
    Process: 3177 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= ||   VAR=`cd /usr/bin/..; /usr/bin/galera_recovery`; [ $?>
    Process: 3218 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
    Process: 3220 ExecStartPost=/etc/mysql/debian-start (code=exited, status=0/SUCCESS)
   Main PID: 3206 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 13 (limit: 4579)
     Memory: 57.2M
        CPU: 582ms
     CGroup: /system.slice/mariadb.service
             ??3206 /usr/sbin/mariadbd

Jun 25 14:34:47 ubuntu2204 mariadbd[3206]: 2022-06-25 14:34:47 0 [Note] /usr/sbin/mariadbd: ready for connections.
Jun 25 14:34:47 ubuntu2204 mariadbd[3206]: Version: '10.6.7-MariaDB-2ubuntu1'  socket: '/run/mysqld/mysqld.sock'  port: 3306  Ubuntu 22.04
Jun 25 14:34:47 ubuntu2204 systemd[1]: Started MariaDB 10.6.7 database server.
Jun 25 14:34:47 ubuntu2204 /etc/mysql/debian-start[3222]: Upgrading MySQL tables if necessary.

次に、MariaDBのインストールを保護し、MariaDBのルートパスワードを設定する必要があります。次のスクリプトを実行することで行うことができます:

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): 

Enterキーを押すだけです。以下に示すように、unix_socket認証に切り替えるように求められます:

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

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

Switch to unix_socket authentication [Y/n] Y

Yを入力してEnterキーを押してください。以下のようにルートパスワードを変更するように求められます:

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

Yを入力してEnterを押してルートパスワードを変更してください。

New password: 
Re-enter new password: 

安全なパスワードを入力してEnterを押してください。次の出力が表示されるはずです:

Password updated successfully!
Reloading privilege tables..
 ... Success!


Next, you will be asked to remove anonymous users as shown below:

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

Yを入力してEnterを押すと、匿名ユーザーを削除できます。以下のようにリモートルートログインを許可しないように求められます:

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

Yを入力してEnterキーを押してください。以下のようにテストデータベースを削除するよう求められます:

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

Yを入力してEnterキーを押すとテストデータベースが削除されます。以下のように特権テーブルを再読み込みするように求められます:

 - 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

Yを入力してEnterを押してください。次の出力が表示されるはずです:

 ... 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のバージョンを確認する

この時点で、MariaDBはインストールされており、セキュリティが確保されています。次に、インストールされているMariaDBのバージョンを確認する必要があります。

まず、次のコマンドを使ってMariaDBシェルにログインします:

mysql -u root -p

MariaDBのrootパスワードを入力し、Enterを押してください。ログインすると、以下の出力が表示されるはずです:

Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 41
Server version: 10.6.7-MariaDB-2ubuntu1 Ubuntu 22.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 [(none)]> SELECT VERSION();

次の出力を得るべきです:

+-------------------------+
| VERSION()               |
+-------------------------+
| 10.6.7-MariaDB-2ubuntu1 |
+-------------------------+
1 row in set (0.000 sec)

結論

おめでとうございます!あなたはUbuntu 22.04サーバーにMariaDB 10.6を正常にインストールし、セキュリティを確保しました。これで新しいデータベースとユーザーを作成し、それをアプリケーションに接続できます。

関連記事: