🐬

Debian 12 bookworm に MariaDB をインストールして Hello World!

2024/05/18に公開

MariaDB 公式サイトのスクリーンショット

この記事はインフラ初心者が纏めているため、あまり信用しないでください。この記事を参考にセットアップしたことによって被った損害・損失に対し、いかなる場合でも一切の責任を負いませんのでご了承ください。あくまでも自己責任でお願いします🙇‍♂️

本記事では、Debian のインストールと設定は完了しているものとする。最終的に目指す構成は以下の通り。

  • OS: Debian 12 bookworm
  • web サーバー + PHP: FrankenPHP (Caddy + PHP8.3)
  • データベース: MariaDB - 本記事

データベース ( MariaDB ) のインストール

まずは MariaDB をインストール。

$ sudo apt install -y mariadb-server

MariaDB の初期設定

$ 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):
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'.

# Unix_Socket 認証に切り替えるか否か
# n を選択しても root のみデフォルトで Unix_Socket 認証は有効
Switch to unix_socket authentication [Y/n] n
 ... skipping.

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

# root パスワードを設定するか否か
# パスワードを設定するとパスワードでも root ログインが可能となる
# パスワードを設定しない場合は sudo や su を使ったときに MariaDB の root ユーザーとしてログイン可能
Change the root password? [Y/n] n
 ... 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
 ... Success!

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

# root のリモートログインは無効とする
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!

root ユーザーでデータベースにログイン

$ sudo mysql

※ ここで指す root ユーザーはデータベースの root ユーザーであり、Debian とは別である。

データベースの一覧を表示

$ MariaDB [(none)]> show databases; 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.001 sec)

データベースを作成

$ MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.001 sec)

ユーザーの一覧を表示

$ MariaDB [(none)]> select user, host from mysql.user;

ユーザーの作成 1

下記の例で作成されるユーザーは、リモート接続が許可されていないユーザーが作成される。

$ MariaDB [(none)]> grant all ON *.* to 'user1'@'localhost' identified by 'user1password;
Query OK, 0 rows affected (0.001 sec)

ユーザーの作成 2

下記の例で作成されるユーザーは、リモート接続(192.168.10.40 が割り振られた PC からの接続)が許可されているユーザーが作成される。

$ MariaDB [(none)]> grant all ON *.* to 'user1'@'192.168.10.40' identified by 'user1password';
Query OK, 0 rows affected (0.001 sec)

ユーザーの作成 3

下記の例で作成されるユーザーは、データベース名 wordpress に対して全ての操作が許可されて且つリモート接続(192.168.10.40 が割り振られた PC からの接続)が許可されているユーザーが作成される。

$ MariaDB [(none)]> grant all ON wordpress.* to 'user1'@'192.168.10.40' identified by 'user1password';
Query OK, 0 rows affected (0.001 sec)

ユーザーを削除

$ MariaDB [(none)]> drop user username@localhost;

リモート接続を許可する

MariaDB の設定ファイルにある bind-address の値を変更すると、権限が付与されているユーザーはリモート接続が可能になる。

$ sudo vi /etc/mysql/mariadb.conf.d/50-server.cnf

- bind-address            = 127.0.0.1
+ bind-address            = 0.0.0.0

変更後、MariaDB を再起動させる。

$ sudo systemctl restart mysql

関連記事

Discussion